@@ -11,53 +11,61 @@ import { createALSProxy } from "./utils";
1111
1212const NON_BODY_RESPONSES = new Set ( [ 101 , 204 , 205 , 304 ] ) ;
1313
14+ const processEnvALS = new AsyncLocalStorage < Record < string , unknown > > ( ) ;
1415const cloudflareContextALS = new AsyncLocalStorage < CloudflareContext > ( ) ;
1516
1617// Note: this symbol needs to be kept in sync with the one defined in `src/api/get-cloudflare-context.ts`
1718// eslint-disable-next-line @typescript-eslint/no-explicit-any
1819( globalThis as any ) [ Symbol . for ( "__cloudflare-context__" ) ] = createALSProxy ( cloudflareContextALS ) ;
1920
21+ globalThis . process = {
22+ ...globalThis . process ,
23+ // @ts -expect-error - populated when we run inside the ALS context
24+ env : createALSProxy ( processEnvALS ) ,
25+ } ;
26+
2027// Injected at build time
2128const nextConfig : NextConfig = JSON . parse ( process . env . __NEXT_PRIVATE_STANDALONE_CONFIG ?? "{}" ) ;
2229
2330let requestHandler : NodeRequestHandler | null = null ;
2431
2532export default {
2633 async fetch ( request , env , ctx ) {
27- return cloudflareContextALS . run ( { env , ctx , cf : request . cf } , async ( ) => {
28- if ( requestHandler == null ) {
29- globalThis . process . env = { ... globalThis . process . env , ... env } ;
30- // Note: "next/dist/server/next-server" is a cjs module so we have to `require` it not to confuse esbuild
31- // (since esbuild can run in projects with different module resolutions)
32- // eslint-disable-next-line @typescript-eslint/no-require-imports
33- const NextNodeServer = require ( "next/dist/server/next-server" )
34- . default as typeof import ( "next/dist/server/next-server" ) . default ;
35-
36- requestHandler = new NextNodeServer ( {
37- conf : nextConfig ,
38- customServer : false ,
39- dev : false ,
40- dir : "" ,
41- minimalMode : false ,
42- } ) . getRequestHandler ( ) ;
43- }
34+ return processEnvALS . run ( { NODE_ENV : "production" , ... env } , ( ) => {
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+ }
4451
45- const url = new URL ( request . url ) ;
52+ const url = new URL ( request . url ) ;
4653
47- if ( url . pathname === "/_next/image" ) {
48- const imageUrl =
49- url . searchParams . get ( "url" ) ?? "https://developers.cloudflare.com/_astro/logo.BU9hiExz.svg" ;
50- if ( imageUrl . startsWith ( "/" ) ) {
51- return env . ASSETS . fetch ( new URL ( imageUrl , request . url ) ) ;
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 } } ) ;
5261 }
53- return fetch ( imageUrl , { cf : { cacheEverything : true } } ) ;
54- }
5562
56- const { req, res, webResponse } = getWrappedStreams ( request , ctx ) ;
63+ const { req, res, webResponse } = getWrappedStreams ( request , ctx ) ;
5764
58- ctx . waitUntil ( Promise . resolve ( requestHandler ( new NodeNextRequest ( req ) , new NodeNextResponse ( res ) ) ) ) ;
65+ ctx . waitUntil ( Promise . resolve ( requestHandler ( new NodeNextRequest ( req ) , new NodeNextResponse ( res ) ) ) ) ;
5966
60- return await webResponse ( ) ;
67+ return await webResponse ( ) ;
68+ } ) ;
6169 } ) ;
6270 } ,
6371} as ExportedHandler < { ASSETS : Fetcher } > ;
0 commit comments