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( ...@@ -938,19 +938,29 @@ export async function handler(
: normalizedSrcPage : normalizedSrcPage
const fallbackRouteParams = const fallbackRouteParams =
// If we're in production and we have fallback route params, always // In production or when debugging the static shell (e.g. instant
// use them for the fallback shell. // navigation testing), use the prerender manifest's fallback
isProduction && prerenderInfo?.fallbackRouteParams // 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( ? createOpaqueFallbackRouteParams(
prerenderInfo.fallbackRouteParams prerenderInfo.fallbackRouteParams
) )
: // Otherwise, if we're debugging the fallback shell or the : // When debugging the fallback shell, treat all params as
// static shell, then we have to manually generate the // fallback (simulating the worst-case shell).
// fallback route params. isDebugFallbackShell
isDebugFallbackShell || isDebugStaticShell
? getFallbackRouteParams(normalizedSrcPage, routeModule) ? getFallbackRouteParams(normalizedSrcPage, routeModule)
: null : 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 // We use the response cache here to handle the revalidation and
// management of the fallback shell. // management of the fallback shell.
const fallbackResponse = await routeModule.handleResponse({ const fallbackResponse = await routeModule.handleResponse({
...@@ -1154,27 +1164,24 @@ export async function handler( ...@@ -1154,27 +1164,24 @@ export async function handler(
} }
const fallbackRouteParams = const fallbackRouteParams =
// If we're in production and we have fallback route params, then we // In production or when debugging the static shell for a
// can use the manifest fallback route params if we need to render the // non-prerendered URL, use the prerender manifest's fallback route
// fallback shell. // params which correctly identifies which params are unknown.
isProduction && ((isProduction && getRequestMeta(req, 'renderFallbackShell')) ||
prerenderInfo?.fallbackRouteParams && (isDebugStaticShell && !isPrerendered)) &&
getRequestMeta(req, 'renderFallbackShell') prerenderInfo?.fallbackRouteParams
? createOpaqueFallbackRouteParams(prerenderInfo.fallbackRouteParams) ? createOpaqueFallbackRouteParams(prerenderInfo.fallbackRouteParams)
: // Otherwise, if we're debugging the fallback shell or the static : isDebugFallbackShell
// shell, then we have to manually generate the fallback route
// params.
isDebugFallbackShell || isDebugStaticShell
? getFallbackRouteParams(normalizedSrcPage, routeModule) ? getFallbackRouteParams(normalizedSrcPage, routeModule)
: null : null
// For staged dynamic rendering (cached navigations), pass the fallback // For staged dynamic rendering (Cached Navigations) and debug static
// params via request meta so the RequestStore knows which params to defer // shell rendering, pass the fallback params via request meta so the
// to the runtime stage. We don't pass them as fallbackRouteParams because // RequestStore knows which params to defer. We don't pass them as
// that would replace actual param values with opaque placeholders during // fallbackRouteParams because that would replace actual param values
// segment resolution. // with opaque placeholders during segment resolution.
if ( if (
isProduction && (isProduction || isDebugStaticShell) &&
nextConfig.cacheComponents && nextConfig.cacheComponents &&
!isPrerendered && !isPrerendered &&
prerenderInfo?.fallbackRouteParams prerenderInfo?.fallbackRouteParams
......
...@@ -909,6 +909,14 @@ export default class DevServer extends Server { ...@@ -909,6 +909,14 @@ export default class DevServer extends Server {
existingManifest.routes[staticPath] = {} as any 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] = { existingManifest.dynamicRoutes[pathname] = {
dataRoute: null, dataRoute: null,
dataRouteRegex: null, dataRouteRegex: null,
...@@ -917,8 +925,8 @@ export default class DevServer extends Server { ...@@ -917,8 +925,8 @@ export default class DevServer extends Server {
fallbackExpire: undefined, fallbackExpire: undefined,
fallbackHeaders: undefined, fallbackHeaders: undefined,
fallbackStatus: undefined, fallbackStatus: undefined,
fallbackRootParams: undefined, fallbackRootParams: fallbackPrerenderedRoute?.fallbackRootParams,
fallbackRouteParams: undefined, fallbackRouteParams: fallbackPrerenderedRoute?.fallbackRouteParams,
fallbackSourceRoute: pathname, fallbackSourceRoute: pathname,
prefetchDataRoute: undefined, prefetchDataRoute: undefined,
prefetchDataRouteRegex: undefined, prefetchDataRouteRegex: undefined,
......
import { Suspense } from 'react' import { Suspense } from 'react'
export function generateStaticParams() {
return [{ slug: 'hello' }]
}
export default function DynamicParamsPage({ export default function DynamicParamsPage({
params, params,
}: { }: {
......
...@@ -23,9 +23,12 @@ export default function HomePage() { ...@@ -23,9 +23,12 @@ export default function HomePage() {
<Link href="/cookies-page" id="link-to-cookies-page"> <Link href="/cookies-page" id="link-to-cookies-page">
Go to cookies page Go to cookies page
</Link> </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 Go to dynamic params page
</Link> </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"> <Link href="/search-params-page?foo=bar" id="link-to-search-params">
Go to search params page Go to search params page
</Link> </Link>
...@@ -36,9 +39,12 @@ export default function HomePage() { ...@@ -36,9 +39,12 @@ export default function HomePage() {
<a href="/cookies-page" id="plain-link-to-cookies-page"> <a href="/cookies-page" id="plain-link-to-cookies-page">
Go to cookies page (MPA) Go to cookies page (MPA)
</a> </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) Go to dynamic params page (MPA)
</a> </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"> <a href="/search-params-page?foo=bar" id="plain-link-to-search-params">
Go to search params page (MPA) Go to search params page (MPA)
</a> </a>
......
...@@ -6,6 +6,7 @@ const nextConfig = { ...@@ -6,6 +6,7 @@ const nextConfig = {
experimental: { experimental: {
// Enable the testing API in production builds for these tests // Enable the testing API in production builds for these tests
exposeTestingApiInProductionBuild: true, 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