diff --git a/packages/next/src/client/components/maybe-postpone.ts b/packages/next/src/client/components/maybe-postpone.ts index 3215ad58194b8d..8e5fcd7611c641 100644 --- a/packages/next/src/client/components/maybe-postpone.ts +++ b/packages/next/src/client/components/maybe-postpone.ts @@ -21,5 +21,9 @@ export function maybePostpone( // Keep track of if the postpone API has been called. 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` + ) } diff --git a/test/e2e/app-dir/ppr-errors/app/logging-error/page.jsx b/test/e2e/app-dir/ppr-errors/app/logging-error/page.jsx new file mode 100644 index 00000000000000..4138a3e2350439 --- /dev/null +++ b/test/e2e/app-dir/ppr-errors/app/logging-error/page.jsx @@ -0,0 +1,20 @@ +import React, { Suspense } from 'react' +import { cookies } from 'next/headers' + +export default async function Page() { + return ( + Loading...}> + + + ) +} + +async function Foobar() { + try { + cookies() + } catch (err) { + console.log('Logged error: ' + err.message); + } + cookies() // still postpones so doesn't fail build + return null +} diff --git a/test/e2e/app-dir/ppr-errors/ppr-errors.test.ts b/test/e2e/app-dir/ppr-errors/ppr-errors.test.ts index 94f4327e963127..8f53b1e5f059ee 100644 --- a/test/e2e/app-dir/ppr-errors/ppr-errors.test.ts +++ b/test/e2e/app-dir/ppr-errors/ppr-errors.test.ts @@ -2,9 +2,12 @@ import { nextBuild } from 'next-test-utils' describe('ppr build errors', () => { let stderr: string + let stdout: string 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', () => { @@ -79,4 +82,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`.' + ) + }) + }); })