Compute dev fallback params from the most-specific prerendered route

When Cache Components is enabled, the development server threads a
`fallbackParams` request meta for dynamic app routes so the staged
render knows which params are not statically known and must be deferred
to a later stage. The previous computation walked the prerendered routes
from `getStaticPaths` and kept the one with the fewest fallback params,
without checking that the route actually matched the requested URL.
Consider `/mixed/[lang]/[id]` where `generateStaticParams` covers `lang:
'en'` but not `id`: the prerendered routes are the base
`/mixed/[lang]/[id]`, which defers `[lang, id]`, and the covered
`/mixed/en/[id]`, which defers only `[id]`. For the request
`/mixed/fr/123` the fewest-fallback route is `/mixed/en/[id]`, but `en`
does not match `fr`, so applying its `[id]` set left `lang` out of the
fallback set and `fr` was treated as a statically known value.

Because this meta decides which stage each param resolves in, and the
stage decides the environment a replayed `console.log` is attributed to,
treating `fr` as static resolved it in the prerender stage instead of
deferring it to the runtime stage. The computation now matches the
requested URL against each prerendered route with the canonical
`getRouteRegex` and, among the routes that match, picks the
most-specific one, the one with the fewest fallback params. For
`/mixed/fr/123` only the base route matches, so its `[lang, id]` set is
used and both params defer, while for `/mixed/en/123` the covered
`/mixed/en/[id]` still matches and wins, so `lang` resolves statically
and only `id` defers. This mirrors what a production build writes to the
prerender manifest, where the server matches the URL to the
most-specific prerendered route at request time. The change is
development-only, gated on the route module being in dev mode, and
production continues to read the manifest. A later change in this stack
reads the same `fallbackParams` meta for the Instant Navigation testing
API's on-demand shell render, so that path defers the identical per-URL
set a production prefetch would.
parent f56e2ac0
......@@ -61,6 +61,7 @@ import { isDynamicRoute } from '../shared/lib/router/utils'
import { execOnce } from '../shared/lib/utils'
import { isBlockedPage } from './utils'
import { getBotType, isBot } from '../shared/lib/router/utils/is-bot'
import { getRouteRegex } from '../shared/lib/router/utils/route-regex'
import RenderResult from './render-result'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
......@@ -2384,26 +2385,40 @@ export default abstract class Server<
if (isAppPath && this.nextConfig.cacheComponents) {
if (pathsResults.prerenderedRoutes?.length) {
let smallestFallbackRouteParams = null
// Replicate, on demand, the per-URL fallback set a production build
// writes to the prerender manifest. Production matches the requested
// URL to the most-specific prerendered route and defers that route's
// `fallbackRouteParams` (so `generateStaticParams`-covered params
// resolve in the static shell and only the uncovered ones are
// deferred). The dev prerender manifest isn't populated for these
// ad-hoc routes, but `getStaticPaths` already computed every
// prerendered route here, so we do the same match: among the routes
// whose canonical regex matches this URL, pick the one with the
// fewest fallback params (the most-specific) and thread it via the
// `fallbackParams` meta.
let perUrlFallbackRouteParams: NonNullable<
(typeof pathsResults.prerenderedRoutes)[number]['fallbackRouteParams']
> | null = null
for (const route of pathsResults.prerenderedRoutes) {
const fallbackRouteParams = route.fallbackRouteParams
const { fallbackRouteParams } = route
if (!fallbackRouteParams || fallbackRouteParams.length === 0) {
// There are no fallback route params so we don't need to continue
smallestFallbackRouteParams = null
break
continue
}
if (!getRouteRegex(route.pathname).re.test(urlPathname)) {
continue
}
if (
smallestFallbackRouteParams === null ||
fallbackRouteParams.length < smallestFallbackRouteParams.length
perUrlFallbackRouteParams === null ||
fallbackRouteParams.length < perUrlFallbackRouteParams.length
) {
smallestFallbackRouteParams = fallbackRouteParams
perUrlFallbackRouteParams = fallbackRouteParams
}
}
if (smallestFallbackRouteParams) {
if (perUrlFallbackRouteParams) {
addRequestMeta(
req,
'fallbackParams',
createOpaqueFallbackRouteParams(smallestFallbackRouteParams)!
createOpaqueFallbackRouteParams(perUrlFallbackRouteParams)!
)
}
}
......
......@@ -368,6 +368,49 @@ describe.each([
}
})
describe('mixed static and fallback params resolve in the correct phase', () => {
it('covered leading param', async () => {
const path = '/mixed/en/123'
const assertLogs = async (browser: Playwright) => {
const logs = await browser.log()
// `en` is covered by `generateStaticParams`, so `lang` resolves in
// the static shell.
assertLog(logs, 'after params - lang', 'Prerender')
// `id` is never covered, so it's deferred to the runtime stage.
assertLog(logs, 'after params - id', RUNTIME_ENV)
}
if (isInitialLoad) {
await testInitialLoad(path, assertLogs)
} else {
await testNavigation(path, assertLogs)
}
})
it('uncovered leading param', async () => {
const path = '/mixed/fr/123'
const assertLogs = async (browser: Playwright) => {
const logs = await browser.log()
// `fr` is not covered by `generateStaticParams`, so the most-specific
// prerendered route matching this URL is the base route and its
// fallback set is both params. `lang` must therefore defer to the
// runtime stage too, not resolve in the static shell. (Picking the
// fewest-param route without matching the URL would resolve `lang`
// in `Prerender` here.)
assertLog(logs, 'after params - lang', RUNTIME_ENV)
assertLog(logs, 'after params - id', RUNTIME_ENV)
}
if (isInitialLoad) {
await testInitialLoad(path, assertLogs)
} else {
await testNavigation(path, assertLogs)
}
})
})
// FIXME: it seems like in Turbopack we sometimes get two instances of `workUnitAsyncStorage` --
// `app-render` gets a second, newer instance, different from `io()`.
// Thus, `io()` gets an undefined `workUnitStore` and does nothing, so sync IO does not get tracked at all.
......
import { Suspense } from 'react'
// `id` is never covered by `generateStaticParams`, so it is always deferred to
// the runtime stage. The parent `[lang]` layout reads `lang`; this page reads
// only `id`.
export default function MixedIdPage({
params,
}: {
params: Promise<{ lang: string; id: string }>
}) {
return (
<main>
<Suspense fallback={<p>Waiting for id...</p>}>
<IdLabel params={params} />
</Suspense>
</main>
)
}
async function IdLabel({
params,
}: {
params: Promise<{ lang: string; id: string }>
}) {
const { id } = await params
console.log('after params - id')
return <p>id: {id}</p>
}
import { ReactNode, Suspense } from 'react'
export const instant = true
export const prefetch = 'allow-runtime'
// `lang` is covered by `generateStaticParams`. At this segment `params` only
// contains `lang`, so reading it here (rather than in the deeper page) lets us
// observe the stage `lang` resolves in independently of the uncovered `id`.
export function generateStaticParams() {
return [{ lang: 'en' }]
}
export default function MixedLangLayout({
children,
params,
}: {
children: ReactNode
params: Promise<{ lang: string }>
}) {
return (
<>
<Suspense fallback={<p>Waiting for lang...</p>}>
<LangLabel params={params} />
</Suspense>
{children}
</>
)
}
async function LangLabel({ params }: { params: Promise<{ lang: string }> }) {
const { lang } = await params
console.log('after params - lang')
return <p>lang: {lang}</p>
}
......@@ -23,6 +23,12 @@ export default function Page() {
<li>
<Link href="/apis/123">/apis/123</Link>
</li>
<li>
<Link href="/mixed/en/123">/mixed/en/123</Link>
</li>
<li>
<Link href="/mixed/fr/123">/mixed/fr/123</Link>
</li>
<li>
<Link href="/sync-io/static">/sync-io/static</Link>
</li>
......
import { Suspense } from 'react'
// `id` is never covered by `generateStaticParams`, so it is always deferred to
// the runtime stage. The parent `[lang]` layout reads `lang`; this page reads
// only `id`.
export default function MixedIdPage({
params,
}: {
params: Promise<{ lang: string; id: string }>
}) {
return (
<main>
<Suspense fallback={<p>Waiting for id...</p>}>
<IdLabel params={params} />
</Suspense>
</main>
)
}
async function IdLabel({
params,
}: {
params: Promise<{ lang: string; id: string }>
}) {
const { id } = await params
console.log('after params - id')
return <p>id: {id}</p>
}
import { ReactNode, Suspense } from 'react'
// `lang` is covered by `generateStaticParams`. At this segment `params` only
// contains `lang`, so reading it here (rather than in the deeper page) lets us
// observe the stage `lang` resolves in independently of the uncovered `id`.
export function generateStaticParams() {
return [{ lang: 'en' }]
}
export default function MixedLangLayout({
children,
params,
}: {
children: ReactNode
params: Promise<{ lang: string }>
}) {
return (
<>
<Suspense fallback={<p>Waiting for lang...</p>}>
<LangLabel params={params} />
</Suspense>
{children}
</>
)
}
async function LangLabel({ params }: { params: Promise<{ lang: string }> }) {
const { lang } = await params
console.log('after params - lang')
return <p>lang: {lang}</p>
}
......@@ -23,6 +23,12 @@ export default function Page() {
<li>
<Link href="/apis/123">/apis/123</Link>
</li>
<li>
<Link href="/mixed/en/123">/mixed/en/123</Link>
</li>
<li>
<Link href="/mixed/fr/123">/mixed/fr/123</Link>
</li>
<li>
<Link href="/sync-io/static">/sync-io/static</Link>
</li>
......
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