forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic-rendering-utils.ts
More file actions
50 lines (44 loc) · 1.72 KB
/
dynamic-rendering-utils.ts
File metadata and controls
50 lines (44 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
* parts of a render can be included in a prerender.
*
* @internal
*/
export function makeHangingPromise<T>(
signal: AbortSignal,
expression: string
): Promise<T> {
const hangingPromise = new Promise<T>((_, reject) => {
signal.addEventListener(
'abort',
() => {
reject(new HangingPromiseRejectionError(expression))
},
{ once: true }
)
})
// We are fine if no one actually awaits this promise. We shouldn't consider this an unhandled rejection so
// we attach a noop catch handler here to suppress this warning. If you actually await somewhere or construct
// your own promise out of it you'll need to ensure you handle the error when it rejects.
hangingPromise.catch(ignoreReject)
return hangingPromise
}
function ignoreReject() {}