Skip to content

Commit f22fea3

Browse files
heiskrCopilot
andauthored
perf: optimize journey path resolver and middleware (#58955)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent e31ddba commit f22fea3

File tree

2 files changed

+74
-26
lines changed

2 files changed

+74
-26
lines changed

src/journeys/lib/journey-path-resolver.ts

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ type ContentContext = {
6868

6969
// Cache for journey pages so we only filter all pages once
7070
let cachedJourneyPages: JourneyPage[] | null = null
71+
// Cache for guide paths to quickly check if a page is part of any journey
72+
let cachedGuidePaths: Set<string> | null = null
73+
let hasDynamicGuides = false
74+
75+
function needsRendering(str: string): boolean {
76+
return str.includes('{{') || str.includes('{%') || str.includes('[') || str.includes('<')
77+
}
7178

7279
function getJourneyPages(pages: Pages): JourneyPage[] {
7380
if (!cachedJourneyPages) {
@@ -78,6 +85,27 @@ function getJourneyPages(pages: Pages): JourneyPage[] {
7885
return cachedJourneyPages
7986
}
8087

88+
function getGuidePaths(pages: Pages): Set<string> {
89+
if (!cachedGuidePaths) {
90+
cachedGuidePaths = new Set()
91+
const journeyPages = getJourneyPages(pages)
92+
for (const page of journeyPages) {
93+
if (!page.journeyTracks) continue
94+
for (const track of page.journeyTracks) {
95+
if (!track.guides) continue
96+
for (const guide of track.guides) {
97+
if (needsRendering(guide.href)) {
98+
hasDynamicGuides = true
99+
} else {
100+
cachedGuidePaths.add(normalizeGuidePath(guide.href))
101+
}
102+
}
103+
}
104+
}
105+
}
106+
return cachedGuidePaths
107+
}
108+
81109
function normalizeGuidePath(path: string): string {
82110
// First ensure we have a leading slash for consistent processing
83111
const pathWithSlash = path.startsWith('/') ? path : `/${path}`
@@ -133,6 +161,16 @@ export async function resolveJourneyContext(
133161
): Promise<JourneyContext | null> {
134162
const normalizedPath = normalizeGuidePath(articlePath)
135163

164+
// Optimization: Fast path check
165+
// If we are not forcing a specific journey page, check our global cache
166+
if (!currentJourneyPage) {
167+
const guidePaths = getGuidePaths(pages)
168+
// If we have no dynamic guides and this path isn't in our known guides, return null early.
169+
if (!hasDynamicGuides && !guidePaths.has(normalizedPath)) {
170+
return null
171+
}
172+
}
173+
136174
// Use the current journey page if provided, otherwise find all journey pages
137175
const journeyPages = currentJourneyPage ? [currentJourneyPage] : getJourneyPages(pages)
138176

@@ -165,15 +203,17 @@ export async function resolveJourneyContext(
165203
let renderedGuidePath = guidePath
166204

167205
// Handle Liquid conditionals in guide paths
168-
try {
169-
renderedGuidePath = await executeWithFallback(
170-
context,
171-
() => renderContent(guidePath, context, { textOnly: true }),
172-
() => guidePath,
173-
)
174-
} catch {
175-
// If rendering fails, use the original path rather than erroring
176-
renderedGuidePath = guidePath
206+
if (needsRendering(guidePath)) {
207+
try {
208+
renderedGuidePath = await executeWithFallback(
209+
context,
210+
() => renderContent(guidePath, context, { textOnly: true }),
211+
() => guidePath,
212+
)
213+
} catch {
214+
// If rendering fails, use the original path rather than erroring
215+
renderedGuidePath = guidePath
216+
}
177217
}
178218

179219
const normalizedGuidePath = normalizeGuidePath(renderedGuidePath)
@@ -189,15 +229,17 @@ export async function resolveJourneyContext(
189229
let renderedAlternativeNextStep = alternativeNextStep
190230

191231
// Handle Liquid conditionals in branching text which likely has links
192-
try {
193-
renderedAlternativeNextStep = await executeWithFallback(
194-
context,
195-
() => renderContent(alternativeNextStep, context),
196-
() => alternativeNextStep,
197-
)
198-
} catch {
199-
// If rendering fails, use the original branching text rather than erroring
200-
renderedAlternativeNextStep = alternativeNextStep
232+
if (needsRendering(alternativeNextStep)) {
233+
try {
234+
renderedAlternativeNextStep = await executeWithFallback(
235+
context,
236+
() => renderContent(alternativeNextStep, context),
237+
() => alternativeNextStep,
238+
)
239+
} catch {
240+
// If rendering fails, use the original branching text rather than erroring
241+
renderedAlternativeNextStep = alternativeNextStep
242+
}
201243
}
202244

203245
result = {
@@ -278,10 +320,14 @@ export async function resolveJourneyTracks(
278320
const result = await Promise.all(
279321
journeyTracks.map(async (track) => {
280322
// Render Liquid templates in title and description
281-
const renderedTitle = await renderContent(track.title, context, { textOnly: true })
282-
const renderedDescription = track.description
283-
? await renderContent(track.description, context, { textOnly: true })
284-
: undefined
323+
const renderedTitle = needsRendering(track.title)
324+
? await renderContent(track.title, context, { textOnly: true })
325+
: track.title
326+
327+
const renderedDescription =
328+
track.description && needsRendering(track.description)
329+
? await renderContent(track.description, context, { textOnly: true })
330+
: track.description
285331

286332
const guides = await Promise.all(
287333
track.guides.map(async (guide: { href: string; alternativeNextStep?: string }) => {

src/journeys/middleware/journey-track.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import type { Response, NextFunction } from 'express'
22
import type { ExtendedRequest, Context } from '@/types'
33

4+
import { resolveJourneyTracks, resolveJourneyContext } from '../lib/journey-path-resolver'
5+
46
export default async function journeyTrack(
57
req: ExtendedRequest & { context: Context },
68
res: Response,
79
next: NextFunction,
810
) {
11+
if (req.method !== 'GET' && req.method !== 'HEAD') return next()
12+
913
if (!req.context) throw new Error('request is not contextualized')
1014
if (!req.context.page) return next()
1115

1216
try {
13-
const journeyResolver = await import('../lib/journey-path-resolver')
14-
1517
// If this page has journey tracks defined, resolve them for the landing page
1618
if ((req.context.page as any).journeyTracks) {
17-
const resolvedTracks = await journeyResolver.resolveJourneyTracks(
19+
const resolvedTracks = await resolveJourneyTracks(
1820
(req.context.page as any).journeyTracks,
1921
req.context,
2022
)
@@ -24,7 +26,7 @@ export default async function journeyTrack(
2426
}
2527

2628
// Always try to resolve journey context (for navigation on guide articles)
27-
const journeyContext = await journeyResolver.resolveJourneyContext(
29+
const journeyContext = await resolveJourneyContext(
2830
req.pagePath || '',
2931
req.context.pages || {},
3032
req.context,

0 commit comments

Comments
 (0)