@@ -116,14 +116,18 @@ async function loadWasm(
116116}
117117
118118function buildEnvironmentVariablesFrom (
119- injectedEnvironments : Record < string , string >
119+ injectedEnvironments : Record < string , string > ,
120+ relativeProjectDir ?: string
120121) : Record < string , string | undefined > {
121- const pairs = Object . keys ( process . env ) . map ( ( key ) => [ key , process . env [ key ] ] )
122- const env = Object . fromEntries ( pairs )
123- for ( const key of Object . keys ( injectedEnvironments ) ) {
124- env [ key ] = injectedEnvironments [ key ]
122+ let env = Object . fromEntries ( [
123+ ...Object . entries ( process . env ) ,
124+ ...Object . entries ( injectedEnvironments ) ,
125+ [ 'NEXT_RUNTIME' , 'edge' ] ,
126+ ] )
127+ if ( process . env . NODE_ENV !== 'production' && relativeProjectDir ) {
128+ // Needed for accessing routerServerGlobal in dev
129+ env . __NEXT_RELATIVE_PROJECT_DIR = relativeProjectDir
125130 }
126- env . NEXT_RUNTIME = 'edge'
127131 return env
128132}
129133
@@ -135,8 +139,13 @@ Learn more: https://nextjs.org/docs/api-reference/edge-runtime`)
135139 throw error
136140}
137141
138- function createProcessPolyfill ( env : Record < string , string > ) {
139- const processPolyfill = { env : buildEnvironmentVariablesFrom ( env ) }
142+ function createProcessPolyfill (
143+ env : Record < string , string > ,
144+ relativeProjectDir ?: string
145+ ) {
146+ const processPolyfill = {
147+ env : buildEnvironmentVariablesFrom ( env , relativeProjectDir ) ,
148+ }
140149 const overriddenValue : Record < string , any > = { }
141150
142151 for ( const key of Object . keys ( process ) ) {
@@ -268,7 +277,10 @@ async function createModuleContext(options: ModuleContextOptions) {
268277 ? { strings : true , wasm : true }
269278 : undefined ,
270279 extend : ( context ) => {
271- context . process = createProcessPolyfill ( edgeFunctionEntry . env )
280+ context . process = createProcessPolyfill (
281+ edgeFunctionEntry . env ,
282+ options . relativeProjectDir
283+ )
272284
273285 Object . defineProperty ( context , 'require' , {
274286 enumerable : false ,
@@ -491,6 +503,7 @@ interface ModuleContextOptions {
491503 onWarning : ( warn : Error ) => void
492504 useCache : boolean
493505 distDir : string
506+ relativeProjectDir : string
494507 edgeFunctionEntry : Pick < EdgeFunctionDefinition , 'assets' | 'wasm' | 'env' >
495508}
496509
0 commit comments