Unverified Commit f765823e authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

prefetchInlining: fix build crash on notFound() pages (#92806)

When traversing the route tree in collectPrefetchHints, we use keys from
the FlightRouterState to look up corresponding entries in the seed data
object. Usually these two structures agree, but there are subtle cases
like notFound() where the router state contains keys that don't exist in
the seed data. TypeScript doesn't catch this because the `in` operator
narrows the key to `string`, which is assignable to the seed data
object's index signature — but the lookup returns `undefined` rather
than `null`.

Eventually the plan is to converge these into a single data structure so
this kind of mismatch can't happen structurally. For now, coalesce
undefined to null with `?? null`, matching the pattern already used in
ppr-navigations.ts and segment-cache/cache.ts on the client.

## Test plan

- Added regression test: page with `notFound()` + `generateStaticParams`
under `prefetchInlining`
- Verified the build crashes without the fix and passes with it
- All existing prefetch-inlining tests continue to pass

<!-- NEXT_JS_LLM_PR -->
parent 3bbbb32f
...@@ -487,7 +487,9 @@ async function collectPrefetchHintsImpl( ...@@ -487,7 +487,9 @@ async function collectPrefetchHintsImpl(
const childRoute = children[parallelRouteKey] const childRoute = children[parallelRouteKey]
const childSegment = childRoute[0] const childSegment = childRoute[0]
const childSeedData = const childSeedData =
seedDataChildren !== null ? seedDataChildren[parallelRouteKey] : null seedDataChildren !== null
? (seedDataChildren[parallelRouteKey] ?? null)
: null
const childRequestKey = appendSegmentRequestKeyPart( const childRequestKey = appendSegmentRequestKeyPart(
requestKey, requestKey,
...@@ -870,7 +872,9 @@ function collectSegmentDataImpl( ...@@ -870,7 +872,9 @@ function collectSegmentDataImpl(
const childRoute = children[parallelRouteKey] const childRoute = children[parallelRouteKey]
const childSegment = childRoute[0] const childSegment = childRoute[0]
const childSeedData = const childSeedData =
seedDataChildren !== null ? seedDataChildren[parallelRouteKey] : null seedDataChildren !== null
? (seedDataChildren[parallelRouteKey] ?? null)
: null
const childRequestKey = appendSegmentRequestKeyPart( const childRequestKey = appendSegmentRequestKeyPart(
requestKey, requestKey,
......
...@@ -65,6 +65,9 @@ export default function Page() { ...@@ -65,6 +65,9 @@ export default function Page() {
Independent head B Independent head B
</LinkAccordion> </LinkAccordion>
</li> </li>
<li>
<LinkAccordion href="/test-not-found/exists">Not found</LinkAccordion>
</li>
</ul> </ul>
</div> </div>
) )
......
import { notFound } from 'next/navigation'
// Some slugs trigger notFound() during prerendering. This produces a
// different RSC payload shape. collectPrefetchHints must handle this
// without crashing the build.
export default async function NotFoundPage({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
if (slug === 'missing') {
notFound()
}
return <p id="page-not-found">Found: {slug}</p>
}
export function generateStaticParams() {
return [{ slug: 'exists' }, { slug: 'missing' }]
}
...@@ -805,4 +805,15 @@ describe('prefetch inlining', () => { ...@@ -805,4 +805,15 @@ describe('prefetch inlining', () => {
'Independent head page' 'Independent head page'
) )
}) })
it('notFound() during prerender does not crash build', async () => {
// Regression test: a page that calls notFound() during prerendering
// produces a flight data tree where some child seed data entries are
// undefined. collectPrefetchHints must handle this without crashing.
// The build succeeding is the primary assertion.
const browser = await next.browser('/test-not-found/exists')
expect(await browser.elementByCss('#page-not-found').text()).toBe(
'Found: exists'
)
})
}) })
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