Middleware Unable to Access Updated Environment Variables Set Asynchronously in next.config.js #69409
Labels
bug
Issue was opened via the bug report template.
Middleware
Related to Next.js Middleware.
Runtime
Related to Node.js or Edge Runtime with Next.js.
Link to the code that reproduces this issue
To Reproduce
Open the Sandbox:
Start the Application:
Observe Console Logs:
updateEnvVars
function innext.config.js
will run asynchronously, and you'll see its console logs in the terminal.Make a Request to Trigger Middleware:
http://localhost:3000
).Check the Terminal Output:
Compare Results:
process.env.ENV_WITH_CONFIGURE_NEXT
isundefined
despite being set innext.config.js
after an asynchronous operation. This shows the environment variable is not accessible in the middleware context.Current vs. Expected behavior
Current Behavior:
updateEnvVars
function innext.config.js
is executed asynchronously with a delay.process.env.ENV_WITH_INSTRUMENTATION
is'true'
.process.env.ENV_WITHOUT_CONFIGURE_NEXT
is'true'
.process.env.ENV_WITH_CONFIGURE_NEXT
isundefined
.process.env.ENV_WITH_INSTRUMENTATION
andprocess.env.ENV_WITHOUT_CONFIGURE_NEXT
are correctly logged, the environment variableprocess.env.ENV_WITH_CONFIGURE_NEXT
, which is set asynchronously innext.config.js
, is not accessible in the middleware.Expected Behavior:
next.config.js
, including those set asynchronously, should be available.process.env.ENV_WITH_INSTRUMENTATION
as'true'
.process.env.ENV_WITHOUT_CONFIGURE_NEXT
as'true'
.process.env.ENV_WITH_CONFIGURE_NEXT
as'true'
, indicating it has been set after the asynchronous operation completes.process.env.ENV_WITH_CONFIGURE_NEXT
should not beundefined
; it should reflect its updated value after being set asynchronously innext.config.js
.Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 23.1.0: Mon Oct 9 21:28:45 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6020 Binaries: Node: 18.17.0 npm: 9.6.7 Yarn: 1.22.22 pnpm: 8.14.3 Relevant Packages: next: 13.5.6 eslint-config-next: 13.4.12 react: 18.2.0 react-dom: 18.2.0 typescript: 5.1.6 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Middleware, Runtime
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local), next start (local)
Additional context
No response
The text was updated successfully, but these errors were encountered: