Unverified Commit cd07aa37 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Node.js streams: Add forkpoint for error recovery (#92457)

## What?

Building on top of the changes in
https://github.com/vercel/next.js/pull/92252. This PR implements similar
fork points for the `catch` path.
parent a05035f3
...@@ -4082,6 +4082,7 @@ async function renderToStream( ...@@ -4082,6 +4082,7 @@ async function renderToStream(
throw err throw err
} }
// MARK: errorRecovery classification
let errorType: MetadataErrorType | 'redirect' | undefined let errorType: MetadataErrorType | 'redirect' | undefined
if (isHTTPAccessFallbackError(err)) { if (isHTTPAccessFallbackError(err)) {
...@@ -4121,6 +4122,104 @@ async function renderToStream( ...@@ -4121,6 +4122,104 @@ async function renderToStream(
'/_not-found/page' '/_not-found/page'
) )
if (process.env.__NEXT_USE_NODE_STREAMS) {
// MARK: nodeStreams errorRecovery RSC + HTML
let errorRSCPayload: InitialRSCPayload
let errorServerStream: import('./stream-ops').AnyStream
try {
errorRSCPayload = await workUnitAsyncStorage.run(
requestStore,
getErrorRSCPayload,
tree,
ctx,
reactServerErrorsByDigest.has((err as any).digest) ? null : err,
errorType
)
errorServerStream = workUnitAsyncStorage.run(
requestStore,
renderToNodeFlightStream,
ctx.componentMod,
errorRSCPayload,
clientModules,
{
filterStackFrame,
onError: serverComponentsErrorHandler,
}
)
if (reactServerResult === null) {
endSpanWithError(err)
throw err
}
} catch (setupErr) {
endSpanWithError(setupErr)
throw setupErr
}
try {
const generateStaticHTML =
supportsDynamicResponse !== true || !!shouldWaitOnAllReady
const { stream: errorHtmlStream, allReady: errorAllReady } =
await workUnitAsyncStorage.run(
requestStore,
renderToNodeFizzStream,
<ErrorApp
reactServerStream={errorServerStream}
ServerInsertedHTMLProvider={ServerInsertedHTMLProvider}
preinitScripts={errorPreinitScripts}
nonce={nonce}
images={ctx.renderOpts.images}
/>,
{
nonce,
bootstrapScriptContent,
bootstrapScripts: [errorBootstrapScript],
formState,
}
)
errorAllReady.finally(() => {
if (renderSpan.isRecording()) renderSpan.end()
})
return await continueFizzStream(errorHtmlStream, {
inlinedDataStream: createNodeInlinedDataStream(
// This is intentionally using the readable datastream from the
// main render rather than the flight data from the error page
// render
reactServerResult.consume(),
nonce,
formState
),
isStaticGeneration: generateStaticHTML,
deploymentId: ctx.sharedContext.deploymentId,
getServerInsertedHTML: makeGetServerInsertedHTML({
polyfills,
renderServerInsertedHTML,
serverCapturedErrors: [],
basePath,
tracingMetadata: tracingMetadata,
}),
getServerInsertedMetadata,
validateRootLayout: !!process.env.__NEXT_DEV_SERVER,
})
} catch (finalErr: any) {
if (
process.env.__NEXT_DEV_SERVER &&
isHTTPAccessFallbackError(finalErr)
) {
const { bailOnRootNotFound } =
require('../../client/components/dev-root-http-access-fallback-boundary') as typeof import('../../client/components/dev-root-http-access-fallback-boundary')
bailOnRootNotFound()
}
endSpanWithError(finalErr)
throw finalErr
}
} else {
// MARK: webStreams errorRecovery RSC + HTML
let errorRSCPayload: InitialRSCPayload let errorRSCPayload: InitialRSCPayload
let errorServerStream: import('./stream-ops').AnyStream let errorServerStream: import('./stream-ops').AnyStream
...@@ -4147,8 +4246,6 @@ async function renderToStream( ...@@ -4147,8 +4246,6 @@ async function renderToStream(
) )
if (reactServerResult === null) { if (reactServerResult === null) {
// We errored when we did not have an RSC stream to read from. This is not just a render
// error, we need to throw early
endSpanWithError(err) endSpanWithError(err)
throw err throw err
} }
...@@ -4180,7 +4277,6 @@ async function renderToStream( ...@@ -4180,7 +4277,6 @@ async function renderToStream(
} }
) )
// End the render span only after React completed rendering (including anything inside Suspense boundaries)
errorAllReady.finally(() => { errorAllReady.finally(() => {
if (renderSpan.isRecording()) renderSpan.end() if (renderSpan.isRecording()) renderSpan.end()
}) })
...@@ -4219,6 +4315,7 @@ async function renderToStream( ...@@ -4219,6 +4315,7 @@ async function renderToStream(
throw finalErr throw finalErr
} }
} }
}
}) })
/* eslint-enable @next/internal/no-ambiguous-jsx */ /* eslint-enable @next/internal/no-ambiguous-jsx */
} }
......
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