resolve journey data from middleware (#58330)
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import type { Response, NextFunction } from 'express'
|
import type { Response, NextFunction } from 'express'
|
||||||
import type { ExtendedRequest, Context } from '@/types'
|
import type { ExtendedRequest, Context } from '@/types'
|
||||||
import { resolveJourneyContext } from '../lib/journey-path-resolver'
|
|
||||||
|
|
||||||
export default async function journeyTrack(
|
export default async function journeyTrack(
|
||||||
req: ExtendedRequest & { context: Context },
|
req: ExtendedRequest & { context: Context },
|
||||||
@@ -10,8 +9,30 @@ export default async function journeyTrack(
|
|||||||
if (!req.context) throw new Error('request is not contextualized')
|
if (!req.context) throw new Error('request is not contextualized')
|
||||||
if (!req.context.page) return next()
|
if (!req.context.page) return next()
|
||||||
|
|
||||||
|
// Only run journey resolution if the page has journey tracks defined
|
||||||
|
if (!(req.context.page as any).journeyTracks) {
|
||||||
|
req.context.currentJourneyTrack = null
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const journeyContext = await resolveJourneyContext(
|
// Import and use the journey resolver which uses renderContent, need the
|
||||||
|
// async import since it uses fs Node apis
|
||||||
|
const journeyResolver = await import('../lib/journey-path-resolver')
|
||||||
|
|
||||||
|
// resolve the journey tracks which renders the journey content like the
|
||||||
|
// description to handle liquid rendering
|
||||||
|
const resolvedTracks = await journeyResolver.resolveJourneyTracks(
|
||||||
|
(req.context.page as any).journeyTracks,
|
||||||
|
req.context,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Store resolved tracks on the page context for later use in getServerSideProps
|
||||||
|
;(req.context.page as any).resolvedJourneyTracks = resolvedTracks
|
||||||
|
|
||||||
|
// resolve the current journey context since we're on a journey track page
|
||||||
|
// i.e. next/prev articles in the track, this article's position in the track
|
||||||
|
const journeyContext = await journeyResolver.resolveJourneyContext(
|
||||||
req.pagePath || '',
|
req.pagePath || '',
|
||||||
req.context.pages || {},
|
req.context.pages || {},
|
||||||
req.context,
|
req.context,
|
||||||
|
|||||||
@@ -60,12 +60,9 @@ export const getLandingContextFromRequest = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let journeyTracks: JourneyTrack[] = []
|
// Note: Journey tracks are resolved in middleware and added to the request
|
||||||
if (landingType === 'journey' && page.journeyTracks) {
|
// context to avoid the error using server side apis client side
|
||||||
// Need a dynamic import because journey-path-resolver uses Node fs apis
|
const journeyTracks: JourneyTrack[] = []
|
||||||
const { resolveJourneyTracks } = await import('@/journeys/lib/journey-path-resolver')
|
|
||||||
journeyTracks = await resolveJourneyTracks(page.journeyTracks, req.context)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
landingType,
|
landingType,
|
||||||
|
|||||||
@@ -178,6 +178,13 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
|
|||||||
additionalUINamespaces.push('product_landing')
|
additionalUINamespaces.push('product_landing')
|
||||||
} else if (currentLayoutName === 'journey-landing' || req.query?.feature === 'journey-landing') {
|
} else if (currentLayoutName === 'journey-landing' || req.query?.feature === 'journey-landing') {
|
||||||
props.journeyContext = await getLandingContextFromRequest(req, 'journey')
|
props.journeyContext = await getLandingContextFromRequest(req, 'journey')
|
||||||
|
|
||||||
|
// journey tracks are resolved in middleware and added to the request
|
||||||
|
// so we need to add them to the journey context here
|
||||||
|
if ((req.context.page as any).resolvedJourneyTracks) {
|
||||||
|
props.journeyContext.journeyTracks = (req.context.page as any).resolvedJourneyTracks
|
||||||
|
}
|
||||||
|
|
||||||
additionalUINamespaces.push('journey_landing', 'product_landing')
|
additionalUINamespaces.push('journey_landing', 'product_landing')
|
||||||
} else if (
|
} else if (
|
||||||
currentLayoutName === 'discovery-landing' ||
|
currentLayoutName === 'discovery-landing' ||
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
"moduleResolution": "Bundler",
|
"moduleResolution": "Bundler",
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"jsx": "preserve",
|
"jsx": "react-jsx",
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
"allowSyntheticDefaultImports": true,
|
"allowSyntheticDefaultImports": true,
|
||||||
@@ -41,6 +41,7 @@
|
|||||||
"**/*.ts",
|
"**/*.ts",
|
||||||
"**/*.tsx",
|
"**/*.tsx",
|
||||||
"*.d.ts",
|
"*.d.ts",
|
||||||
".next/types/**/*.ts"
|
".next/types/**/*.ts",
|
||||||
|
".next/dev/types/**/*.ts"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user