@@ -13,14 +13,18 @@ import { createALSProxy } from "./utils";
1313
1414const NON_BODY_RESPONSES = new Set ( [ 101 , 204 , 205 , 304 ] ) ;
1515
16- const processALS = new AsyncLocalStorage < typeof process > ( ) ;
16+ const processEnvALS = new AsyncLocalStorage < Record < string , unknown > > ( ) ;
1717const cloudflareContextALS = new AsyncLocalStorage < CloudflareContext > ( ) ;
1818
1919// Note: this symbol needs to be kept in sync with the one defined in `src/api/get-cloudflare-context.ts`
2020// eslint-disable-next-line @typescript-eslint/no-explicit-any
2121( globalThis as any ) [ Symbol . for ( "__cloudflare-context__" ) ] = createALSProxy ( cloudflareContextALS ) ;
2222
23- globalThis . process = createALSProxy ( processALS ) ;
23+ globalThis . process = {
24+ ...globalThis . process ,
25+ // @ts -expect-error - populated when we run inside the ALS context
26+ env : createALSProxy ( processEnvALS ) ,
27+ } ;
2428
2529// Injected at build time
2630const nextConfig : NextConfig = JSON . parse ( process . env . __NEXT_PRIVATE_STANDALONE_CONFIG ?? "{}" ) ;
@@ -29,45 +33,42 @@ let requestHandler: NodeRequestHandler | null = null;
2933
3034export default {
3135 async fetch ( request , env , ctx ) {
32- return processALS . run (
33- { ...globalThis . process , env : { ...globalThis . process . env , NODE_ENV : "production" , ...env } } ,
34- ( ) => {
35- return cloudflareContextALS . run ( { env, ctx, cf : request . cf } , async ( ) => {
36- if ( requestHandler == null ) {
37- // Note: "next/dist/server/next-server" is a cjs module so we have to `require` it not to confuse esbuild
38- // (since esbuild can run in projects with different module resolutions)
39- // eslint-disable-next-line @typescript-eslint/no-require-imports
40- const NextNodeServer = require ( "next/dist/server/next-server" )
41- . default as typeof import ( "next/dist/server/next-server" ) . default ;
42-
43- requestHandler = new NextNodeServer ( {
44- conf : nextConfig ,
45- customServer : false ,
46- dev : false ,
47- dir : "" ,
48- minimalMode : false ,
49- } ) . getRequestHandler ( ) ;
50- }
36+ return processEnvALS . run ( { NODE_ENV : "production" , ...env } , ( ) => {
37+ return cloudflareContextALS . run ( { env, ctx, cf : request . cf } , async ( ) => {
38+ if ( requestHandler == null ) {
39+ // Note: "next/dist/server/next-server" is a cjs module so we have to `require` it not to confuse esbuild
40+ // (since esbuild can run in projects with different module resolutions)
41+ // eslint-disable-next-line @typescript-eslint/no-require-imports
42+ const NextNodeServer = require ( "next/dist/server/next-server" )
43+ . default as typeof import ( "next/dist/server/next-server" ) . default ;
44+
45+ requestHandler = new NextNodeServer ( {
46+ conf : nextConfig ,
47+ customServer : false ,
48+ dev : false ,
49+ dir : "" ,
50+ minimalMode : false ,
51+ } ) . getRequestHandler ( ) ;
52+ }
5153
52- const url = new URL ( request . url ) ;
54+ const url = new URL ( request . url ) ;
5355
54- if ( url . pathname === "/_next/image" ) {
55- const imageUrl =
56- url . searchParams . get ( "url" ) ?? "https://developers.cloudflare.com/_astro/logo.BU9hiExz.svg" ;
57- if ( imageUrl . startsWith ( "/" ) ) {
58- return env . ASSETS . fetch ( new URL ( imageUrl , request . url ) ) ;
59- }
60- return fetch ( imageUrl , { cf : { cacheEverything : true } } ) ;
56+ if ( url . pathname === "/_next/image" ) {
57+ const imageUrl =
58+ url . searchParams . get ( "url" ) ?? "https://developers.cloudflare.com/_astro/logo.BU9hiExz.svg" ;
59+ if ( imageUrl . startsWith ( "/" ) ) {
60+ return env . ASSETS . fetch ( new URL ( imageUrl , request . url ) ) ;
6161 }
62+ return fetch ( imageUrl , { cf : { cacheEverything : true } } ) ;
63+ }
6264
63- const { req, res, webResponse } = getWrappedStreams ( request , ctx ) ;
65+ const { req, res, webResponse } = getWrappedStreams ( request , ctx ) ;
6466
65- ctx . waitUntil ( Promise . resolve ( requestHandler ( new NodeNextRequest ( req ) , new NodeNextResponse ( res ) ) ) ) ;
67+ ctx . waitUntil ( Promise . resolve ( requestHandler ( new NodeNextRequest ( req ) , new NodeNextResponse ( res ) ) ) ) ;
6668
67- return await webResponse ( ) ;
68- } ) ;
69- }
70- ) ;
69+ return await webResponse ( ) ;
70+ } ) ;
71+ } ) ;
7172 } ,
7273} as ExportedHandler < { ASSETS : Fetcher } > ;
7374
0 commit comments