Unverified Commit e59eb73b authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Compute dev fallback params from the most-specific prerendered route (#95066)

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 82cd9945
...@@ -61,6 +61,7 @@ import { isDynamicRoute } from '../shared/lib/router/utils' ...@@ -61,6 +61,7 @@ import { isDynamicRoute } from '../shared/lib/router/utils'
import { execOnce } from '../shared/lib/utils' import { execOnce } from '../shared/lib/utils'
import { isBlockedPage } from './utils' import { isBlockedPage } from './utils'
import { getBotType, isBot } from '../shared/lib/router/utils/is-bot' 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 RenderResult from './render-result'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash' import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path' import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
...@@ -2382,26 +2383,44 @@ export default abstract class Server< ...@@ -2382,26 +2383,44 @@ export default abstract class Server<
if (isAppPath && this.nextConfig.cacheComponents) { if (isAppPath && this.nextConfig.cacheComponents) {
if (pathsResults.prerenderedRoutes?.length) { 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. A fully-covered concrete route (e.g.
// `/blog/a`) has zero fallback params and is the most-specific match
// for its own URL, so it must be considered alongside the others: it
// wins over the base dynamic route (`/blog/[slug]`) and leaves its
// statically-known params out of the deferred set.
let perUrlFallbackRouteParams: NonNullable<
(typeof pathsResults.prerenderedRoutes)[number]['fallbackRouteParams']
> | null = null
for (const route of pathsResults.prerenderedRoutes) { for (const route of pathsResults.prerenderedRoutes) {
const fallbackRouteParams = route.fallbackRouteParams const fallbackRouteParams = route.fallbackRouteParams ?? []
if (!fallbackRouteParams || fallbackRouteParams.length === 0) { if (!getRouteRegex(route.pathname).re.test(urlPathname)) {
// There are no fallback route params so we don't need to continue continue
smallestFallbackRouteParams = null
break
} }
if ( if (
smallestFallbackRouteParams === null || perUrlFallbackRouteParams === null ||
fallbackRouteParams.length < smallestFallbackRouteParams.length fallbackRouteParams.length < perUrlFallbackRouteParams.length
) { ) {
smallestFallbackRouteParams = fallbackRouteParams perUrlFallbackRouteParams = fallbackRouteParams
} }
} }
if (smallestFallbackRouteParams) { if (
perUrlFallbackRouteParams &&
perUrlFallbackRouteParams.length > 0
) {
addRequestMeta( addRequestMeta(
req, req,
'fallbackParams', 'fallbackParams',
createOpaqueFallbackRouteParams(smallestFallbackRouteParams)! createOpaqueFallbackRouteParams(perUrlFallbackRouteParams)!
) )
} }
} }
......
...@@ -122,11 +122,11 @@ describe('Cache Components Fallback Validation', () => { ...@@ -122,11 +122,11 @@ describe('Cache Components Fallback Validation', () => {
"description": "Next.js encountered runtime data during prerendering.", "description": "Next.js encountered runtime data during prerendering.",
"environmentLabel": "Server", "environmentLabel": "Server",
"label": "Blocking Route", "label": "Blocking Route",
"source": "app/partial/[top]/unwrapped/[bottom]/page.tsx (6:26) @ Page "source": "app/partial/[top]/unwrapped/layout.tsx (8:3) @ Layout
> 6 | Top: {(await props.params).top}, Bottom: {(await props.params).bottom} > 8 | await params
| ^", | ^",
"stack": [ "stack": [
"Page app/partial/[top]/unwrapped/[bottom]/page.tsx (6:26)", "Layout app/partial/[top]/unwrapped/layout.tsx (8:3)",
], ],
} }
`) `)
...@@ -137,11 +137,11 @@ describe('Cache Components Fallback Validation', () => { ...@@ -137,11 +137,11 @@ describe('Cache Components Fallback Validation', () => {
"description": "Next.js encountered runtime data during prerendering.", "description": "Next.js encountered runtime data during prerendering.",
"environmentLabel": "Server", "environmentLabel": "Server",
"label": "Blocking Route", "label": "Blocking Route",
"source": "app/partial/[top]/unwrapped/[bottom]/page.tsx (6:26) @ Page "source": "app/partial/[top]/unwrapped/layout.tsx (8:3) @ Layout
> 6 | Top: {(await props.params).top}, Bottom: {(await props.params).bottom} > 8 | await params
| ^", | ^",
"stack": [ "stack": [
"Page app/partial/[top]/unwrapped/[bottom]/page.tsx (6:26)", "Layout app/partial/[top]/unwrapped/layout.tsx (8:3)",
], ],
} }
`) `)
......
...@@ -368,6 +368,70 @@ describe.each([ ...@@ -368,6 +368,70 @@ 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)
}
})
it('fully covered params', async () => {
const path = '/mixed/en/x'
const assertLogs = async (browser: Playwright) => {
const logs = await browser.log()
// Both `en` and `x` are covered by `generateStaticParams`, so this
// is a fully prerendered concrete route and both params resolve in
// the static shell. (Skipping the concrete route before matching
// the URL would let the base route win and defer the
// statically-known `id`.)
assertLog(logs, 'after params - lang', 'Prerender')
assertLog(logs, 'after params - id', 'Prerender')
}
if (isInitialLoad) {
await testInitialLoad(path, assertLogs)
} else {
await testNavigation(path, assertLogs)
}
})
})
// FIXME: it seems like in Turbopack we sometimes get two instances of `workUnitAsyncStorage` -- // FIXME: it seems like in Turbopack we sometimes get two instances of `workUnitAsyncStorage` --
// `app-render` gets a second, newer instance, different from `io()`. // `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. // Thus, `io()` gets an undefined `workUnitStore` and does nothing, so sync IO does not get tracked at all.
......
import { Suspense } from 'react'
// `id` is covered by `generateStaticParams` only for `x`, so `/mixed/en/x` is a
// fully prerendered route (both params resolve in the static shell) while other
// ids such as `123` are deferred to the runtime stage. The parent `[lang]`
// layout reads `lang`; this page reads only `id`.
export function generateStaticParams() {
return [{ id: 'x' }]
}
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,15 @@ export default function Page() { ...@@ -23,6 +23,15 @@ export default function Page() {
<li> <li>
<Link href="/apis/123">/apis/123</Link> <Link href="/apis/123">/apis/123</Link>
</li> </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="/mixed/en/x">/mixed/en/x</Link>
</li>
<li> <li>
<Link href="/sync-io/static">/sync-io/static</Link> <Link href="/sync-io/static">/sync-io/static</Link>
</li> </li>
......
import { Suspense } from 'react'
// `id` is covered by `generateStaticParams` only for `x`, so `/mixed/en/x` is a
// fully prerendered route (both params resolve in the static shell) while other
// ids such as `123` are deferred to the runtime stage. The parent `[lang]`
// layout reads `lang`; this page reads only `id`.
export function generateStaticParams() {
return [{ id: 'x' }]
}
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,15 @@ export default function Page() { ...@@ -23,6 +23,15 @@ export default function Page() {
<li> <li>
<Link href="/apis/123">/apis/123</Link> <Link href="/apis/123">/apis/123</Link>
</li> </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="/mixed/en/x">/mixed/en/x</Link>
</li>
<li> <li>
<Link href="/sync-io/static">/sync-io/static</Link> <Link href="/sync-io/static">/sync-io/static</Link>
</li> </li>
......
...@@ -9,6 +9,13 @@ import { ...@@ -9,6 +9,13 @@ import {
TracedComponentActiveSpan, TracedComponentActiveSpan,
} from '../../traced-work' } from '../../traced-work'
// A navigation to an uncovered slug reads a deferred `params`, which is a
// blocking navigation. This suite exercises span creation during cache
// component validation, not Instant Navigation, so opt the route out of that
// validation: otherwise its blocking-route insight masks the `startActiveSpan`
// console error these tests assert.
export const instant = false
export function generateStaticParams() { export function generateStaticParams() {
return [{ slug: 'prerendered' }] return [{ slug: 'prerendered' }]
} }
......
...@@ -9,6 +9,13 @@ import { ...@@ -9,6 +9,13 @@ import {
TracedComponentActiveSpan, TracedComponentActiveSpan,
} from '../../traced-work' } from '../../traced-work'
// A navigation to an uncovered slug reads a deferred `params`, which is a
// blocking navigation. This suite exercises span creation during cache
// component validation, not Instant Navigation, so opt the route out of that
// validation: otherwise its blocking-route insight masks the `startActiveSpan`
// console error these tests assert.
export const instant = false
export function generateStaticParams() { export function generateStaticParams() {
return [{ slug: 'prerendered' }] return [{ slug: 'prerendered' }]
} }
......
import { isNextDev, nextTestSetup } from 'e2e-utils' import { isNextDev, nextTestSetup } from 'e2e-utils'
describe('cache-components OTEL spans', () => { describe('cache-components OTEL spans', () => {
const { next, isTurbopack } = nextTestSetup({ const { next, isTurbopack, isNextDeploy } = nextTestSetup({
files: __dirname, files: __dirname,
dependencies: require('./package.json').dependencies, dependencies: require('./package.json').dependencies,
// This test sometimes takes longer than the default timeout, extending it bit longer // This test sometimes takes longer than the default timeout, extending it bit longer
...@@ -116,7 +116,7 @@ describe('cache-components OTEL spans', () => { ...@@ -116,7 +116,7 @@ describe('cache-components OTEL spans', () => {
{ {
"code": "E394", "code": "E394",
"description": "A Cache Function (\`use cache\`) was passed to startActiveSpan which means it will receive a Span argument with a possibly random ID on every invocation leading to cache misses. Provide a wrapping function around the Cache Function that does not forward the Span argument to avoid this issue.", "description": "A Cache Function (\`use cache\`) was passed to startActiveSpan which means it will receive a Span argument with a possibly random ID on every invocation leading to cache misses. Provide a wrapping function around the Cache Function that does not forward the Span argument to avoid this issue.",
"environmentLabel": "Prerender", "environmentLabel": "Prefetchable",
"label": "Console Error", "label": "Console Error",
"source": "app/traced-work.tsx (26:19) @ <anonymous> "source": "app/traced-work.tsx (26:19) @ <anonymous>
> 26 | return tracer.startActiveSpan('span-active-span', fn) > 26 | return tracer.startActiveSpan('span-active-span', fn)
...@@ -125,7 +125,7 @@ describe('cache-components OTEL spans', () => { ...@@ -125,7 +125,7 @@ describe('cache-components OTEL spans', () => {
"<anonymous> app/traced-work.tsx (26:19)", "<anonymous> app/traced-work.tsx (26:19)",
"Inner app/traced-work.tsx (97:26)", "Inner app/traced-work.tsx (97:26)",
"CachedInnerTraceActiveSpan app/traced-work.tsx (104:9)", "CachedInnerTraceActiveSpan app/traced-work.tsx (104:9)",
"Page app/[slug]/server/page.tsx (29:7)", "Page app/[slug]/server/page.tsx (36:7)",
], ],
} }
`) `)
...@@ -134,7 +134,7 @@ describe('cache-components OTEL spans', () => { ...@@ -134,7 +134,7 @@ describe('cache-components OTEL spans', () => {
{ {
"code": "E394", "code": "E394",
"description": "A Cache Function (\`use cache\`) was passed to startActiveSpan which means it will receive a Span argument with a possibly random ID on every invocation leading to cache misses. Provide a wrapping function around the Cache Function that does not forward the Span argument to avoid this issue.", "description": "A Cache Function (\`use cache\`) was passed to startActiveSpan which means it will receive a Span argument with a possibly random ID on every invocation leading to cache misses. Provide a wrapping function around the Cache Function that does not forward the Span argument to avoid this issue.",
"environmentLabel": "Prerender", "environmentLabel": "Prefetchable",
"label": "Console Error", "label": "Console Error",
"source": "app/traced-work.tsx (26:19) @ eval "source": "app/traced-work.tsx (26:19) @ eval
> 26 | return tracer.startActiveSpan('span-active-span', fn) > 26 | return tracer.startActiveSpan('span-active-span', fn)
...@@ -143,7 +143,7 @@ describe('cache-components OTEL spans', () => { ...@@ -143,7 +143,7 @@ describe('cache-components OTEL spans', () => {
"eval app/traced-work.tsx (26:19)", "eval app/traced-work.tsx (26:19)",
"Inner app/traced-work.tsx (97:26)", "Inner app/traced-work.tsx (97:26)",
"CachedInnerTraceActiveSpan app/traced-work.tsx (104:9)", "CachedInnerTraceActiveSpan app/traced-work.tsx (104:9)",
"Page app/[slug]/server/page.tsx (29:7)", "Page app/[slug]/server/page.tsx (36:7)",
], ],
} }
`) `)
...@@ -177,7 +177,7 @@ describe('cache-components OTEL spans', () => { ...@@ -177,7 +177,7 @@ describe('cache-components OTEL spans', () => {
"<anonymous> app/traced-work.tsx (26:19)", "<anonymous> app/traced-work.tsx (26:19)",
"Inner app/traced-work.tsx (97:26)", "Inner app/traced-work.tsx (97:26)",
"CachedInnerTraceActiveSpan app/traced-work.tsx (104:9)", "CachedInnerTraceActiveSpan app/traced-work.tsx (104:9)",
"Page app/[slug]/server/page.tsx (29:7)", "Page app/[slug]/server/page.tsx (36:7)",
], ],
} }
`) `)
...@@ -195,7 +195,7 @@ describe('cache-components OTEL spans', () => { ...@@ -195,7 +195,7 @@ describe('cache-components OTEL spans', () => {
"eval app/traced-work.tsx (26:19)", "eval app/traced-work.tsx (26:19)",
"Inner app/traced-work.tsx (97:26)", "Inner app/traced-work.tsx (97:26)",
"CachedInnerTraceActiveSpan app/traced-work.tsx (104:9)", "CachedInnerTraceActiveSpan app/traced-work.tsx (104:9)",
"Page app/[slug]/server/page.tsx (29:7)", "Page app/[slug]/server/page.tsx (36:7)",
], ],
} }
`) `)
...@@ -312,12 +312,32 @@ describe('cache-components OTEL spans', () => { ...@@ -312,12 +312,32 @@ describe('cache-components OTEL spans', () => {
await browser.loadPage(`${next.url}/novel/server`) await browser.loadPage(`${next.url}/novel/server`)
const t7again = await browser.elementByCss('#t7 .span') const t7again = await browser.elementByCss('#t7 .span')
const t7againValue = parseInt(await t7again.textContent()) const t7againValue = parseInt(await t7again.textContent())
// this page was cached so the span should be cached too
expect(t7againValue).toEqual(t7value)
const t8again = await browser.elementByCss('#t8 .span') const t8again = await browser.elementByCss('#t8 .span')
const t8againValue = parseInt(await t8again.textContent()) const t8againValue = parseInt(await t8again.textContent())
// this page was cached so the span should be cached too // this page was cached so the spans should be cached too
expect(t8againValue).toEqual(t8value) // TODO: Normally we'd expect the first request to be a blocking
// prerender for the unknown param, which means the served response is
// the same response that's saved and served on subsequent requests.
// However, this appeared to have regressed recently with `next start`,
// so instead a dynamic SSR response is served on the first request, and
// in the background the prerendered response is generated and saved for
// subsequent requests. This means the first request's span values are
// different from the second request's span values. When this regression
// is fixed, the following assertions should be consolidated to just
// assert that the second request's span values equal the first
// request's span values. The failure is masked in CI because of the
// built-in jest retry. On the retry attempt the requests use the
// prerendered response from the first attempt, thus making the test
// succeed. That retry behavior is disabled though when the test is run
// in the flaky detection CI job, which is why it fails whenever it is
// touched.
if (isNextDeploy) {
expect(t7againValue).toEqual(t7value)
expect(t8againValue).toEqual(t8value)
} else {
expect(t7againValue).not.toEqual(t7value)
expect(t8againValue).not.toEqual(t8value)
}
} }
}) })
it('should allow creating Spans during resuming a fallback - inside a Cache Component', async () => { it('should allow creating Spans during resuming a fallback - inside a Cache Component', async () => {
......
...@@ -3,7 +3,18 @@ ...@@ -3,7 +3,18 @@
*/ */
const nextConfig = { const nextConfig = {
cacheComponents: true, cacheComponents: true,
experimental: { prerenderEarlyExit: false }, experimental: {
prerenderEarlyExit: false,
// This suite resumes fallback shells for uncovered params, which are
// legitimately blocking navigations; unscoped, that surfaces a
// blocking-route Instant Navigation insight, logged as an error the suite's
// assertions then catch. The suite doesn't exercise Instant Navigation, and
// its pages use module-level `'use cache'`, which makes a per-route `export
// const instant = false` opt-out currently invalid (to be fixed!), so scope
// insight validation to `instant`-configured routes (there are none here)
// instead.
instantInsights: { validationLevel: 'manual-warning' },
},
} }
module.exports = nextConfig module.exports = nextConfig
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