Unverified Commit f847daef authored by Josh Story's avatar Josh Story Committed by GitHub

[dynamicIO] warn in debug mode when prospective renders error (#71266)

When prerendering pages and routes errors with dynamicIO enabled, errors
during the prospective render may be supressed from logging. Normally
this is expected and ok because the dynamic portion of a render may be
expected to error during prerendering. However some codepaths are not
hit the same between the prospective and final renders and so it can be
useful to have the ability to get additional insight into the runtime
behavior of the prospective render.

This change adds extra logging when `process.env.NEXT_DEBUG_BUILD` is
enabled. When this environment variable is set we log errors that throw
during the prospective render.
parent fc2a7d29
...@@ -147,6 +147,7 @@ import { ...@@ -147,6 +147,7 @@ import {
createReactServerPrerenderResultFromRender, createReactServerPrerenderResultFromRender,
prerenderAndAbortInSequentialTasks, prerenderAndAbortInSequentialTasks,
} from './app-render-prerender-utils' } from './app-render-prerender-utils'
import { printDebugThrownValueForProspectiveRender } from './prospective-render-utils'
import { scheduleInSequentialTasks } from './app-render-render-utils' import { scheduleInSequentialTasks } from './app-render-render-utils'
import { waitAtLeastOneReactRenderTask } from '../../lib/scheduler' import { waitAtLeastOneReactRenderTask } from '../../lib/scheduler'
import { import {
...@@ -1953,16 +1954,19 @@ async function prerenderToStream( ...@@ -1953,16 +1954,19 @@ async function prerenderToStream(
}) })
let reactServerIsDynamic = false let reactServerIsDynamic = false
function onError(err: unknown) { function prospectiveRenderOnError(err: unknown) {
if (err === abortReason) { if (err === abortReason) {
reactServerIsDynamic = true reactServerIsDynamic = true
return PRERENDER_COMPLETE return PRERENDER_COMPLETE
} else if (isPrerenderInterruptedError(err)) { } else if (isPrerenderInterruptedError(err)) {
reactServerIsDynamic = true reactServerIsDynamic = true
return err.digest return err.digest
} else if (process.env.NEXT_DEBUG_BUILD) {
printDebugThrownValueForProspectiveRender(err, workStore.route)
} }
// We don't track errors during the prospective render because we will
return serverComponentsErrorHandler(err) // always do a final render and we cannot infer the errors from this render
// are relevant to the final result
} }
// We're not going to use the result of this render because the only time it could be used // We're not going to use the result of this render because the only time it could be used
...@@ -1975,8 +1979,6 @@ async function prerenderToStream( ...@@ -1975,8 +1979,6 @@ async function prerenderToStream(
res.statusCode === 404 res.statusCode === 404
) )
let didError = false
let prospectiveRenderError: unknown = null
;( ;(
workUnitAsyncStorage.run( workUnitAsyncStorage.run(
// The store to scope // The store to scope
...@@ -1987,35 +1989,27 @@ async function prerenderToStream( ...@@ -1987,35 +1989,27 @@ async function prerenderToStream(
firstAttemptRSCPayload, firstAttemptRSCPayload,
clientReferenceManifest.clientModules, clientReferenceManifest.clientModules,
{ {
// This render will be thrown away so we don't need to track errors or postpones onError: prospectiveRenderOnError,
onError,
onPostpone: undefined,
// we don't care to track postpones during the prospective render because we need // we don't care to track postpones during the prospective render because we need
// to always do a final render anyway // to always do a final render anyway
onPostpone: undefined,
signal: flightController.signal, signal: flightController.signal,
} }
) as Promise<ReactServerPrerenderResolveToType> ) as Promise<ReactServerPrerenderResolveToType>
).catch((err) => { ).catch((err) => {
if ( if (
process.env.NEXT_DEBUG_BUILD &&
err !== abortReason && err !== abortReason &&
!isPrerenderInterruptedError(err) && !isPrerenderInterruptedError(err) &&
!isDynamicServerError(err) !isDynamicServerError(err)
) { ) {
didError = true printDebugThrownValueForProspectiveRender(err, workStore.route)
prospectiveRenderError = err
} }
}) })
// When this resolves the cache has no inflight reads and we can ascertain the dynamic outcome // When this resolves the cache has no inflight reads and we can ascertain the dynamic outcome
await cacheSignal.cacheReady() await cacheSignal.cacheReady()
flightController.abort(abortReason) flightController.abort(abortReason)
// We wait a microtask to to ensure the catch handler has a chance to run if the root errors when we abort.
await 1
if (didError) {
// We errored with something other than prerender errors during the warmup. We throw here
// to allow the user error to be handled
throw prospectiveRenderError
}
// When PPR is enabled we don't synchronously abort the render when performing a prospective render // When PPR is enabled we don't synchronously abort the render when performing a prospective render
// because it might prevent us from discovering all caches during the render which is essential // because it might prevent us from discovering all caches during the render which is essential
...@@ -2045,6 +2039,18 @@ async function prerenderToStream( ...@@ -2045,6 +2039,18 @@ async function prerenderToStream(
tags: [...ctx.requestStore.implicitTags], tags: [...ctx.requestStore.implicitTags],
}) })
function finalRenderOnError(err: unknown) {
if (err === abortReason) {
reactServerIsDynamic = true
return PRERENDER_COMPLETE
} else if (isPrerenderInterruptedError(err)) {
reactServerIsDynamic = true
return err.digest
}
return serverComponentsErrorHandler(err)
}
function onPostpone(reason: string) { function onPostpone(reason: string) {
if ( if (
reason === PRERENDER_COMPLETE || reason === PRERENDER_COMPLETE ||
...@@ -2073,7 +2079,7 @@ async function prerenderToStream( ...@@ -2073,7 +2079,7 @@ async function prerenderToStream(
finalAttemptRSCPayload, finalAttemptRSCPayload,
clientReferenceManifest.clientModules, clientReferenceManifest.clientModules,
{ {
onError, onError: finalRenderOnError,
onPostpone, onPostpone,
signal: flightController.signal, signal: flightController.signal,
} }
...@@ -2340,16 +2346,19 @@ async function prerenderToStream( ...@@ -2340,16 +2346,19 @@ async function prerenderToStream(
let reactServerIsDynamic = false let reactServerIsDynamic = false
let reactServerIsSynchronouslyDynamic = false let reactServerIsSynchronouslyDynamic = false
function onError(err: unknown) { function prospectiveRenderOnError(err: unknown) {
if (err === abortReason) { if (err === abortReason) {
reactServerIsDynamic = true reactServerIsDynamic = true
return PRERENDER_COMPLETE return PRERENDER_COMPLETE
} else if (isPrerenderInterruptedError(err)) { } else if (isPrerenderInterruptedError(err)) {
reactServerIsSynchronouslyDynamic = true reactServerIsSynchronouslyDynamic = true
return err.digest return err.digest
} else if (process.env.NEXT_DEBUG_BUILD) {
printDebugThrownValueForProspectiveRender(err, workStore.route)
} }
// We don't track errors during the prospective render because we will
return serverComponentsErrorHandler(err) // always do a final render and we cannot infer the errors from this render
// are relevant to the final result
} }
try { try {
...@@ -2362,7 +2371,7 @@ async function prerenderToStream( ...@@ -2362,7 +2371,7 @@ async function prerenderToStream(
firstAttemptRSCPayload, firstAttemptRSCPayload,
clientReferenceManifest.clientModules, clientReferenceManifest.clientModules,
{ {
onError, onError: prospectiveRenderOnError,
signal: flightController.signal, signal: flightController.signal,
} }
) as ReadableStream<Uint8Array> ) as ReadableStream<Uint8Array>
...@@ -2376,15 +2385,12 @@ async function prerenderToStream( ...@@ -2376,15 +2385,12 @@ async function prerenderToStream(
await warmFlightResponse(prospectiveStream, clientReferenceManifest) await warmFlightResponse(prospectiveStream, clientReferenceManifest)
} catch (err) { } catch (err) {
if ( if (
err === abortReason || process.env.NEXT_DEBUG_BUILD &&
isPrerenderInterruptedError(err) || err !== abortReason &&
isDynamicServerError(err) !isPrerenderInterruptedError(err) &&
!isDynamicServerError(err)
) { ) {
// We aborted with an incomplete shell. We'll handle this below with the handling printDebugThrownValueForProspectiveRender(err, workStore.route)
// for dynamic.
} else {
// We have some other kind of shell error, we want to bubble this up to be handled
throw err
} }
} }
...@@ -2440,6 +2446,18 @@ async function prerenderToStream( ...@@ -2440,6 +2446,18 @@ async function prerenderToStream(
res.statusCode === 404 res.statusCode === 404
) )
function finalRenderOnError(err: unknown) {
if (err === abortReason) {
reactServerIsDynamic = true
return PRERENDER_COMPLETE
} else if (isPrerenderInterruptedError(err)) {
reactServerIsDynamic = true
return err.digest
}
return serverComponentsErrorHandler(err)
}
function SSROnError(err: unknown, errorInfo?: ErrorInfo) { function SSROnError(err: unknown, errorInfo?: ErrorInfo) {
if ( if (
isAbortReason(err, abortReason) || isAbortReason(err, abortReason) ||
...@@ -2477,7 +2495,7 @@ async function prerenderToStream( ...@@ -2477,7 +2495,7 @@ async function prerenderToStream(
finalAttemptRSCPayload, finalAttemptRSCPayload,
clientReferenceManifest.clientModules, clientReferenceManifest.clientModules,
{ {
onError, onError: finalRenderOnError,
signal: flightController.signal, signal: flightController.signal,
} }
) as ReadableStream<Uint8Array> ) as ReadableStream<Uint8Array>
......
export function printDebugThrownValueForProspectiveRender(
thrownValue: unknown,
route: string
) {
let message: undefined | string
if (
typeof thrownValue === 'object' &&
thrownValue !== null &&
typeof (thrownValue as any).message === 'string'
) {
message = (thrownValue as any).message
if (typeof (thrownValue as any).stack === 'string') {
const originalErrorStack: string = (thrownValue as any).stack
const stackStart = originalErrorStack.indexOf('\n')
if (stackStart > -1) {
const error = new Error(
`Route ${route} errored during the prospective render. These errors are normally ignored and may not prevent the route from prerendering but are logged here because build debugging is enabled.
Original Error: ${message}`
)
error.stack =
'Error: ' + error.message + originalErrorStack.slice(stackStart)
console.error(error)
return
}
}
} else if (typeof thrownValue === 'string') {
message = thrownValue
}
if (message) {
console.error(`Route ${route} errored during the prospective render. These errors are normally ignored and may not prevent the route from prerendering but are logged here because build debugging is enabled. No stack was provided.
Original Message: ${message}`)
return
}
console.error(
`Route ${route} errored during the prospective render. These errors are normally ignored and may not prevent the route from prerendering but are logged here because build debugging is enabled. The thrown value is logged just following this message`
)
console.error(thrownValue)
return
}
...@@ -31,6 +31,7 @@ import { ...@@ -31,6 +31,7 @@ import {
import { HeadersAdapter } from '../../web/spec-extension/adapters/headers' import { HeadersAdapter } from '../../web/spec-extension/adapters/headers'
import { RequestCookiesAdapter } from '../../web/spec-extension/adapters/request-cookies' import { RequestCookiesAdapter } from '../../web/spec-extension/adapters/request-cookies'
import { parsedUrlQueryToParams } from './helpers/parsed-url-query-to-params' import { parsedUrlQueryToParams } from './helpers/parsed-url-query-to-params'
import { printDebugThrownValueForProspectiveRender } from '../../app-render/prospective-render-utils'
import * as serverHooks from '../../../client/components/hooks-server-context' import * as serverHooks from '../../../client/components/hooks-server-context'
import { DynamicServerError } from '../../../client/components/hooks-server-context' import { DynamicServerError } from '../../../client/components/hooks-server-context'
...@@ -327,7 +328,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -327,7 +328,7 @@ export class AppRouteRouteModule extends RouteModule<
* *
* Next we run the handler again and we check if we get a result back in a microtask. * Next we run the handler again and we check if we get a result back in a microtask.
* Next.js expects the return value to be a Response or a Thenable that resolves to a Response. * Next.js expects the return value to be a Response or a Thenable that resolves to a Response.
* Unfortunately Response's do not allow for acessing the response body synchronously or in * Unfortunately Response's do not allow for accessing the response body synchronously or in
* a microtask so we need to allow one more task to unwrap the response body. This is a slightly * a microtask so we need to allow one more task to unwrap the response body. This is a slightly
* different semantic than what we have when we render and it means that certain tasks can still * different semantic than what we have when we render and it means that certain tasks can still
* execute before a prerender completes such as a carefully timed setImmediate. * execute before a prerender completes such as a carefully timed setImmediate.
...@@ -370,6 +371,8 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -370,6 +371,8 @@ export class AppRouteRouteModule extends RouteModule<
// the route handler called an API which is always dynamic // the route handler called an API which is always dynamic
// there is no need to try again // there is no need to try again
prospectiveRenderIsDynamic = true prospectiveRenderIsDynamic = true
} else if (process.env.NEXT_DEBUG_BUILD) {
printDebugThrownValueForProspectiveRender(err, workStore.route)
} }
} }
if ( if (
...@@ -386,6 +389,11 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -386,6 +389,11 @@ export class AppRouteRouteModule extends RouteModule<
// the route handler called an API which is always dynamic // the route handler called an API which is always dynamic
// there is no need to try again // there is no need to try again
prospectiveRenderIsDynamic = true prospectiveRenderIsDynamic = true
} else if (process.env.NEXT_DEBUG_BUILD) {
printDebugThrownValueForProspectiveRender(
err,
workStore.route
)
} }
} }
) )
......
import { Indirection } from '../indirection'
let didError = false
export default async function Page() {
return (
<>
<p>
This page errors during the prospective render during build. It errors
on the first render during dev.
</p>
<Indirection>
<ErrorFirstTime />
</Indirection>
</>
)
}
async function ErrorFirstTime() {
if (!didError) {
didError = true
throw new Error('BOOM (Error)')
}
return null
}
'use client'
export function Indirection({ children }: { children: React.ReactNode }) {
return children
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
import { Indirection } from '../indirection'
let didError = false
export default async function Page() {
return (
<>
<p>
This page errors during the prospective render during build. It errors
on the first render during dev.
</p>
<Indirection>
<ErrorFirstTime />
</Indirection>
</>
)
}
async function ErrorFirstTime() {
if (!didError) {
didError = true
// eslint-disable-next-line no-throw-literal
throw null
}
return null
}
import { Indirection } from '../indirection'
let didError = false
export default async function Page() {
return (
<>
<p>
This page errors during the prospective render during build. It errors
on the first render during dev.
</p>
<Indirection>
<ErrorFirstTime />
</Indirection>
</>
)
}
async function ErrorFirstTime() {
if (!didError) {
didError = true
// eslint-disable-next-line no-throw-literal
throw { boom: '(Object)' }
}
return null
}
let didError = false
export async function GET() {
errorFirstTime()
return new Response(
'This page errors during the prospective render during build. It errors on the first render during dev.'
)
}
function errorFirstTime() {
if (!didError) {
didError = true
throw new Error('BOOM (Error route)')
}
return null
}
let didError = false
export async function GET() {
errorFirstTime()
return new Response(
'This page errors during the prospective render during build. It errors on the first render during dev.'
)
}
function errorFirstTime() {
if (!didError) {
didError = true
// eslint-disable-next-line no-throw-literal
throw { boom: '(Object route)' }
}
return null
}
let didError = false
export async function GET() {
errorFirstTime()
return new Response(
'This page errors during the prospective render during build. It errors on the first render during dev.'
)
}
function errorFirstTime() {
if (!didError) {
didError = true
// eslint-disable-next-line no-throw-literal
throw 'BOOM (string route)'
}
return null
}
let didError = false
export async function GET() {
errorFirstTime()
return new Response(
'This page errors during the prospective render during build. It errors on the first render during dev.'
)
}
function errorFirstTime() {
if (!didError) {
didError = true
// eslint-disable-next-line no-throw-literal
throw undefined
}
return null
}
import { Indirection } from '../indirection'
let didError = false
export default async function Page() {
return (
<>
<p>
This page errors during the prospective render during build. It errors
on the first render during dev.
</p>
<Indirection>
<ErrorFirstTime />
</Indirection>
</>
)
}
async function ErrorFirstTime() {
if (!didError) {
didError = true
// eslint-disable-next-line no-throw-literal
throw 'BOOM (string)'
}
return null
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
ppr: process.env.__NEXT_EXPERIMENTAL_PPR === 'true',
pprFallbacks: process.env.__NEXT_EXPERIMENTAL_PPR === 'true',
dynamicIO: true,
serverMinification: false,
},
}
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