Skip to content

Vite dependency optimization re-evaluates server modules during in-flight requests #18130

Description

@TorbjornHoltmon

Astro Info

Astro                    v7.3.5
Node                     v24.19.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   server
Adapter                  @astrojs/cloudflare
Integrations             none

If 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P4: importantViolate documented behavior or significantly impacts performance (priority)pkg: astroRelated to the core `astro` package (scope)pkg: cloudflareRelated to the Cloudflare adaptertriage: fix verifiedReporter confirmed the triage bot fix works

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions