@@ -3,10 +3,6 @@ import { AsyncLocalStorage } from "node:async_hooks";
33import type { CloudflareContext } from "../../api" ;
44// @ts -expect-error: resolved by wrangler build
55import * as nextEnvVars from "./env/next-env.mjs" ;
6- // @ts -expect-error: resolved by wrangler build
7- import { handler as middlewareHandler } from "./middleware/handler.mjs" ;
8- // @ts -expect-error: resolved by wrangler build
9- import { handler as serverHandler } from "./server-functions/default/handler.mjs" ;
106
117const cloudflareContextALS = new AsyncLocalStorage < CloudflareContext > ( ) ;
128
@@ -30,7 +26,7 @@ export default {
3026 return cloudflareContextALS . run ( { env, ctx, cf : request . cf } , async ( ) => {
3127 const url = new URL ( request . url ) ;
3228
33- populateProcessEnv ( url , env . NEXTJS_ENV ) ;
29+ populateProcessEnv ( url , env ) ;
3430
3531 // Serve images in development.
3632 // Note: "/cdn-cgi/image/..." requests do not reach production workers.
@@ -42,45 +38,44 @@ export default {
4238 const imageUrl = m . groups ! . url ! ;
4339 return imageUrl . match ( / ^ h t t p s ? : \/ \/ / )
4440 ? fetch ( imageUrl , { cf : { cacheEverything : true } } )
45- : env . ASSETS . fetch ( new URL ( `/${ imageUrl } ` , url ) ) ;
41+ : env . ASSETS ? .fetch ( new URL ( `/${ imageUrl } ` , url ) ) ;
4642 }
4743
4844 // Fallback for the Next default image loader.
4945 if ( url . pathname === "/_next/image" ) {
5046 const imageUrl = url . searchParams . get ( "url" ) ?? "" ;
5147 return imageUrl . startsWith ( "/" )
52- ? env . ASSETS . fetch ( new URL ( imageUrl , request . url ) )
48+ ? env . ASSETS ? .fetch ( new URL ( imageUrl , request . url ) )
5349 : fetch ( imageUrl , { cf : { cacheEverything : true } } ) ;
5450 }
5551
56- // The Middleware handler can return either a `Response` or a `Request`:
57- // - `Response`s should be returned early
58- // - `Request`s are handled by the Next server
59- const reqOrResp = await middlewareHandler ( request , env , ctx ) ;
60-
61- if ( reqOrResp instanceof Response ) {
62- return reqOrResp ;
63- }
52+ // @ts -expect-error: resolved by wrangler build
53+ const { handler } = await import ( "./server-functions/default/handler.mjs" ) ;
6454
65- return serverHandler ( reqOrResp , env , ctx ) ;
55+ return handler ( request , env , ctx ) ;
6656 } ) ;
6757 } ,
68- } as ExportedHandler < { ASSETS : Fetcher ; NEXTJS_ENV ?: string } > ;
58+ } as ExportedHandler < CloudflareEnv > ;
6959
7060/**
7161 * Populate process.env with:
62+ * - the environment variables and secrets from the cloudflare platform
7263 * - the variables from Next .env* files
7364 * - the origin resolver information
74- *
75- * Note that cloudflare env string values are copied by the middleware handler.
7665 */
77- function populateProcessEnv ( url : URL , nextJsEnv ?: string ) {
66+ function populateProcessEnv ( url : URL , env : CloudflareEnv ) {
7867 if ( processEnvPopulated ) {
7968 return ;
8069 }
8170 processEnvPopulated = true ;
82- const mode = nextJsEnv ?? "production" ;
8371
72+ for ( const [ key , value ] of Object . entries ( env ) ) {
73+ if ( typeof value === "string" ) {
74+ process . env [ key ] = value ;
75+ }
76+ }
77+
78+ const mode = env . NEXTJS_ENV ?? "production" ;
8479 if ( nextEnvVars [ mode ] ) {
8580 for ( const key in nextEnvVars [ mode ] ) {
8681 process . env [ key ] = nextEnvVars [ mode ] [ key ] ;
0 commit comments