Unverified Commit d884398d authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

feat(build): optimize filterUniqueParamsCombinations to generate sub-combinations (#81321)

### What?

Optimizes the `generateParamPrefixCombinations` function (previously
`filterUniqueRootParamsCombinations`) to generate all unique
sub-combinations of route parameters while ensuring partial shells
include complete sets of root parameters.

### Why?

The previous implementation only generated combinations at the root
parameter level, which was insufficient for PPR (Partial Prerendering)
shells. PPR needs all possible prefix combinations of route parameters
to create effective shells for nested routes. For example, for a route
`/[lang]/[region]/[slug]`, we need shells for:
- `/[lang]/[region]` (complete root params)
- `/[lang]/[region]/[slug]` (full route)

Without proper sub-combinations, nested routes couldn't benefit from PPR
shell optimization, leading to performance degradation.

### How?

- Renamed `filterUniqueRootParamsCombinations` to
`generateParamPrefixCombinations` to better reflect its expanded
functionality
- Added logic to generate all prefix combinations of route parameters,
not just root-level combinations
- Implemented root parameter validation to ensure partial shells only
include complete sets of root parameters
- Added comprehensive test coverage for edge cases including undefined
parameters, missing keys, and parameter collisions
- Optimized the algorithm to skip invalid combinations early when root
parameters are incomplete

The function now generates prefix combinations systematically while
maintaining backward compatibility and preventing invalid partial
routes.

NAR-159

---------
Co-authored-by: 's avatarHendrik Liebau <mail@hendrik-liebau.de>
parent 2b3728f9
...@@ -719,5 +719,6 @@ ...@@ -719,5 +719,6 @@
"718": "Invariant: projectDir is required for node runtime", "718": "Invariant: projectDir is required for node runtime",
"719": "Failed to get source map for '%s'. This is a bug in Next.js", "719": "Failed to get source map for '%s'. This is a bug in Next.js",
"720": "A client prerender store should not be used for a route handler.", "720": "A client prerender store should not be used for a route handler.",
"721": "Render in Browser" "721": "Render in Browser",
"722": "Unable to match pathname to a dynamic route"
} }
...@@ -402,6 +402,13 @@ export type ManifestRoute = ManifestBuiltRoute & { ...@@ -402,6 +402,13 @@ export type ManifestRoute = ManifestBuiltRoute & {
namedRegex?: string namedRegex?: string
routeKeys?: { [key: string]: string } routeKeys?: { [key: string]: string }
prefetchSegmentDataRoutes?: PrefetchSegmentDataRoute[] prefetchSegmentDataRoutes?: PrefetchSegmentDataRoute[]
/**
* If true, this indicates that the route should not be considered for routing
* for the internal router, and instead has been added to support external
* routers.
*/
skipInternalRouting?: boolean
} }
type ManifestDataRoute = { type ManifestDataRoute = {
...@@ -3096,18 +3103,44 @@ export default async function build( ...@@ -3096,18 +3103,44 @@ export default async function build(
} }
let prefetchDataRoute: string | undefined let prefetchDataRoute: string | undefined
let dynamicRoute = routesManifest.dynamicRoutes.find(
(r) => r.page === route.pathname
)
if (!isAppRouteHandler && isAppPPREnabled) { if (!isAppRouteHandler && isAppPPREnabled) {
prefetchDataRoute = path.posix.join( prefetchDataRoute = path.posix.join(
`${normalizedRoute}${RSC_PREFETCH_SUFFIX}` `${normalizedRoute}${RSC_PREFETCH_SUFFIX}`
) )
// If the dynamic route wasn't found, then we need to create
// it. This ensures that for each fallback shell there's an
// entry in the app routes manifest which enables routing for
// this fallback shell.
if (!dynamicRoute) {
dynamicRoute = pageToRoute(route.pathname)
// This route is not for the internal router, but instead
// for external routers.
dynamicRoute.skipInternalRouting = true
// Push this to the end of the array. The dynamic routes are
// sorted by page later.
routesManifest.dynamicRoutes.push(dynamicRoute)
}
} }
if (!isAppRouteHandler && metadata?.segmentPaths) { if (!isAppRouteHandler && metadata?.segmentPaths) {
const dynamicRoute = routesManifest.dynamicRoutes.find( // If PPR isn't enabled, then we might not find the dynamic
(r) => r.page === page // route by pathname. If that's the case, we need to find the
) // route by page.
if (!dynamicRoute) { if (!dynamicRoute) {
throw new Error('Dynamic route not found') dynamicRoute = routesManifest.dynamicRoutes.find(
(r) => r.page === page
)
// If it can't be found by page, we must throw an error.
if (!dynamicRoute) {
throw new InvariantError('Dynamic route not found')
}
} }
dynamicRoute.prefetchSegmentDataRoutes ??= [] dynamicRoute.prefetchSegmentDataRoutes ??= []
...@@ -3566,8 +3599,14 @@ export default async function build( ...@@ -3566,8 +3599,14 @@ export default async function build(
} }
}) })
// We need to write the manifest with rewrites after build as it might // As we may have modified the routesManifest.dynamicRoutes, we need to
// have been modified. // sort the dynamic routes by page.
routesManifest.dynamicRoutes = getSortedRouteObjects(
routesManifest.dynamicRoutes,
(route) => route.page
)
// Now write the routes manifest out.
await nextBuildSpan await nextBuildSpan
.traceChild('write-routes-manifest') .traceChild('write-routes-manifest')
.traceAsyncFn(() => writeManifest(routesManifestPath, routesManifest)) .traceAsyncFn(() => writeManifest(routesManifestPath, routesManifest))
......
This diff is collapsed.
...@@ -166,8 +166,8 @@ export async function handler( ...@@ -166,8 +166,8 @@ export async function handler(
let { isOnDemandRevalidate } = prepareResult let { isOnDemandRevalidate } = prepareResult
const prerenderInfo = prerenderManifest.dynamicRoutes[normalizedSrcPage] const prerenderInfo = routeModule.match(pathname, prerenderManifest)
const isPrerendered = prerenderManifest.routes[resolvedPathname] const isPrerendered = !!prerenderManifest.routes[resolvedPathname]
let isSSG = Boolean( let isSSG = Boolean(
prerenderInfo || prerenderInfo ||
...@@ -663,10 +663,17 @@ export async function handler( ...@@ -663,10 +663,17 @@ export async function handler(
let fallbackResponse: ResponseCacheEntry | null | undefined let fallbackResponse: ResponseCacheEntry | null | undefined
if (isRoutePPREnabled && !isRSCRequest) { if (isRoutePPREnabled && !isRSCRequest) {
const cacheKey =
typeof prerenderInfo?.fallback === 'string'
? prerenderInfo.fallback
: isProduction
? normalizedSrcPage
: null
// We use the response cache here to handle the revalidation and // We use the response cache here to handle the revalidation and
// management of the fallback shell. // management of the fallback shell.
fallbackResponse = await routeModule.handleResponse({ fallbackResponse = await routeModule.handleResponse({
cacheKey: isProduction ? normalizedSrcPage : null, cacheKey,
req, req,
nextConfig, nextConfig,
routeKind: RouteKind.APP_PAGE, routeKind: RouteKind.APP_PAGE,
......
...@@ -75,6 +75,7 @@ import { ...@@ -75,6 +75,7 @@ import {
getInstrumentationModule, getInstrumentationModule,
} from '../lib/router-utils/instrumentation-globals.external' } from '../lib/router-utils/instrumentation-globals.external'
import type { PrerenderManifest } from '../../build' import type { PrerenderManifest } from '../../build'
import { getRouteRegex } from '../../shared/lib/router/utils/route-regex'
// Load ReactDevOverlay only when needed // Load ReactDevOverlay only when needed
let PagesDevOverlayBridgeImpl: PagesDevOverlayBridgeType let PagesDevOverlayBridgeImpl: PagesDevOverlayBridgeType
...@@ -887,8 +888,22 @@ export default class DevServer extends Server { ...@@ -887,8 +888,22 @@ export default class DevServer extends Server {
existingManifest.routes[staticPath] = {} as any existingManifest.routes[staticPath] = {} as any
} }
existingManifest.dynamicRoutes[pathname] = { existingManifest.dynamicRoutes[pathname] = {
dataRoute: null,
dataRouteRegex: null,
fallback: fallbackModeToFallbackField(res.value.fallbackMode, page), fallback: fallbackModeToFallbackField(res.value.fallbackMode, page),
} as any fallbackRevalidate: false,
fallbackExpire: undefined,
fallbackHeaders: undefined,
fallbackStatus: undefined,
fallbackRootParams: undefined,
fallbackSourceRoute: pathname,
prefetchDataRoute: undefined,
prefetchDataRouteRegex: undefined,
routeRegex: getRouteRegex(pathname).re.source,
experimentalPPR: undefined,
renderingMode: undefined,
allowHeader: [],
}
const updatedManifest = JSON.stringify(existingManifest) const updatedManifest = JSON.stringify(existingManifest)
......
...@@ -281,6 +281,12 @@ export async function setupFsCheck(opts: { ...@@ -281,6 +281,12 @@ export async function setupFsCheck(opts: {
} }
for (const route of routesManifest.dynamicRoutes) { for (const route of routesManifest.dynamicRoutes) {
// If a route is marked as skipInternalRouting, it's not for the internal
// router, and instead has been added to support external routers.
if (route.skipInternalRouting) {
continue
}
dynamicRoutes.push({ dynamicRoutes.push({
...route, ...route,
match: getRouteMatcher(getRouteRegex(route.page)), match: getRouteMatcher(getRouteRegex(route.page)),
......
import { PrerenderManifestMatcher } from './prerender-manifest-matcher'
import type {
PrerenderManifest,
DynamicPrerenderManifestRoute,
} from '../../../../build'
import { RenderingMode } from '../../../../build/rendering-mode'
// Helper function to create a mock PrerenderManifest
function createMockPrerenderManifest(
dynamicRoutes: Record<string, DynamicPrerenderManifestRoute> = {}
): PrerenderManifest {
return {
version: 4,
routes: {},
dynamicRoutes,
notFoundRoutes: [],
preview: {
previewModeId: 'test-preview-id',
previewModeEncryptionKey: 'test-encryption-key',
previewModeSigningKey: 'test-signing-key',
},
}
}
// Helper function to create a mock DynamicPrerenderManifestRoute
function createMockDynamicRoute(
overrides: Partial<DynamicPrerenderManifestRoute> = {}
): DynamicPrerenderManifestRoute {
return {
dataRoute: null,
dataRouteRegex: null,
fallback: null,
fallbackRevalidate: false,
fallbackExpire: undefined,
fallbackHeaders: undefined,
fallbackStatus: undefined,
fallbackRootParams: undefined,
fallbackSourceRoute: undefined,
prefetchDataRoute: undefined,
prefetchDataRouteRegex: undefined,
routeRegex: '^/[^/]+(?:/[^/]+)?/?$',
experimentalPPR: undefined,
renderingMode: RenderingMode.STATIC,
allowHeader: ['host'],
...overrides,
}
}
describe('PrerenderManifestMatcher', () => {
describe('match', () => {
describe('successful matches', () => {
it('should respect route specificity order', () => {
const specificRoute = createMockDynamicRoute({
fallbackSourceRoute: '/[category]/[id]',
})
const catchAllRoute = createMockDynamicRoute({
fallbackSourceRoute: '/[category]/[id]',
})
// Order matters - more specific routes should come first
const manifest = createMockPrerenderManifest({
'/products/[id]': specificRoute,
'/[category]/[id]': catchAllRoute,
})
const matcher = new PrerenderManifestMatcher(
'/[category]/[id]',
manifest
)
const result = matcher.match('/products/123')
expect(result).toBe(specificRoute)
})
it('should handle when the fallbackSourceRoute is not set', () => {
const route = createMockDynamicRoute({
fallbackSourceRoute: undefined,
})
const manifest = createMockPrerenderManifest({
'/products/[id]': route,
})
const matcher = new PrerenderManifestMatcher('/products/[id]', manifest)
const result = matcher.match('/products/123')
expect(result).toBe(route)
})
})
describe('no match scenarios', () => {
it('should return null when no matching route is found', () => {
const route = createMockDynamicRoute({
fallbackSourceRoute: '/[category]/[id]',
})
const manifest = createMockPrerenderManifest({
'/products/[id]': route,
})
const matcher = new PrerenderManifestMatcher(
'/[category]/[id]',
manifest
)
const result = matcher.match('/non-matching-path')
expect(result).toBe(null)
})
it('should return null when no routes match the fallback source route', () => {
const route = createMockDynamicRoute({
fallbackSourceRoute: '/products/[id]',
})
const manifest = createMockPrerenderManifest({
'/products/[id]': route,
})
const matcher = new PrerenderManifestMatcher(
'/[category]/[id]',
manifest
)
const result = matcher.match('/products/123')
expect(result).toBe(null)
})
})
})
})
import type {
DynamicPrerenderManifestRoute,
PrerenderManifest,
} from '../../../../build'
import type { DeepReadonly } from '../../../../shared/lib/deep-readonly'
import {
getRouteMatcher,
type RouteMatchFn,
} from '../../../../shared/lib/router/utils/route-matcher'
import { getRouteRegex } from '../../../../shared/lib/router/utils/route-regex'
/**
* A matcher for a dynamic route.
*/
type Matcher = {
/**
* The matcher for the dynamic route. This is lazily created when the matcher
* is first used.
*/
matcher?: RouteMatchFn
/**
* The source of the dynamic route.
*/
source: string
/**
* The route that matches the source.
*/
route: DeepReadonly<DynamicPrerenderManifestRoute>
}
/**
* A matcher for the prerender manifest.
*
* This class is used to match the pathname to the dynamic route.
*/
export class PrerenderManifestMatcher {
private readonly matchers: Array<Matcher>
constructor(
pathname: string,
prerenderManifest: DeepReadonly<PrerenderManifest>
) {
this.matchers = Object.entries(prerenderManifest.dynamicRoutes)
.filter(([source, route]) => {
// If the pathname is a fallback source route, or the source route is
// the same as the pathname, then we should include it in the matchers.
return route.fallbackSourceRoute === pathname || source === pathname
})
.map(([source, route]) => ({ source, route }))
}
/**
* Match the pathname to the dynamic route. If no match is found, an error is
* thrown.
*
* @param pathname - The pathname to match.
* @returns The dynamic route that matches the pathname.
*/
public match(
pathname: string
): DeepReadonly<DynamicPrerenderManifestRoute> | null {
// Iterate over the matchers. They're already in the correct order of
// specificity as they were inserted into the prerender manifest that way
// and iterating over them with Object.entries guarantees that.
for (const matcher of this.matchers) {
// Lazily create the matcher, this is only done once per matcher.
if (!matcher.matcher) {
matcher.matcher = getRouteMatcher(getRouteRegex(matcher.source))
}
const match = matcher.matcher(pathname)
if (match) {
return matcher.route
}
}
return null
}
}
...@@ -3,6 +3,7 @@ import type RenderResult from '../../render-result' ...@@ -3,6 +3,7 @@ import type RenderResult from '../../render-result'
import type { RenderOpts } from '../../app-render/types' import type { RenderOpts } from '../../app-render/types'
import type { NextParsedUrlQuery } from '../../request-meta' import type { NextParsedUrlQuery } from '../../request-meta'
import type { LoaderTree } from '../../lib/app-dir-module' import type { LoaderTree } from '../../lib/app-dir-module'
import type { PrerenderManifest } from '../../../build'
import { import {
renderToHTMLOrFlight, renderToHTMLOrFlight,
...@@ -17,6 +18,8 @@ import * as vendoredContexts from './vendored/contexts/entrypoints' ...@@ -17,6 +18,8 @@ import * as vendoredContexts from './vendored/contexts/entrypoints'
import type { BaseNextRequest, BaseNextResponse } from '../../base-http' import type { BaseNextRequest, BaseNextResponse } from '../../base-http'
import type { ServerComponentsHmrCache } from '../../response-cache' import type { ServerComponentsHmrCache } from '../../response-cache'
import type { FallbackRouteParams } from '../../request/fallback-params' import type { FallbackRouteParams } from '../../request/fallback-params'
import { PrerenderManifestMatcher } from './helpers/prerender-manifest-matcher'
import type { DeepReadonly } from '../../../shared/lib/deep-readonly'
let vendoredReactRSC let vendoredReactRSC
let vendoredReactSSR let vendoredReactSSR
...@@ -67,6 +70,28 @@ export class AppPageRouteModule extends RouteModule< ...@@ -67,6 +70,28 @@ export class AppPageRouteModule extends RouteModule<
this.isAppRouter = true this.isAppRouter = true
} }
private matchers = new WeakMap<
DeepReadonly<PrerenderManifest>,
PrerenderManifestMatcher
>()
public match(
pathname: string,
prerenderManifest: DeepReadonly<PrerenderManifest>
) {
// Lazily create the matcher based on the provided prerender manifest.
let matcher = this.matchers.get(prerenderManifest)
if (!matcher) {
matcher = new PrerenderManifestMatcher(
this.definition.pathname,
prerenderManifest
)
this.matchers.set(prerenderManifest, matcher)
}
// Match the pathname to the dynamic route.
return matcher.match(pathname)
}
public render( public render(
req: BaseNextRequest, req: BaseNextRequest,
res: BaseNextResponse, res: BaseNextResponse,
......
import { getSentinelValue } from '../../sentinel'
export default async function Page({
params,
}: {
params: Promise<{ lang: string; slug: string }>
}) {
const { slug } = await params
return (
<>
<p id="page">Page: ({getSentinelValue()})</p>
<p>slug: {slug}</p>
</>
)
}
export function generateStaticParams({ params }: { params: { lang: string } }) {
return params.lang === 'fr' ? [{ slug: '1' }] : []
}
import { ReactNode, Suspense } from 'react'
import { getSentinelValue } from '../sentinel'
export default async function LangLayout({
children,
params,
}: {
children: ReactNode
params: Promise<{ lang: string }>
}) {
const { lang } = await params
return (
<>
<div id="lang-layout">Lang Layout: ({getSentinelValue()})</div>
<h1>lang: {lang}</h1>
<main>
<Suspense fallback={<p id="loading">Loading...</p>}>
{children}
</Suspense>
</main>
</>
)
}
export function generateStaticParams() {
return [{ lang: 'en' }, { lang: 'fr' }]
}
'use cache'
import { ReactNode, Suspense } from 'react'
import { getSentinelValue } from './sentinel'
export default async function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>
<div id="root-layout">Root Layout: ({getSentinelValue()})</div>
<Suspense fallback={<p id="loading">Loading...</p>}>
{children}
</Suspense>
</body>
</html>
)
}
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
export function getSentinelValue() {
return process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD
? 'buildtime'
: 'runtime'
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
dynamicIO: true,
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import * as cheerio from 'cheerio'
describe('sub-shell-generation', () => {
const { next, isNextDev, isNextDeploy } = nextTestSetup({
files: __dirname,
})
if (isNextDev) {
it.skip('skipping dev test', () => {})
return
}
describe('should serve the correct shell', () => {
describe.each([
[
'/[lang]/[slug]',
{
page: 'Page: (runtime)',
langLayout: 'Lang Layout: (runtime)',
rootLayout: 'Root Layout: (buildtime)',
},
['/es/1', '/es/2'],
true,
],
[
'/en/[slug]',
{
page: 'Page: (runtime)',
langLayout: 'Lang Layout: (buildtime)',
rootLayout: 'Root Layout: (buildtime)',
},
['/en/1', '/en/2'],
true,
],
[
'/fr/[slug]',
{
page: 'Page: (runtime)',
langLayout: 'Lang Layout: (buildtime)',
rootLayout: 'Root Layout: (buildtime)',
},
['/fr/2', '/fr/3'],
true,
],
[
'/fr/1',
{
page: 'Page: (buildtime)',
langLayout: 'Lang Layout: (buildtime)',
rootLayout: 'Root Layout: (buildtime)',
},
['/fr/1'],
false,
],
])('%s', (shell, { page, langLayout, rootLayout }, paths, isPostponed) => {
it.each(paths)('should serve the correct shell for %s', async (path) => {
const res = await next.fetch(path)
expect(res.status).toBe(200)
if (isNextDeploy) {
expect(res.headers.get('x-matched-path')).toBe(shell)
} else {
expect(res.headers.get('x-nextjs-postponed')).toBe(
isPostponed ? '1' : null
)
}
const html = await res.text()
const $ = cheerio.load(html)
expect({
page: $('#page').text(),
langLayout: $('#lang-layout').text(),
rootLayout: $('#root-layout').text(),
}).toEqual({
page,
langLayout,
rootLayout,
})
})
})
})
})
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