Skip to content

Commit e13fa25

Browse files
ascorbicpieh
authored andcommitted
feat: support waitUntil
1 parent beec061 commit e13fa25

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/run/handlers/server.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { nextResponseProxy } from '../revalidate.js'
1616

1717
import { createRequestContext, getLogger, getRequestContext } from './request-context.cjs'
1818
import { getTracer } from './tracer.cjs'
19+
import { setWaitUntil } from './wait-until.cjs'
1920

2021
const nextImportPromise = import('../next.cjs')
2122

@@ -52,7 +53,7 @@ interface FutureContext extends Context {
5253

5354
export default async (request: Request, context: FutureContext) => {
5455
const tracer = getTracer()
55-
56+
setWaitUntil(context)
5657
if (!nextHandler) {
5758
await tracer.withActiveSpan('initialize next server', async () => {
5859
// set the server config

src/run/handlers/wait-until.cts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @see https://github.com/vercel/next.js/blob/canary/packages/next/src/server/after/builtin-request-context.ts
3+
*/
4+
const NEXT_REQUEST_CONTEXT_SYMBOL = Symbol.for('@next/request-context')
5+
6+
export type NextJsRequestContext = {
7+
get(): { waitUntil?: (promise: Promise<unknown>) => void } | undefined
8+
}
9+
10+
type GlobalThisWithRequestContext = typeof globalThis & {
11+
[NEXT_REQUEST_CONTEXT_SYMBOL]?: NextJsRequestContext
12+
}
13+
14+
/**
15+
* Registers a `waitUntil` to be used by Next.js for next/after
16+
*/
17+
18+
export function setWaitUntil({ waitUntil }: { waitUntil?: (promise: Promise<unknown>) => void }) {
19+
if (!waitUntil) {
20+
return
21+
}
22+
// eslint-disable-next-line @typescript-eslint/no-extra-semi
23+
;(globalThis as GlobalThisWithRequestContext)[NEXT_REQUEST_CONTEXT_SYMBOL] = {
24+
get() {
25+
return { waitUntil }
26+
},
27+
}
28+
}

0 commit comments

Comments
 (0)