Unverified Commit 834c41ef authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Respect `generateStaticParams` in instant navigation shell (#91316)

When the instant navigation testing cookie is set, the debug static
shell path previously called `getFallbackRouteParams()` which treats all
dynamic segments as fallback params regardless of
`generateStaticParams`. This caused two issues:

- Root params (via `next/root-params`) errored with a
`NEXT_STATIC_GEN_BAILOUT` 500 response because the root layout's param
access returned a hanging promise, which was treated as uncached data
accessed outside of `<Suspense>`.
- Params defined in `generateStaticParams` were incorrectly excluded
from the instant shell, shown behind Suspense fallbacks instead of
resolving.

The fix uses `prerenderInfo?.fallbackRouteParams` from the prerender
manifest instead, which correctly distinguishes between statically known
params (defined in `generateStaticParams`) and unknown params. For
prerendered URLs, the outer guard (`!isPrerendered`) prevents the
fallback path from being entered, so no fallback params are set and all
params resolve normally.

To make this work in dev mode, the dev server now populates
`fallbackRouteParams` in the prerender manifest's dynamic route entries,
which were previously left as `undefined`.

Additionally, the `fallbackParams` request metadata is now overridden
when rendering a debug static shell. Without this override, the staged
rendering would use the smallest set of fallback params across all
prerendered routes (set by `base-server.ts` for dev validation), which
may omit fallback params for the current URL when a different value for
the same param is defined in `generateStaticParams`. The override
ensures the route-specific fallback params are used instead.
parent c1f13e48
......@@ -938,19 +938,29 @@ export async function handler(
: normalizedSrcPage
const fallbackRouteParams =
// If we're in production and we have fallback route params, always
// use them for the fallback shell.
isProduction && prerenderInfo?.fallbackRouteParams
// In production or when debugging the static shell (e.g. instant
// navigation testing), use the prerender manifest's fallback
// route params which correctly identifies which params are
// unknown. Note: in dev, this block is only entered for
// non-prerendered URLs (guarded by the outer condition).
(isProduction || isDebugStaticShell) &&
prerenderInfo?.fallbackRouteParams
? createOpaqueFallbackRouteParams(
prerenderInfo.fallbackRouteParams
)
: // Otherwise, if we're debugging the fallback shell or the
// static shell, then we have to manually generate the
// fallback route params.
isDebugFallbackShell || isDebugStaticShell
: // When debugging the fallback shell, treat all params as
// fallback (simulating the worst-case shell).
isDebugFallbackShell
? getFallbackRouteParams(normalizedSrcPage, routeModule)
: null
// When rendering a debug static shell, override the fallback
// params on the request so that the staged rendering correctly
// defers params that are not statically known.
if (isDebugStaticShell && fallbackRouteParams) {
addRequestMeta(req, 'fallbackParams', fallbackRouteParams)
}
// We use the response cache here to handle the revalidation and
// management of the fallback shell.
const fallbackResponse = await routeModule.handleResponse({
......@@ -1154,27 +1164,24 @@ export async function handler(
}
const fallbackRouteParams =
// If we're in production and we have fallback route params, then we
// can use the manifest fallback route params if we need to render the
// fallback shell.
isProduction &&
prerenderInfo?.fallbackRouteParams &&
getRequestMeta(req, 'renderFallbackShell')
// In production or when debugging the static shell for a
// non-prerendered URL, use the prerender manifest's fallback route
// params which correctly identifies which params are unknown.
((isProduction && getRequestMeta(req, 'renderFallbackShell')) ||
(isDebugStaticShell && !isPrerendered)) &&
prerenderInfo?.fallbackRouteParams
? createOpaqueFallbackRouteParams(prerenderInfo.fallbackRouteParams)
: // Otherwise, if we're debugging the fallback shell or the static
// shell, then we have to manually generate the fallback route
// params.
isDebugFallbackShell || isDebugStaticShell
: isDebugFallbackShell
? getFallbackRouteParams(normalizedSrcPage, routeModule)
: null
// For staged dynamic rendering (cached navigations), pass the fallback
// params via request meta so the RequestStore knows which params to defer
// to the runtime stage. We don't pass them as fallbackRouteParams because
// that would replace actual param values with opaque placeholders during
// segment resolution.
// For staged dynamic rendering (Cached Navigations) and debug static
// shell rendering, pass the fallback params via request meta so the
// RequestStore knows which params to defer. We don't pass them as
// fallbackRouteParams because that would replace actual param values
// with opaque placeholders during segment resolution.
if (
isProduction &&
(isProduction || isDebugStaticShell) &&
nextConfig.cacheComponents &&
!isPrerendered &&
prerenderInfo?.fallbackRouteParams
......
......@@ -909,6 +909,14 @@ export default class DevServer extends Server {
existingManifest.routes[staticPath] = {} as any
}
// Find the fallback route from the prerendered routes. This is
// the route whose pathname matches the page pattern (e.g.
// /dynamic-params/[slug]) and has fallback route params describing
// which params are unknown at build time.
const fallbackPrerenderedRoute = prerenderedRoutes?.find(
(route) => route.pathname === pathname
)
existingManifest.dynamicRoutes[pathname] = {
dataRoute: null,
dataRouteRegex: null,
......@@ -917,8 +925,8 @@ export default class DevServer extends Server {
fallbackExpire: undefined,
fallbackHeaders: undefined,
fallbackStatus: undefined,
fallbackRootParams: undefined,
fallbackRouteParams: undefined,
fallbackRootParams: fallbackPrerenderedRoute?.fallbackRootParams,
fallbackRouteParams: fallbackPrerenderedRoute?.fallbackRouteParams,
fallbackSourceRoute: pathname,
prefetchDataRoute: undefined,
prefetchDataRouteRegex: undefined,
......
import { Suspense } from 'react'
export function generateStaticParams() {
return [{ slug: 'hello' }]
}
export default function DynamicParamsPage({
params,
}: {
......
......@@ -23,9 +23,12 @@ export default function HomePage() {
<Link href="/cookies-page" id="link-to-cookies-page">
Go to cookies page
</Link>
<Link href="/dynamic-params/hello" id="link-to-dynamic-params">
<Link href="/dynamic-params/unknown" id="link-to-dynamic-params">
Go to dynamic params page
</Link>
<Link href="/dynamic-params/hello" id="link-to-static-dynamic-params">
Go to static dynamic params page
</Link>
<Link href="/search-params-page?foo=bar" id="link-to-search-params">
Go to search params page
</Link>
......@@ -36,9 +39,12 @@ export default function HomePage() {
<a href="/cookies-page" id="plain-link-to-cookies-page">
Go to cookies page (MPA)
</a>
<a href="/dynamic-params/hello" id="plain-link-to-dynamic-params">
<a href="/dynamic-params/unknown" id="plain-link-to-dynamic-params">
Go to dynamic params page (MPA)
</a>
<a href="/dynamic-params/hello" id="plain-link-to-static-dynamic-params">
Go to static dynamic params page (MPA)
</a>
<a href="/search-params-page?foo=bar" id="plain-link-to-search-params">
Go to search params page (MPA)
</a>
......
......@@ -6,6 +6,7 @@ const nextConfig = {
experimental: {
// Enable the testing API in production builds for these tests
exposeTestingApiInProductionBuild: true,
instantNavigationDevToolsToggle: true,
},
}
......
import { lang } from 'next/root-params'
import { ReactNode } from 'react'
export async function generateStaticParams() {
return [{ lang: 'en' }]
}
export default async function RootLayout({
children,
}: {
children: ReactNode
}) {
const currentLang = await lang()
return (
<html lang={currentLang}>
<body>
<p data-testid="lang-value">lang: {currentLang}</p>
{children}
</body>
</html>
)
}
export default function HomePage() {
return (
<div>
<h1 data-testid="home-title">Root Params Test</h1>
</div>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
experimental: {
exposeTestingApiInProductionBuild: true,
instantNavigationDevToolsToggle: true,
},
}
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