Unverified Commit 290ecac9 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

fix(build): add sourcePage context for PPR dynamic route lambda creation (#81781)

### What?

This PR adds a `sourcePage` property to dynamic routes in the routes
manifest to provide the necessary context for lambda creation during the
build process when PPR is enabled.

### Why?

The build process was missing the required context to properly create
lambdas for dynamic routes when PPR is enabled. Without the source page
information, the build couldn't determine which page a dynamic route
originated from, preventing correct lambda generation.

### How?

- Added a new `DynamicManifestRoute` type that extends `ManifestRoute`
with an optional `sourcePage` property
- Updated the `pageToRoute` function with overloads to pass the source
page context
- Modified the routes manifest generation to include the source page
when creating dynamic routes for PPR-enabled pages
- The `sourcePage` property is only populated for app pages when PPR is
enabled (remains `undefined` for regular dynamic routes)

This ensures the build process has the necessary context to create the
underlying lambdas correctly.
parent 2817a46d
...@@ -412,6 +412,15 @@ export type ManifestRoute = ManifestBuiltRoute & { ...@@ -412,6 +412,15 @@ export type ManifestRoute = ManifestBuiltRoute & {
skipInternalRouting?: boolean skipInternalRouting?: boolean
} }
type DynamicManifestRoute = ManifestRoute & {
/**
* The source page that this route is based on. This is used to determine the
* source page for the route and is only relevant for app pages where PPR is
* enabled and the page differs from the source page.
*/
sourcePage: string | undefined
}
type ManifestDataRoute = { type ManifestDataRoute = {
page: string page: string
routeKeys?: { [key: string]: string } routeKeys?: { [key: string]: string }
...@@ -431,7 +440,7 @@ export type RoutesManifest = { ...@@ -431,7 +440,7 @@ export type RoutesManifest = {
} }
headers: Array<ManifestHeaderRoute> headers: Array<ManifestHeaderRoute>
staticRoutes: Array<ManifestRoute> staticRoutes: Array<ManifestRoute>
dynamicRoutes: ReadonlyArray<ManifestRoute> dynamicRoutes: ReadonlyArray<DynamicManifestRoute>
dataRoutes: Array<ManifestDataRoute> dataRoutes: Array<ManifestDataRoute>
i18n?: { i18n?: {
domains?: ReadonlyArray<{ domains?: ReadonlyArray<{
...@@ -479,11 +488,35 @@ export type RoutesManifest = { ...@@ -479,11 +488,35 @@ export type RoutesManifest = {
} }
} }
function pageToRoute(page: string): ManifestRoute { /**
* Converts a page to a manifest route.
*
* @param page The page to convert to a route.
* @returns A route object.
*/
function pageToRoute(page: string): ManifestRoute
/**
* Converts a page to a dynamic manifest route.
*
* @param page The page to convert to a route.
* @param sourcePage The source page that this route is based on. This is used
* to determine the source page for the route and is only relevant for app
* pages when PPR is enabled on them.
* @returns A route object.
*/
function pageToRoute(
page: string,
sourcePage: string | undefined
): DynamicManifestRoute
function pageToRoute(
page: string,
sourcePage?: string
): DynamicManifestRoute | ManifestRoute {
const routeRegex = getNamedRouteRegex(page, { const routeRegex = getNamedRouteRegex(page, {
prefixRouteKeys: true, prefixRouteKeys: true,
}) })
return { return {
sourcePage,
page, page,
regex: normalizeRouteRegex(routeRegex.re.source), regex: normalizeRouteRegex(routeRegex.re.source),
routeKeys: routeRegex.routeKeys, routeKeys: routeRegex.routeKeys,
...@@ -1329,7 +1362,7 @@ export default async function build( ...@@ -1329,7 +1362,7 @@ export default async function build(
const isAppPPREnabled = checkIsAppPPREnabled(config.experimental.ppr) const isAppPPREnabled = checkIsAppPPREnabled(config.experimental.ppr)
const routesManifestPath = path.join(distDir, ROUTES_MANIFEST) const routesManifestPath = path.join(distDir, ROUTES_MANIFEST)
const dynamicRoutes: Array<ManifestRoute> = [] const dynamicRoutes: Array<DynamicManifestRoute> = []
/** /**
* A map of all the pages to their sourcePage value. This is only used for * A map of all the pages to their sourcePage value. This is only used for
...@@ -1347,7 +1380,13 @@ export default async function build( ...@@ -1347,7 +1380,13 @@ export default async function build(
for (const route of sortedRoutes) { for (const route of sortedRoutes) {
if (isDynamicRoute(route)) { if (isDynamicRoute(route)) {
dynamicRoutes.push(pageToRoute(route)) dynamicRoutes.push(
pageToRoute(
route,
// This property is only relevant when PPR is enabled.
undefined
)
)
} else if (!isReservedPage(route)) { } else if (!isReservedPage(route)) {
staticRoutes.push(pageToRoute(route)) staticRoutes.push(pageToRoute(route))
} }
...@@ -3123,7 +3162,7 @@ export default async function build( ...@@ -3123,7 +3162,7 @@ export default async function build(
// entry in the app routes manifest which enables routing for // entry in the app routes manifest which enables routing for
// this fallback shell. // this fallback shell.
if (!dynamicRoute) { if (!dynamicRoute) {
dynamicRoute = pageToRoute(route.pathname) dynamicRoute = pageToRoute(route.pathname, page)
sourcePages.set(route.pathname, page) sourcePages.set(route.pathname, page)
// This route is not for the internal router, but instead // This route is not for the internal router, but instead
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment