@@ -68,6 +68,13 @@ type ContentContext = {
6868
6969// Cache for journey pages so we only filter all pages once
7070let 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
7279function 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+
81109function 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 } ) => {
0 commit comments