Unverified Commit 0f35dab6 authored by Josh Story's avatar Josh Story Committed by GitHub

Improve notFound recovery in error pathway (#94037)

Effectively

Relands #93988
Reverts #92231

Prerendering the notFound loaderTree turns out to not be the correct
behavior in many cases. The original motivation of 92231 as to solve the
connection closed problem caused by statically prerendering an
incomplete HTTP event like notFound() when cacheComponents is enabled
because it would leave holes in the inlined flight data that never
resolved and led to reported errors on hydration.

This change now continues to fork the CC behavior of the error handler
but it treats the error page as a resumable page so fresh hydration data
is generated if it wasn't able to be entirely static.

This is good for correctness but it also means the page is not going to
statically prerender the notFound UI. This will be handled in a followup

---------
Co-authored-by: 's avatarHendrik Liebau <mail@hendrik-liebau.de>
parent 04aa19df
...@@ -1278,5 +1278,11 @@ ...@@ -1278,5 +1278,11 @@
"1277": "no route matched for path \"%s\"", "1277": "no route matched for path \"%s\"",
"1278": "compileRoute: either routeSpecifier or path is required", "1278": "compileRoute: either routeSpecifier or path is required",
"1279": "\\`experimental.appShells\\` requires the following to also be enabled: %s. Please update your %s accordingly.", "1279": "\\`experimental.appShells\\` requires the following to also be enabled: %s. Please update your %s accordingly.",
"1280": "Invariant: expected static metadata route to have a prerender pathname (%s)" "1280": "Invariant: expected static metadata route to have a prerender pathname (%s)",
"1281": "Cache Components HTTP fallback recovery unexpectedly produced a DynamicServerError",
"1282": "Cache Components error recovery expected an original Flight prerender result",
"1283": "Cache Components error recovery expected to know whether the original Flight prerender result was dynamic",
"1284": "Cache Components error recovery expected an original prerender resume data cache",
"1285": "Cache Components error recovery expected an original prerender store",
"1286": "Cache Components error recovery unexpectedly produced a dynamic HTML hole"
} }
...@@ -44,13 +44,6 @@ import { ...@@ -44,13 +44,6 @@ import {
import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config' import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config'
import { RenderStage, type StagedRenderingController } from './staged-rendering' import { RenderStage, type StagedRenderingController } from './staged-rendering'
type HTTPAccessErrorStatusCode = 404 | 403 | 401
export type PrerenderHTTPErrorState = {
boundaryTree: LoaderTree
triggeredStatus: HTTPAccessErrorStatusCode
}
/** /**
* Use the provided loader tree to create the React Component tree. * Use the provided loader tree to create the React Component tree.
*/ */
...@@ -69,7 +62,6 @@ export function createComponentTree(props: { ...@@ -69,7 +62,6 @@ export function createComponentTree(props: {
preloadCallbacks: PreloadCallbacks preloadCallbacks: PreloadCallbacks
authInterrupts: boolean authInterrupts: boolean
MetadataOutlet: ComponentType MetadataOutlet: ComponentType
prerenderHTTPError?: PrerenderHTTPErrorState
}): Promise<CacheNodeSeedData> { }): Promise<CacheNodeSeedData> {
return getTracer().trace( return getTracer().trace(
NextNodeServerSpan.createComponentTree, NextNodeServerSpan.createComponentTree,
...@@ -107,7 +99,6 @@ async function createComponentTreeInternal( ...@@ -107,7 +99,6 @@ async function createComponentTreeInternal(
preloadCallbacks, preloadCallbacks,
authInterrupts, authInterrupts,
MetadataOutlet, MetadataOutlet,
prerenderHTTPError,
}: { }: {
loaderTree: LoaderTree loaderTree: LoaderTree
parentParams: Params parentParams: Params
...@@ -122,7 +113,6 @@ async function createComponentTreeInternal( ...@@ -122,7 +113,6 @@ async function createComponentTreeInternal(
preloadCallbacks: PreloadCallbacks preloadCallbacks: PreloadCallbacks
authInterrupts: boolean authInterrupts: boolean
MetadataOutlet: ComponentType | null MetadataOutlet: ComponentType | null
prerenderHTTPError?: PrerenderHTTPErrorState
}, },
isRoot: boolean isRoot: boolean
): Promise<CacheNodeSeedData> { ): Promise<CacheNodeSeedData> {
...@@ -602,46 +592,6 @@ async function createComponentTreeInternal( ...@@ -602,46 +592,6 @@ async function createComponentTreeInternal(
} }
} }
// The outer prerender catch already found the deepest segment whose
// HTTP fallback should replace the throwing page. When we reach that
// segment's `children` slot, render the fallback directly instead of
// descending back into the subtree that threw during deserialization.
// Like the other segment-level boundary props below, HTTP access
// fallbacks are attached to the default `children` slot, not to named
// parallel routes.
const shouldRenderPrerenderHTTPFallback =
prerenderHTTPError?.boundaryTree === tree && isChildrenRouteKey
if (shouldRenderPrerenderHTTPFallback) {
let fallbackElement: React.ReactNode | undefined
switch (prerenderHTTPError.triggeredStatus) {
case 404:
fallbackElement = notFoundElement
break
case 403:
fallbackElement = forbiddenElement
break
case 401:
fallbackElement = unauthorizedElement
break
default:
break
}
if (fallbackElement) {
childCacheNodeSeedData = createSeedData(
ctx,
fallbackElement,
{},
null,
isPossiblyPartialResponse,
false,
emptyVaryParamsAccumulator
)
}
}
if (childCacheNodeSeedData === null) { if (childCacheNodeSeedData === null) {
const seedData = await createComponentTreeInternal( const seedData = await createComponentTreeInternal(
{ {
...@@ -661,7 +611,6 @@ async function createComponentTreeInternal( ...@@ -661,7 +611,6 @@ async function createComponentTreeInternal(
// `StreamingMetadataOutlet` is used to conditionally throw. In the case of parallel routes we will have more than one page // `StreamingMetadataOutlet` is used to conditionally throw. In the case of parallel routes we will have more than one page
// but we only want to throw on the first one. // but we only want to throw on the first one.
MetadataOutlet: isChildrenRouteKey ? MetadataOutlet : null, MetadataOutlet: isChildrenRouteKey ? MetadataOutlet : null,
prerenderHTTPError,
}, },
false false
) )
......
import { notFound } from 'next/navigation'
export default function Layout() {
notFound()
}
export default function Layout({
children,
parallel,
}: {
children: React.ReactNode
parallel: React.ReactNode
}) {
return (
<>
{children}
{parallel}
</>
)
}
import { Suspense } from 'react'
import { cookies } from 'next/headers'
export function generateStaticParams() {
return [{ slug: 'not-found' }]
}
export default function Page() {
return (
<Suspense fallback={<p>loading dynamic flight</p>}>
<DynamicCookieValue />
</Suspense>
)
}
async function DynamicCookieValue() {
const value =
(await cookies()).get('http-access-fallback-prerender')?.value ?? 'missing'
return <p>dynamic cookie value: {value}</p>
}
export default function Layout({
children,
parallel,
}: {
children: React.ReactNode
parallel: React.ReactNode
}) {
return (
<>
{children}
{parallel}
</>
)
}
export function generateStaticParams() {
return [{ slug: 'not-found' }]
}
export default function Page() {
return <p>static flight</p>
}
'use client'
import { useParams } from 'next/navigation'
export default function NotFound() {
const params = useParams<{ slug: string }>()
return <p>not found {params.slug}</p>
}
import { notFound } from 'next/navigation'
export function generateStaticParams() {
return [{ slug: 'found' }, { slug: 'not-found' }]
}
export default async function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
if (slug === 'found') {
return <p>hello world</p>
}
notFound()
}
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