Repository: next.js
Issue: Stale environment variable values when accessing from middleware
Labels: bug,Middleware,Runtime

### Link to the code that reproduces this issue

[Code sandbox](https://codesandbox.io/p/devbox/twilight-fast-prfv2k?file=%2Fmiddleware.ts%3A7%2C1&layout=%257B%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522rootPanelGroup%2522%253A%257B%2522direction%2522%253A%2522horizontal%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522id%2522%253A%2522ROOT_LAYOUT%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522clxjaf5dj00063b6nd4fvdohf%2522%252C%2522sizes%2522%253A%255B70%252C30%255D%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522EDITOR%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522id%2522%253A%2522clxjaf5di00023b6nuiw2qb5u%2522%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522SHELLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522id%2522%253A%2522clxjaf5dj00043b6nfrdiymmw%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522DEVTOOLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522id%2522%253A%2522clxjaf5dj00053b6nv9dtn0h4%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%252C%2522sizes%2522%253A%255B50%252C50%255D%257D%252C%2522tabbedPanels%2522%253A%257B%2522clxjaf5di00023b6nuiw2qb5u%2522%253A%257B%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522clxjaf5di00013b6n8jc832h0%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252FREADME.md%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%252C%257B%2522id%2522%253A%2522clxjajdab00023b6l5mymnv4l%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522FILE%2522%252C%2522initialSelections%2522%253A%255B%257B%2522startLineNumber%2522%253A7%252C%2522startColumn%2522%253A1%252C%2522endLineNumber%2522%253A7%252C%2522endColumn%2522%253A1%257D%255D%252C%2522filepath%2522%253A%2522%252Fmiddleware.ts%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%255D%252C%2522id%2522%253A%2522clxjaf5di00023b6nuiw2qb5u%2522%252C%2522activeTabId%2522%253A%2522clxjajdab00023b6l5mymnv4l%2522%257D%252C%2522clxjaf5dj00053b6nv9dtn0h4%2522%253A%257B%2522id%2522%253A%2522clxjaf5dj00053b6nv9dtn0h4%2522%252C%2522tabs%2522%253A%255B%257B%2522type%2522%253A%2522UNASSIGNED_PORT%2522%252C%2522port%2522%253A3000%252C%2522id%2522%253A%2522clxjaikkc009h3b6nz96msdxm%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522path%2522%253A%2522%252F%2522%257D%255D%252C%2522activeTabId%2522%253A%2522clxjaikkc009h3b6nz96msdxm%2522%257D%252C%2522clxjaf5dj00043b6nfrdiymmw%2522%253A%257B%2522id%2522%253A%2522clxjaf5dj00043b6nfrdiymmw%2522%252C%2522activeTabId%2522%253A%2522clxjaqb4j00463b6l4wl2a573%2522%252C%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522clxjaf5dj00033b6n2hwwvrak%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522TASK_LOG%2522%252C%2522taskId%2522%253A%2522dev%2522%257D%252C%257B%2522id%2522%253A%2522clxjaqb4j00463b6l4wl2a573%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522TERMINAL%2522%252C%2522shellId%2522%253A%2522clxjaqb900059dbgccxpo4c49%2522%257D%255D%257D%257D%252C%2522showDevtools%2522%253Atrue%252C%2522showShells%2522%253Atrue%252C%2522showSidebar%2522%253Atrue%252C%2522sidebarPanelSize%2522%253A15%257D)

### To Reproduce

- Define an environment variable on `.env.local`
- Access it on `middleware.ts`, log it to the console
- Access it on `page.ts` (must be a server component), log it to the console 
- Refresh page, see the logs 
- Update env var on `.env.local`
- Refresh page, see the logs. Verify that the values aren't matching 👀 

![CleanShot 2024-06-17 at 15 10 50](https://github.com/vercel/next.js/assets/48022589/24257e1a-0fbe-4fdc-8e49-c6789d70bb26)


### Current vs. Expected behavior

**Current behavior:** Environment variables get stale on the middleware side when updating env var, as the value differs from the latest that can be accessed on the application side. 

**Expected behavior:** Values must change on both sides, both being fresh. Currently, we're facing an issue, on this [PR](https://github.com/clerk/javascript/pull/3525#issuecomment-2173898950), where the same env var value is used for encryption on the middleware runtime and decryption on the application runtime, therefore those must be in sync, especially when developing locally. 

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:59 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6030
  Available memory (MB): 18432
  Available CPU cores: 11
Binaries:
  Node: 20.12.2
  npm: 10.5.0
  Yarn: 1.22.22
  pnpm: 9.2.0
Relevant Packages:
  next: 14.2.4 // Latest available version is detected (14.2.4).
  eslint-config-next: 14.2.4
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
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)

### Additional context

_No response_