Skip to content

Commit 2a1a2cf

Browse files
authored
resolve journey data from middleware (#58330)
1 parent 5043f52 commit 2a1a2cf

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

src/journeys/middleware/journey-track.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Response, NextFunction } from 'express'
22
import type { ExtendedRequest, Context } from '@/types'
3-
import { resolveJourneyContext } from '../lib/journey-path-resolver'
43

54
export default async function journeyTrack(
65
req: ExtendedRequest & { context: Context },
@@ -10,8 +9,30 @@ export default async function journeyTrack(
109
if (!req.context) throw new Error('request is not contextualized')
1110
if (!req.context.page) return next()
1211

12+
// Only run journey resolution if the page has journey tracks defined
13+
if (!(req.context.page as any).journeyTracks) {
14+
req.context.currentJourneyTrack = null
15+
return next()
16+
}
17+
1318
try {
14-
const journeyContext = await resolveJourneyContext(
19+
// Import and use the journey resolver which uses renderContent, need the
20+
// async import since it uses fs Node apis
21+
const journeyResolver = await import('../lib/journey-path-resolver')
22+
23+
// resolve the journey tracks which renders the journey content like the
24+
// description to handle liquid rendering
25+
const resolvedTracks = await journeyResolver.resolveJourneyTracks(
26+
(req.context.page as any).journeyTracks,
27+
req.context,
28+
)
29+
30+
// Store resolved tracks on the page context for later use in getServerSideProps
31+
;(req.context.page as any).resolvedJourneyTracks = resolvedTracks
32+
33+
// resolve the current journey context since we're on a journey track page
34+
// i.e. next/prev articles in the track, this article's position in the track
35+
const journeyContext = await journeyResolver.resolveJourneyContext(
1536
req.pagePath || '',
1637
req.context.pages || {},
1738
req.context,

src/landings/context/LandingContext.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,9 @@ export const getLandingContextFromRequest = async (
6060
}
6161
}
6262

63-
let journeyTracks: JourneyTrack[] = []
64-
if (landingType === 'journey' && page.journeyTracks) {
65-
// Need a dynamic import because journey-path-resolver uses Node fs apis
66-
const { resolveJourneyTracks } = await import('@/journeys/lib/journey-path-resolver')
67-
journeyTracks = await resolveJourneyTracks(page.journeyTracks, req.context)
68-
}
63+
// Note: Journey tracks are resolved in middleware and added to the request
64+
// context to avoid the error using server side apis client side
65+
const journeyTracks: JourneyTrack[] = []
6966

7067
return {
7168
landingType,

src/landings/pages/product.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
178178
additionalUINamespaces.push('product_landing')
179179
} else if (currentLayoutName === 'journey-landing' || req.query?.feature === 'journey-landing') {
180180
props.journeyContext = await getLandingContextFromRequest(req, 'journey')
181+
182+
// journey tracks are resolved in middleware and added to the request
183+
// so we need to add them to the journey context here
184+
if ((req.context.page as any).resolvedJourneyTracks) {
185+
props.journeyContext.journeyTracks = (req.context.page as any).resolvedJourneyTracks
186+
}
187+
181188
additionalUINamespaces.push('journey_landing', 'product_landing')
182189
} else if (
183190
currentLayoutName === 'discovery-landing' ||

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"moduleResolution": "Bundler",
1717
"resolveJsonModule": true,
1818
"isolatedModules": true,
19-
"jsx": "preserve",
19+
"jsx": "react-jsx",
2020
"baseUrl": ".",
2121
"noEmit": true,
2222
"allowSyntheticDefaultImports": true,
@@ -41,6 +41,7 @@
4141
"**/*.ts",
4242
"**/*.tsx",
4343
"*.d.ts",
44-
".next/types/**/*.ts"
44+
".next/types/**/*.ts",
45+
".next/dev/types/**/*.ts"
4546
]
4647
}

0 commit comments

Comments
 (0)