Skip to content

Commit c938bb6

Browse files
committed
chore: clarify awaiting backgroundWorkPromise
1 parent 80de44f commit c938bb6

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/run/handlers/request-context.cts

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ export type RequestContext = {
2222
serverTiming?: string
2323
routeHandlerRevalidate?: NetlifyCachedRouteValue['revalidate']
2424
/**
25-
* Track promise running in the background and need to be waited for
25+
* Track promise running in the background and need to be waited for.
26+
* Uses `context.waitUntil` if available, otherwise stores promises to
27+
* await on.
2628
*/
2729
trackBackgroundWork: (promise: Promise<unknown>) => void
2830
/**
29-
* Promise that need to be executed even if response was already sent
31+
* Promise that need to be executed even if response was already sent.
32+
* If `context.waitUntil` is available this promise will be always resolved
33+
* because background work tracking was offloaded to `context.waitUntil`.
3034
*/
3135
backgroundWorkPromise: Promise<unknown>
3236
logger: SystemLogger

src/run/handlers/server.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ export default async (request: Request) => {
134134
// otherwise Next would never run the callback variant of `next/after`
135135
res.emit('close')
136136

137-
// if waitUntil is not available, we have to keep response stream open until background promises are resolved
138-
// to ensure that all background work executes
137+
// We have to keep response stream open until tracked background promises that are don't use `context.waitUntil`
138+
// are resolved. If `context.waitUntil` is available, `requestContext.backgroundWorkPromise` will be empty
139+
// resolved promised and so awaiting it is no-op
139140
await requestContext.backgroundWorkPromise
140141
},
141142
})

0 commit comments

Comments
 (0)