@@ -3,10 +3,6 @@ import { AsyncLocalStorage } from "node:async_hooks";
3
3
import type { CloudflareContext } from "../../api" ;
4
4
// @ts -expect-error: resolved by wrangler build
5
5
import * 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" ;
10
6
11
7
const cloudflareContextALS = new AsyncLocalStorage < CloudflareContext > ( ) ;
12
8
@@ -30,7 +26,7 @@ export default {
30
26
return cloudflareContextALS . run ( { env, ctx, cf : request . cf } , async ( ) => {
31
27
const url = new URL ( request . url ) ;
32
28
33
- populateProcessEnv ( url , env . NEXTJS_ENV ) ;
29
+ populateProcessEnv ( url , env ) ;
34
30
35
31
// Serve images in development.
36
32
// Note: "/cdn-cgi/image/..." requests do not reach production workers.
@@ -42,45 +38,44 @@ export default {
42
38
const imageUrl = m . groups ! . url ! ;
43
39
return imageUrl . match ( / ^ h t t p s ? : \/ \/ / )
44
40
? fetch ( imageUrl , { cf : { cacheEverything : true } } )
45
- : env . ASSETS . fetch ( new URL ( `/${ imageUrl } ` , url ) ) ;
41
+ : env . ASSETS ? .fetch ( new URL ( `/${ imageUrl } ` , url ) ) ;
46
42
}
47
43
48
44
// Fallback for the Next default image loader.
49
45
if ( url . pathname === "/_next/image" ) {
50
46
const imageUrl = url . searchParams . get ( "url" ) ?? "" ;
51
47
return imageUrl . startsWith ( "/" )
52
- ? env . ASSETS . fetch ( new URL ( imageUrl , request . url ) )
48
+ ? env . ASSETS ? .fetch ( new URL ( imageUrl , request . url ) )
53
49
: fetch ( imageUrl , { cf : { cacheEverything : true } } ) ;
54
50
}
55
51
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" ) ;
64
54
65
- return serverHandler ( reqOrResp , env , ctx ) ;
55
+ return handler ( request , env , ctx ) ;
66
56
} ) ;
67
57
} ,
68
- } as ExportedHandler < { ASSETS : Fetcher ; NEXTJS_ENV ?: string } > ;
58
+ } as ExportedHandler < CloudflareEnv > ;
69
59
70
60
/**
71
61
* Populate process.env with:
62
+ * - the environment variables and secrets from the cloudflare platform
72
63
* - the variables from Next .env* files
73
64
* - the origin resolver information
74
- *
75
- * Note that cloudflare env string values are copied by the middleware handler.
76
65
*/
77
- function populateProcessEnv ( url : URL , nextJsEnv ?: string ) {
66
+ function populateProcessEnv ( url : URL , env : CloudflareEnv ) {
78
67
if ( processEnvPopulated ) {
79
68
return ;
80
69
}
81
70
processEnvPopulated = true ;
82
- const mode = nextJsEnv ?? "production" ;
83
71
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" ;
84
79
if ( nextEnvVars [ mode ] ) {
85
80
for ( const key in nextEnvVars [ mode ] ) {
86
81
process . env [ key ] = nextEnvVars [ mode ] [ key ] ;
0 commit comments