Vite dependency optimization re-evaluates server modules during in-flight requests
Astro Info
Astro v7.3.5
Node v24.19.0
System macOS (arm64)
Package Manager pnpm
Output server
Adapter @astrojs/cloudflare
Integrations noneIf this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
After switching from astro 5 to workers and astro 7, we started seeing a lot of issues with local development and optimize deps. This is one of the issues we have been able to reliably reproduce so far.
During development, Vite can discover and optimize a new dependency while a server request is already being processed. This can happen with a dynamic import or when calling an Astro action directly from an .astro file.
The request then spans two module generations.
In the reproduction, middleware stores a request ID using AsyncLocalStorage. After dependency optimization reloads the server, later code sees a new module instance and the request context is lost.
The reproduction also demonstrates the same behavior with a plain exported object: a value stored by middleware is missing after optimization. This shows that the issue is caused by module re-evaluation, not AsyncLocalStorage itself.
We are seeing this specifically when using the Cloudflare plugin.
What's the expected result?
Any context or in-memory state set before optimization should still be available afterward.
Link to Minimal Reproducible Example
https://github.com/TorbjornHoltmon/vite-cloudflare-optimize-deps-repro
Participation
- I am willing to submit a pull request for this issue.
Source: withastro/astro