Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: add prerender abort errors to unstable rethrow #74556

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/next/src/client/components/unstable-rethrow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isDynamicUsageError } from '../../export/helpers/is-dynamic-usage-error'
import { isHangingPromiseRejectionError } from '../../server/dynamic-rendering-utils'
import { isPostpone } from '../../server/lib/router-utils/is-postpone'
import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
import { isNextRouterError } from './is-next-router-error'
Expand All @@ -15,7 +16,8 @@ export function unstable_rethrow(error: unknown): void {
isNextRouterError(error) ||
isBailoutToCSRError(error) ||
isDynamicUsageError(error) ||
isPostpone(error)
isPostpone(error) ||
isHangingPromiseRejectionError(error)
) {
throw error
}
Expand Down
28 changes: 23 additions & 5 deletions packages/next/src/server/dynamic-rendering-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
export function isHangingPromiseRejectionError(
err: unknown
): err is HangingPromiseRejectionError {
if (typeof err !== 'object' || err === null || !('digest' in err)) {
return false
}

return err.digest === HANGING_PROMISE_REJECTION
}

const HANGING_PROMISE_REJECTION = 'HANGING_PROMISE_REJECTION'

class HangingPromiseRejectionError extends Error {
public readonly digest = HANGING_PROMISE_REJECTION

constructor(public readonly expression: string) {
super(
`During prerendering, ${expression} rejects when the prerender is complete. Typically these errors are handled by React but if you move ${expression} to a different context by using \`setTimeout\`, \`after\`, or similar functions you may observe this error and you should handle it in that context.`
)
}
}

/**
* This function constructs a promise that will never resolve. This is primarily
* useful for dynamicIO where we use promise resolution timing to determine which
Expand All @@ -13,11 +35,7 @@ export function makeHangingPromise<T>(
signal.addEventListener(
'abort',
() => {
reject(
new Error(
`During prerendering, ${expression} rejects when the prerender is complete. Typically these errors are handled by React but if you move ${expression} to a different context by using \`setTimeout\`, \`after\`, or similar functions you may observe this error and you should handle it in that context.`
)
)
reject(new HangingPromiseRejectionError(expression))
},
{ once: true }
)
Expand Down
Loading