Skip to content

Commit

Permalink
Add helpful context to postpone reason if it's caught and logged or e…
Browse files Browse the repository at this point in the history
…scapes

other ways such as on the client.
  • Loading branch information
sebmarkbage committed Nov 8, 2023
1 parent cf8a733 commit 6087bf2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/next/src/client/components/maybe-postpone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)
}
20 changes: 20 additions & 0 deletions test/e2e/app-dir/ppr-errors/app/logging-error/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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
}
13 changes: 12 additions & 1 deletion test/e2e/app-dir/ppr-errors/ppr-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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`.'
)
})
});
})

0 comments on commit 6087bf2

Please sign in to comment.