Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware Unable to Access Updated Environment Variables Set Asynchronously in next.config.js #69409

Open
muradsofi opened this issue Aug 28, 2024 · 1 comment
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.

Comments

@muradsofi
Copy link

muradsofi commented Aug 28, 2024

Link to the code that reproduces this issue

To Reproduce

  1. Open the Sandbox:

    • Go to the provided CodeSandbox link containing the reproduction code.
  2. Start the Application:

    • Start the Next.js development server by running:
      npm run dev
    • This will launch the application in development mode.
  3. Observe Console Logs:

    • After the server starts, the updateEnvVars function in next.config.js will run asynchronously, and you'll see its console logs in the terminal.
    • Wait for the server to finish starting up and loading the configurations.
  4. Make a Request to Trigger Middleware:

    • Open your browser and navigate to any page of the application (e.g., http://localhost:3000).
    • This action will trigger the middleware function.
  5. Check the Terminal Output:

    • Look at the terminal where your development server is running. You should see the output of the console logs from the middleware:
      process.env.ENV_WITH_INSTRUMENTATION: true
      process.env.ENV_WITHOUT_CONFIGURE_NEXT: true
      process.env.ENV_WITH_CONFIGURE_NEXT: undefined
  6. Compare Results:

    • Notice that process.env.ENV_WITH_CONFIGURE_NEXT is undefined despite being set in next.config.js after an asynchronous operation. This shows the environment variable is not accessible in the middleware context.

Current vs. Expected behavior

Current Behavior:

  • After starting the application, the updateEnvVars function in next.config.js is executed asynchronously with a delay.
  • When making a request to trigger the middleware, the console logs show:
    • process.env.ENV_WITH_INSTRUMENTATION is 'true'.
    • process.env.ENV_WITHOUT_CONFIGURE_NEXT is 'true'.
    • process.env.ENV_WITH_CONFIGURE_NEXT is undefined.
  • This indicates that while process.env.ENV_WITH_INSTRUMENTATION and process.env.ENV_WITHOUT_CONFIGURE_NEXT are correctly logged, the environment variable process.env.ENV_WITH_CONFIGURE_NEXT, which is set asynchronously in next.config.js, is not accessible in the middleware.

Expected Behavior:

  • When the application runs and the middleware is triggered, all environment variables set or modified in next.config.js, including those set asynchronously, should be available.
  • The expected console log output in the middleware should be:
    • 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.
  • The environment variable process.env.ENV_WITH_CONFIGURE_NEXT should not be undefined; it should reflect its updated value after being set asynchronously in next.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

@muradsofi muradsofi added the bug Issue was opened via the bug report template. label Aug 28, 2024
@github-actions github-actions bot added Middleware Related to Next.js Middleware. Runtime Related to Node.js or Edge Runtime with Next.js. labels Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

No branches or pull requests

2 participants
@muradsofi and others