Unverified Commit 7a0fdb33 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix: ensure DynamicUsageErrors caught during render bubble up (#58747)

After the changes in #58669, it's now possible for these `DynamicUsageErrors` to be caught in the streaming renderer. We didn't have any logic to handle this happening inside the existing catch block, so this adds a check for that specific error and re-throws it so that it properly bubbles to `exportPage`. `exportPage` checks the error to see if it's a `DynamicUsageError` and if so, marks the page as dynamic.

(This resolves an issue with particular scenarios hanging the build, caught by a few of our tests: [example](https://github.com/vercel/next.js/actions/runs/6949079695/job/18907597751?pr=58744), [example](https://github.com/vercel/next.js/actions/runs/6948412805/job/18904387924))
parent 3043feef
......@@ -160,17 +160,11 @@ export async function exportAppPage(
const { staticBailoutInfo = {} } = metadata
if (revalidate === 0 && debugOutput && staticBailoutInfo?.description) {
const err = new Error(
`Static generation failed due to dynamic usage on ${path}, reason: ${staticBailoutInfo.description}`
)
// Update the stack if it was provided via the bailout info.
const { stack } = staticBailoutInfo
if (stack) {
err.stack = err.message + stack.substring(stack.indexOf('\n'))
}
console.warn(err)
logDynamicUsageWarning({
path,
description: staticBailoutInfo.description,
stack: staticBailoutInfo.stack,
})
}
return { revalidate: 0 }
......@@ -234,6 +228,37 @@ export async function exportAppPage(
throw err
}
if (debugOutput) {
const { dynamicUsageDescription, dynamicUsageStack } = (renderOpts as any)
.store
logDynamicUsageWarning({
path,
description: dynamicUsageDescription,
stack: dynamicUsageStack,
})
}
return { revalidate: 0 }
}
}
function logDynamicUsageWarning({
path,
description,
stack,
}: {
path: string
description: string
stack?: string
}) {
const errMessage = new Error(
`Static generation failed due to dynamic usage on ${path}, reason: ${description}`
)
if (stack) {
errMessage.stack = errMessage.message + stack.substring(stack.indexOf('\n'))
}
console.warn(errMessage)
}
......@@ -75,6 +75,7 @@ import { setReferenceManifestsSingleton } from './action-encryption-utils'
import { createStaticRenderer } from './static/static-renderer'
import { MissingPostponeDataError } from './is-missing-postpone-error'
import { DetachedPromise } from '../../lib/detached-promise'
import { DYNAMIC_ERROR_CODE } from '../../client/components/hooks-server-context'
export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug]
......@@ -819,6 +820,12 @@ async function renderToHTMLOrFlightImpl(
throw err
}
if (isStaticGeneration && err.digest === DYNAMIC_ERROR_CODE) {
// ensure that DynamicUsageErrors bubble up during static generation
// as this will indicate that the page needs to be dynamically rendered
throw err
}
if (err.digest === NEXT_DYNAMIC_NO_SSR_CODE) {
warn(
`Entire page ${pagePath} deopted into client-side rendering. https://nextjs.org/docs/messages/deopted-into-client-rendering`,
......
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