11import type { ReadableStream } from "node:stream/web" ;
22
33import type { InternalEvent , InternalResult } from "types/open-next" ;
4+ import { runWithOpenNextRequestContext } from "utils/promise" ;
45import { emptyReadableStream } from "utils/stream" ;
56
67// We import it like that so that the edge plugin can replace it
@@ -11,58 +12,65 @@ import {
1112 convertToQueryString ,
1213} from "../core/routing/util" ;
1314
14- declare global {
15- var isEdgeRuntime : true ;
16- }
15+ globalThis . __openNextAls = new AsyncLocalStorage ( ) ;
1716
1817const defaultHandler = async (
1918 internalEvent : InternalEvent ,
2019) : Promise < InternalResult > => {
2120 globalThis . isEdgeRuntime = true ;
2221
23- const host = internalEvent . headers . host
24- ? `https://${ internalEvent . headers . host } `
25- : "http://localhost:3000" ;
26- const initialUrl = new URL ( internalEvent . rawPath , host ) ;
27- initialUrl . search = convertToQueryString ( internalEvent . query ) ;
28- const url = initialUrl . toString ( ) ;
22+ // We run everything in the async local storage context so that it is available in edge runtime functions
23+ return runWithOpenNextRequestContext (
24+ { isISRRevalidation : false } ,
25+ async ( ) => {
26+ const host = internalEvent . headers . host
27+ ? `https://${ internalEvent . headers . host } `
28+ : "http://localhost:3000" ;
29+ const initialUrl = new URL ( internalEvent . rawPath , host ) ;
30+ initialUrl . search = convertToQueryString ( internalEvent . query ) ;
31+ const url = initialUrl . toString ( ) ;
2932
30- // @ts -expect-error - This is bundled
31- const handler = await import ( `./middleware.mjs` ) ;
33+ // @ts -expect-error - This is bundled
34+ const handler = await import ( `./middleware.mjs` ) ;
3235
33- const response : Response = await handler . default ( {
34- headers : internalEvent . headers ,
35- method : internalEvent . method || "GET" ,
36- nextConfig : {
37- basePath : NextConfig . basePath ,
38- i18n : NextConfig . i18n ,
39- trailingSlash : NextConfig . trailingSlash ,
40- } ,
41- url,
42- body : convertBodyToReadableStream ( internalEvent . method , internalEvent . body ) ,
43- } ) ;
44- const responseHeaders : Record < string , string | string [ ] > = { } ;
45- response . headers . forEach ( ( value , key ) => {
46- if ( key . toLowerCase ( ) === "set-cookie" ) {
47- responseHeaders [ key ] = responseHeaders [ key ]
48- ? [ ...responseHeaders [ key ] , value ]
49- : [ value ] ;
50- } else {
51- responseHeaders [ key ] = value ;
52- }
53- } ) ;
36+ const response : Response = await handler . default ( {
37+ headers : internalEvent . headers ,
38+ method : internalEvent . method || "GET" ,
39+ nextConfig : {
40+ basePath : NextConfig . basePath ,
41+ i18n : NextConfig . i18n ,
42+ trailingSlash : NextConfig . trailingSlash ,
43+ } ,
44+ url,
45+ body : convertBodyToReadableStream (
46+ internalEvent . method ,
47+ internalEvent . body ,
48+ ) ,
49+ } ) ;
50+ const responseHeaders : Record < string , string | string [ ] > = { } ;
51+ response . headers . forEach ( ( value , key ) => {
52+ if ( key . toLowerCase ( ) === "set-cookie" ) {
53+ responseHeaders [ key ] = responseHeaders [ key ]
54+ ? [ ...responseHeaders [ key ] , value ]
55+ : [ value ] ;
56+ } else {
57+ responseHeaders [ key ] = value ;
58+ }
59+ } ) ;
5460
55- const body =
56- ( response . body as ReadableStream < Uint8Array > ) ?? emptyReadableStream ( ) ;
61+ const body =
62+ ( response . body as ReadableStream < Uint8Array > ) ?? emptyReadableStream ( ) ;
5763
58- return {
59- type : "core" ,
60- statusCode : response . status ,
61- headers : responseHeaders ,
62- body : body ,
63- // Do we need to handle base64 encoded response?
64- isBase64Encoded : false ,
65- } ;
64+ return {
65+ type : "core" ,
66+ statusCode : response . status ,
67+ headers : responseHeaders ,
68+ body : body ,
69+ // Do we need to handle base64 encoded response?
70+ isBase64Encoded : false ,
71+ } ;
72+ } ,
73+ ) ;
6674} ;
6775
6876export const handler = await createGenericHandler ( {
0 commit comments