Unverified Commit 2c21635c authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

if there are errors during postpone, or postpone was caught, fail static generation (#57477)

Rather than sending a warning, we should fail the build if a postpone
call is caught. We should also fail the build if there are any errors
during static generation similar to the non-ppr case.

---------
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 3e341678
......@@ -13,7 +13,6 @@ import {
NEXT_URL,
NEXT_ROUTER_PREFETCH,
} from '../../client/components/app-router-headers'
import { isDynamicUsageError } from '../helpers/is-dynamic-usage-error'
import { NEXT_CACHE_TAGS_HEADER } from '../../lib/constants'
import { hasNextSupport } from '../../telemetry/ci-info'
import { lazyRenderAppPage } from '../../server/future/route-modules/app-page/module.render'
......@@ -80,8 +79,35 @@ export async function exportAppPage(
pathname = '/404'
}
try {
if (isAppPrefetch) {
if (isAppPrefetch) {
await generatePrefetchRsc(
req,
path,
res,
pathname,
htmlFilepath,
renderOpts,
fileWriter
)
return { revalidate: 0 }
}
const result = await lazyRenderAppPage(req, res, pathname, query, renderOpts)
const html = result.toUnchunkedString()
const { metadata } = result
const flightData = metadata.pageData
const revalidate = metadata.revalidate ?? false
const postponed = metadata.postponed
if (revalidate === 0) {
if (isDynamicError) {
throw new Error(
`Page with dynamic = "error" encountered dynamic data method on ${path}.`
)
}
if (!(renderOpts as any).store.staticPrefetchBailout) {
await generatePrefetchRsc(
req,
path,
......@@ -91,109 +117,60 @@ export async function exportAppPage(
renderOpts,
fileWriter
)
return { revalidate: 0 }
}
const result = await lazyRenderAppPage(
req,
res,
pathname,
query,
renderOpts
)
const html = result.toUnchunkedString()
const { metadata } = result
const flightData = metadata.pageData
const revalidate = metadata.revalidate ?? false
const postponed = metadata.postponed
if (revalidate === 0) {
if (isDynamicError) {
throw new Error(
`Page with dynamic = "error" encountered dynamic data method on ${path}.`
)
}
if (!(renderOpts as any).store.staticPrefetchBailout) {
await generatePrefetchRsc(
req,
path,
res,
pathname,
htmlFilepath,
renderOpts,
fileWriter
)
}
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}`
)
const { staticBailoutInfo = {} } = metadata
// 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'))
}
if (revalidate === 0 && debugOutput && staticBailoutInfo?.description) {
const err = new Error(
`Static generation failed due to dynamic usage on ${path}, reason: ${staticBailoutInfo.description}`
)
console.warn(err)
// 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'))
}
return { revalidate: 0 }
console.warn(err)
}
let headers: OutgoingHttpHeaders | undefined
if (metadata.fetchTags) {
headers = { [NEXT_CACHE_TAGS_HEADER]: metadata.fetchTags }
}
return { revalidate: 0 }
}
// Writing static HTML to a file.
await fileWriter(
ExportedAppPageFiles.HTML,
htmlFilepath,
html ?? '',
'utf8'
)
let headers: OutgoingHttpHeaders | undefined
if (metadata.fetchTags) {
headers = { [NEXT_CACHE_TAGS_HEADER]: metadata.fetchTags }
}
// Writing the request metadata to a file.
const meta: RouteMetadata = {
status: undefined,
headers,
postponed,
}
// Writing static HTML to a file.
await fileWriter(ExportedAppPageFiles.HTML, htmlFilepath, html ?? '', 'utf8')
await fileWriter(
ExportedAppPageFiles.META,
htmlFilepath.replace(/\.html$/, '.meta'),
JSON.stringify(meta, null, 2)
)
// Writing the request metadata to a file.
const meta: RouteMetadata = {
status: undefined,
headers,
postponed,
}
// Writing the RSC payload to a file.
await fileWriter(
ExportedAppPageFiles.FLIGHT,
htmlFilepath.replace(/\.html$/, '.rsc'),
flightData
)
await fileWriter(
ExportedAppPageFiles.META,
htmlFilepath.replace(/\.html$/, '.meta'),
JSON.stringify(meta, null, 2)
)
return {
// Only include the metadata if the environment has next support.
metadata: hasNextSupport ? meta : undefined,
hasEmptyPrelude: Boolean(postponed) && html === '',
hasPostponed: Boolean(postponed),
revalidate,
}
} catch (err: any) {
// if the error isn't a special dynamic usage error (caught by Next)
// we also do not throw the error if it occurred while attempting a postpone
// since those will be captured and logged during build/ISR
if (!isDynamicUsageError(err) && !renderOpts.hasPostponeErrors) {
throw err
}
// Writing the RSC payload to a file.
await fileWriter(
ExportedAppPageFiles.FLIGHT,
htmlFilepath.replace(/\.html$/, '.rsc'),
flightData
)
return { revalidate: 0, hasEmptyPrelude: true }
return {
// Only include the metadata if the environment has next support.
metadata: hasNextSupport ? meta : undefined,
hasEmptyPrelude: Boolean(postponed) && html === '',
hasPostponed: Boolean(postponed),
revalidate,
}
}
......@@ -16,7 +16,7 @@ import type {
MockedRequest,
MockedResponse,
} from '../../server/lib/mock-request'
import { isDynamicUsageError } from '../helpers/is-dynamic-usage-error'
import { isDynamicUsageError } from '../../server/app-render/is-dynamic-usage-error'
import { SERVER_DIRECTORY } from '../../shared/lib/constants'
import { hasNextSupport } from '../../telemetry/ci-info'
......
......@@ -61,7 +61,7 @@ import { validateURL } from './validate-url'
import { createFlightRouterStateFromLoaderTree } from './create-flight-router-state-from-loader-tree'
import { handleAction } from './action-handler'
import { NEXT_DYNAMIC_NO_SSR_CODE } from '../../shared/lib/lazy-dynamic/no-ssr-error'
import { warn, error } from '../../build/output/log'
import { warn } from '../../build/output/log'
import { appendMutableCookies } from '../web/spec-extension/adapters/request-cookies'
import { createServerInsertedHTML } from './server-inserted-html'
import { getRequiredScripts } from './required-scripts'
......@@ -72,6 +72,8 @@ import { createComponentTree } from './create-component-tree'
import { getAssetQueryString } from './get-asset-query-string'
import { setReferenceManifestsSingleton } from './action-encryption-utils'
import { createStaticRenderer } from './static/static-renderer'
import { isPostpone } from '../lib/router-utils/is-postpone'
import { isDynamicUsageError } from './is-dynamic-usage-error'
export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug]
......@@ -999,33 +1001,24 @@ async function renderToHTMLOrFlightImpl(
if (staticGenerationStore.isStaticGeneration) {
const htmlResult = await renderResult.toUnchunkedString(true)
if (renderOpts.ppr && postponeErrors.length > 0) {
renderOpts.hasPostponeErrors = true
}
if (
renderOpts.ppr &&
staticGenerationStore.postponeWasTriggered &&
!extraRenderResultMeta.postponed
) {
warn('')
warn(
`${urlPathname} opted out of partial prerendering because the postpone signal was intercepted by a try/catch in your application code.`
throw new Error(
`Postpone signal was caught while rendering ${urlPathname}. These errors should not be caught during static generation. Learn more: https://nextjs.org/docs/messages/ppr-postpone-errors`
)
}
if (postponeErrors.length > 0) {
warn(
'The following errors were re-thrown, and might help find the location of the try/catch that triggered this.'
)
for (let i = 0; i < postponeErrors.length; i++) {
error(`${postponeErrors[i].stack?.split('\n').join('\n ')}`)
}
for (const err of capturedErrors) {
if (!isDynamicUsageError(err) && !isPostpone(err)) {
throw err
}
if (isDynamicUsageError(err)) {
staticGenerationStore.revalidate = 0
}
}
// if we encountered any unexpected errors during build
// we fail the prerendering phase and the build
if (capturedErrors.length > 0) {
throw capturedErrors[0]
}
if (staticGenerationStore.forceStatic === false) {
......
......@@ -137,7 +137,6 @@ export interface RenderOptsPartial {
isPrefetch?: boolean
ppr: boolean
postponed?: string
hasPostponeErrors?: boolean
}
export type RenderOpts = LoadComponentsReturnType<AppPageModule> &
......
import React from 'react'
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
......@@ -7,7 +7,7 @@ export default async function Page() {
<h2>Dynamic Component Catching Errors</h2>
<p>
This shows the dynamic component that reads cookies but wraps the read
in a try/catch. This test does not re-throw the caught error.
in a try/catch.
</p>
<div id="container">
<Suspense fallback={<div>Loading...</div>}>
......
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
ppr: true,
},
}
module.exports = nextConfig
import { nextBuild } from 'next-test-utils'
describe('ppr build errors', () => {
it('should fail the build', async () => {
const out = await nextBuild(__dirname, [], { stderr: true })
expect(out.stderr).toContain(
'Postpone signal was caught while rendering /. These errors should not be caught during static generation.'
)
})
})
import React, { Suspense } from 'react'
import { cookies } from 'next/headers'
import { Dynamic } from '../../../../components/dynamic'
export default async function Page() {
return (
<>
<h2>Dynamic Component Catching Errors</h2>
<p>
This shows the dynamic component that reads cookies but wraps the read
in a try/catch. This test re-throws the error that is caught.
</p>
<div id="container">
<Suspense fallback={<Dynamic fallback />}>
<Dynamic catchErrors />
</Suspense>
<Suspense fallback={<div>Loading...</div>}>
<Foobar />
</Suspense>
</div>
</>
)
}
async function Foobar() {
try {
cookies()
} catch (err) {
throw new Error('You are not signed in')
}
return null
}
import React, { Suspense } from 'react'
import { Dynamic } from '../../../../components/dynamic'
export default async function Page() {
const getData = () =>
fetch('https://example.vercel.sh', {
cache: 'no-store',
})
.then((res) => res.text())
.then((text) => new Promise((res) => setTimeout(() => res(text), 1000)))
try {
await getData()
} catch (err) {
throw new Error('Fetch failed')
}
return (
<>
<h2>Dynamic Component Catching Errors</h2>
<p>
This shows the dynamic component that reads cookies but wraps the read
in a try/catch.
</p>
<div id="container">
<Suspense fallback={<Dynamic fallback />}>
<Dynamic catchErrors />
</Suspense>
</div>
</>
)
}
......@@ -7,26 +7,6 @@ createNextDescribe(
skipDeployment: true,
},
({ next, isNextDev, isNextStart }) => {
if (isNextStart) {
it("should log a warning when `unstable_postpone` is called but there's no postpone state", async () => {
// Three different pages wrap APIs that use `unstable_postpone` in a try catch to trigger these errors
expect(next.cliOutput).toContain(
'/suspense/node/cookies-error-no-throw opted out of partial prerendering because the postpone signal was intercepted by a try/catch in your application code.'
)
expect(next.cliOutput).toContain(
'/suspense/node/fetch-error opted out of partial prerendering because the postpone signal was intercepted by a try/catch in your application code.'
)
expect(next.cliOutput).toContain(
'/suspense/node/cookies-error opted out of partial prerendering because the postpone signal was intercepted by a try/catch in your application code.'
)
// fetch-error re-throws the error after catching it, so we want to make sure that's retained in the logs
expect(next.cliOutput).toContain(
'The following errors were re-thrown, and might help find the location of the try/catch that triggered this.'
)
expect(next.cliOutput).toContain('Error: You are not signed in')
})
}
describe.each([
{ pathname: '/suspense/node' },
{ pathname: '/suspense/node/nested/1' },
......
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