@@ -5,6 +5,7 @@ import type { BuildManifest } from '../../../../server/get-page-files'
5
5
import type { ReactLoadableManifest } from '../../../../server/load-components'
6
6
import type { ClientReferenceManifest } from '../../plugins/flight-manifest-plugin'
7
7
import type { NextFontManifest } from '../../plugins/next-font-manifest-plugin'
8
+ import type { NextFetchEvent } from '../../../../server/web/spec-extension/fetch-event'
8
9
9
10
import WebServer from '../../../../server/web-server'
10
11
import {
@@ -16,6 +17,15 @@ import { PrerenderManifest } from '../../..'
16
17
import { normalizeAppPath } from '../../../../shared/lib/router/utils/app-paths'
17
18
import { SizeLimit } from '../../../../../types'
18
19
20
+ const NEXT_PRIVATE_GLOBAL_WAIT_UNTIL = Symbol . for (
21
+ '__next_private_global_wait_until__'
22
+ )
23
+
24
+ // @ts -ignore
25
+ globalThis [ NEXT_PRIVATE_GLOBAL_WAIT_UNTIL ] =
26
+ // @ts -ignore
27
+ globalThis [ NEXT_PRIVATE_GLOBAL_WAIT_UNTIL ] || [ ]
28
+
19
29
export function getRender ( {
20
30
dev,
21
31
page,
@@ -143,13 +153,20 @@ export function getRender({
143
153
144
154
const handler = server . getRequestHandler ( )
145
155
146
- return async function render ( request : Request ) {
156
+ return async function render ( request : Request , event : NextFetchEvent ) {
147
157
const extendedReq = new WebNextRequest ( request )
148
158
const extendedRes = new WebNextResponse ( )
149
159
150
160
handler ( extendedReq , extendedRes )
151
161
const result = await extendedRes . toResponse ( )
152
162
163
+ if ( event && event . waitUntil ) {
164
+ event . waitUntil (
165
+ // @ts -ignore
166
+ Promise . all ( [ ...globalThis [ NEXT_PRIVATE_GLOBAL_WAIT_UNTIL ] ] )
167
+ )
168
+ }
169
+
153
170
// fetchMetrics is attached to the web request that going through the server,
154
171
// wait for the handler result is ready and attach it back to the original request.
155
172
; ( request as any ) . fetchMetrics = extendedReq . fetchMetrics
0 commit comments