Unverified Commit d68bbd7a authored by Sebastian Markbåge's avatar Sebastian Markbåge Committed by GitHub

Add helpful context to postpone reason if it's caught and logged or escapes (#58222)

Other ways such as on the client.
parent d422aeb5
...@@ -21,5 +21,9 @@ export function maybePostpone( ...@@ -21,5 +21,9 @@ export function maybePostpone(
// Keep track of if the postpone API has been called. // Keep track of if the postpone API has been called.
staticGenerationStore.postponeWasTriggered = true staticGenerationStore.postponeWasTriggered = true
React.unstable_postpone(reason) React.unstable_postpone(
`This page needs to opt out of static rendering at this point because it used ` +
`${reason}. React throws this special object to bail out. It should not be caught ` +
`by your own try/catch. Learn more: https://nextjs.org/docs/messages/ppr-postpone-error`
)
} }
import React, { Suspense } from 'react'
import { cookies } from 'next/headers'
export default async function Page() {
return (
<Suspense fallback={<div>Loading...</div>}>
<Foobar />
</Suspense>
)
}
async function Foobar() {
try {
cookies()
} catch (err) {
console.log('Logged error: ' + err.message)
}
cookies() // still postpones so doesn't fail build
return null
}
...@@ -2,9 +2,15 @@ import { nextBuild } from 'next-test-utils' ...@@ -2,9 +2,15 @@ import { nextBuild } from 'next-test-utils'
describe('ppr build errors', () => { describe('ppr build errors', () => {
let stderr: string let stderr: string
let stdout: string
beforeAll(async () => { beforeAll(async () => {
stderr = (await nextBuild(__dirname, [], { stderr: true })).stderr const output = await nextBuild(__dirname, [], {
stderr: true,
stdout: true,
})
stderr = output.stderr
stdout = output.stdout
}) })
describe('within a suspense boundary', () => { describe('within a suspense boundary', () => {
...@@ -79,4 +85,12 @@ describe('ppr build errors', () => { ...@@ -79,4 +85,12 @@ describe('ppr build errors', () => {
}) })
}) })
}) })
describe('when a postpone call is caught and logged it should', () => {
it('should include a message telling why', async () => {
expect(stdout).toContain(
"Logged error: This page needs to opt out of static rendering at this point because it used Page couldn't be rendered statically because it used `cookies`."
)
})
})
}) })
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