@@ -10,55 +10,70 @@ import {
1010 convertBodyToReadableStream ,
1111 convertToQueryString ,
1212} from "../core/routing/util" ;
13+ import { generateOpenNextRequestContext } from "./util" ;
14+
15+ globalThis . __als = new AsyncLocalStorage ( ) ;
1316
1417const defaultHandler = async (
1518 internalEvent : InternalEvent ,
1619) : Promise < InternalResult > => {
1720 globalThis . isEdgeRuntime = true ;
1821
19- const host = internalEvent . headers . host
20- ? `https://${ internalEvent . headers . host } `
21- : "http://localhost:3000" ;
22- const initialUrl = new URL ( internalEvent . rawPath , host ) ;
23- initialUrl . search = convertToQueryString ( internalEvent . query ) ;
24- const url = initialUrl . toString ( ) ;
22+ const { requestId, pendingPromiseRunner, isISRRevalidation } =
23+ generateOpenNextRequestContext ( ) ;
2524
26- // @ts -expect-error - This is bundled
27- const handler = await import ( `./middleware.mjs` ) ;
25+ // We run everything in the async local storage context so that it is available in edge runtime functions
26+ return globalThis . __als . run (
27+ { requestId, pendingPromiseRunner, isISRRevalidation } ,
28+ async ( ) => {
29+ const host = internalEvent . headers . host
30+ ? `https://${ internalEvent . headers . host } `
31+ : "http://localhost:3000" ;
32+ const initialUrl = new URL ( internalEvent . rawPath , host ) ;
33+ initialUrl . search = convertToQueryString ( internalEvent . query ) ;
34+ const url = initialUrl . toString ( ) ;
2835
29- const response : Response = await handler . default ( {
30- headers : internalEvent . headers ,
31- method : internalEvent . method || "GET" ,
32- nextConfig : {
33- basePath : NextConfig . basePath ,
34- i18n : NextConfig . i18n ,
35- trailingSlash : NextConfig . trailingSlash ,
36- } ,
37- url,
38- body : convertBodyToReadableStream ( internalEvent . method , internalEvent . body ) ,
39- } ) ;
40- const responseHeaders : Record < string , string | string [ ] > = { } ;
41- response . headers . forEach ( ( value , key ) => {
42- if ( key . toLowerCase ( ) === "set-cookie" ) {
43- responseHeaders [ key ] = responseHeaders [ key ]
44- ? [ ...responseHeaders [ key ] , value ]
45- : [ value ] ;
46- } else {
47- responseHeaders [ key ] = value ;
48- }
49- } ) ;
36+ // @ts -expect-error - This is bundled
37+ const handler = await import ( `./middleware.mjs` ) ;
5038
51- const body =
52- ( response . body as ReadableStream < Uint8Array > ) ?? emptyReadableStream ( ) ;
39+ const response : Response = await handler . default ( {
40+ headers : internalEvent . headers ,
41+ method : internalEvent . method || "GET" ,
42+ nextConfig : {
43+ basePath : NextConfig . basePath ,
44+ i18n : NextConfig . i18n ,
45+ trailingSlash : NextConfig . trailingSlash ,
46+ } ,
47+ url,
48+ body : convertBodyToReadableStream (
49+ internalEvent . method ,
50+ internalEvent . body ,
51+ ) ,
52+ } ) ;
53+ const responseHeaders : Record < string , string | string [ ] > = { } ;
54+ response . headers . forEach ( ( value , key ) => {
55+ if ( key . toLowerCase ( ) === "set-cookie" ) {
56+ responseHeaders [ key ] = responseHeaders [ key ]
57+ ? [ ...responseHeaders [ key ] , value ]
58+ : [ value ] ;
59+ } else {
60+ responseHeaders [ key ] = value ;
61+ }
62+ } ) ;
5363
54- return {
55- type : "core" ,
56- statusCode : response . status ,
57- headers : responseHeaders ,
58- body : body ,
59- // Do we need to handle base64 encoded response?
60- isBase64Encoded : false ,
61- } ;
64+ const body =
65+ ( response . body as ReadableStream < Uint8Array > ) ?? emptyReadableStream ( ) ;
66+
67+ return {
68+ type : "core" ,
69+ statusCode : response . status ,
70+ headers : responseHeaders ,
71+ body : body ,
72+ // Do we need to handle base64 encoded response?
73+ isBase64Encoded : false ,
74+ } ;
75+ } ,
76+ ) ;
6277} ;
6378
6479export const handler = await createGenericHandler ( {
0 commit comments