Skip to content

@astrojs/cloudflare dev server crashes nondeterministically while Vite dep optimizer is bundling #17990

Closed

Description

@J-Klakus

Astro Info

Astro                    v7.3.2
Node                     v22.23.1
System                   Windows (x64)
Package Manager          pnpm
Output                   static
Adapter                  @astrojs/cloudflare (v14.3.1)
Integrations             keystatic
                         @astrojs/react (v6.0.1)
                         @astrojs/sitemap (v3.7.3)
                         auto-import
                         @astrojs/mdx (v7.0.2)

If this issue only occurs in one browser, which browser is a problem?

N/A, server-side crash during dev startup.

Describe the Bug

When I run astro dev on a project using @astrojs/cloudflare, the dev server crashes on startup almost every time. It dies right after the Using secrets defined in .dev.vars / Enabling sessions... lines, while the worker entry is first being evaluated.

The underlying problem seems to be a startup race inside @cloudflare/vite-plugin: its workerd dev-runner starts evaluating the worker (virtual:cloudflare/worker-entry) while Vite's dependency optimizer is still scanning/bundling. The runner caches transformed modules using temporary hashes, and when the optimizer finishes it writes different filenames — so the runner then asks for files that never existed. Depending on which module loses the race, I get one of three errors:

require is not defined
(a CJS-only package like picomatch gets served into workerd completely untransformed)

Unexpected token 'export'
(same idea, but a raw ESM file)

The file does not exist at ".../node_modules/.vite/deps_ssr/base-Co5EOvYG.js?v=8f3bad24"

(the stale-hash flavor — the hash is different on every run, which is what makes me think it's a race rather than a config problem)

I built a minimal one-page repro (linked below). With platformProxy.enabled: true and a wrangler.jsonc present it crashes 2 out of 2 cold starts; without platformProxy it boots fine. Our real production site crashes either way.

Things I ruled out before concluding it's not my setup:

  • Dependency versions — the exact lockfile combo that worked before crashes too, as does the fully-updated stack
  • Stale cache — deleting .vite and restarting warm/cold makes no difference
  • Stray processes / port conflicts — checked, none
  • optimizeDeps.include — pre-declaring the deps only stops the second optimizer pass; the first evaluation still crashes

What's the expected result?

astro dev boots;

Link to Minimal Reproducible Example

https://github.com/JojokCreator/astro-cloudflare-dev-crash

Participation

  • I am willing to submit a pull request for this issue.

Activity

  1. added
    triage: in progress
    on Sep 14, 2026
  2. astro-factory commented on Sep 14, 2026

    @astro-factory
    Contributor

    Triage complete

    • Prepare the workspace: ready
    • Install dependencies: complete
    • Build the project: complete
    • Reproduce the issue: not reproduced
    • Diagnose the cause: not needed (the issue was not reproduced)
    • Verify the diagnosis: not needed (the issue was not reproduced)
    • Attempt a fix: not needed (the issue was not reproduced)
    • Publish the result: set triage: unable to reproduce

    • Reproduced: No — the race condition did not trigger in our Linux CI environment across two clean cold-start attempts.
    • Exploration: No — View branch
    • Unit Test: No — could not reproduce the failure to write a test against.
    • Priority: P3: minor bug. This is a nondeterministic race condition specific to the @astrojs/cloudflare adapter with platformProxy: { enabled: true }. The reporter confirms it doesn't crash without platformProxy, providing a workaround. The root cause appears to be in @cloudflare/vite-plugin rather than Astro itself.

    The reported crash is a timing-dependent race between @cloudflare/vite-plugin's workerd dev-runner and Vite's dependency optimizer. The workerd runner begins evaluating the worker entry while the optimizer is still scanning/bundling, and when the optimizer finishes it writes files with different hashes — causing the runner to request files that no longer exist (or receive untransformed CJS/ESM modules). The race is more likely to trigger with many dependencies (the reporter's full project uses keystatic, react, sitemap, mdx, etc.), on Windows, or under different I/O timing conditions.

    I was unable to determine the cause of this issue. The root cause likely lives in @cloudflare/vite-plugin (how it handles dep optimizer invalidation mid-evaluation in the workerd module runner), which is outside this repository. However, @astrojs/cloudflare could potentially mitigate this by pre-declaring known SSR dependencies in optimizeDeps.include to reduce the optimizer's discovery window — though the reporter notes this "only stops the second optimizer pass." Relevant areas in the Astro codebase: packages/integrations/cloudflare/src/, packages/astro/src/core/create-vite.ts (where optimizeDeps is configured), and packages/astro/src/vite-plugin-environment/index.ts.

    Full Triage Report

    Issue Details

    Author: @JojokCreator
    Title: @astrojs/cloudflare dev server crashes nondeterministically while Vite dep optimizer is bundling

    Description

    When running astro dev on a project using @astrojs/cloudflare, the dev server crashes on startup almost every time. It dies right after the "Using secrets defined in .dev.vars / Enabling sessions..." lines, while the worker entry is first being evaluated.

    The underlying problem is described as a startup race inside @cloudflare/vite-plugin: its workerd dev-runner starts evaluating the worker (virtual:cloudflare/worker-entry) while Vite's dependency optimizer is still scanning/bundling. The runner caches transformed modules using temporary hashes, and when the optimizer finishes it writes different filenames — so the runner then asks for files that never existed.

    Error Variants

    The reporter sees one of three errors depending on which module loses the race:

    1. require is not defined — a CJS-only package (like picomatch) gets served into workerd completely untransformed
    2. Unexpected token 'export' — same idea, but a raw ESM file
    3. The file does not exist at ".../node_modules/.vite/deps_ssr/base-Co5EOvYG.js?v=8f3bad24" — stale-hash flavor with different hash on every run

    Reporter's Environment

    Astro                    v7.3.2
    Node                     v22.23.1
    System                   Windows (x64)
    Package Manager          pnpm
    Output                   static
    Adapter                  @astrojs/cloudflare (v14.3.1)
    Integrations             keystatic, @astrojs/react, @astrojs/sitemap, auto-import, @astrojs/mdx
    

    Key Configuration

    • platformProxy: { enabled: true } in cloudflare adapter config
    • wrangler.jsonc present with KV namespace bindings
    • Reporter says: "without platformProxy it boots fine"

    Reproduction Attempts

    Attempt 1 (Cold Start): Server started successfully in ~9.6 seconds. curl http://localhost:4321/ returned the expected HTML. No crash, no errors.

    Attempt 2 (Clean Cold Start): Deleted .vite cache and .astro directory. Server started successfully in ~9.2 seconds. Page loaded correctly. No crash, no errors.

    Why It Likely Didn't Reproduce

    1. Nondeterministic race condition: The race depends on system timing, CPU load, and I/O speed. Our Linux environment has different timing characteristics than the reporter's Windows system.
    2. Minimal reproduction: The provided repro has very few dependencies (just astro and @astrojs/cloudflare). The reporter's actual project uses many more integrations, widening the race window.
    3. Workspace linking: Workspace-linked packages are resolved differently than registry installs, which affects how the dep optimizer handles them.
    4. Platform differences: Reporter is on Windows (x64), our sandbox is Linux (x64).

    Observations

    • The dep optimizer runs after the server starts (visible in logs as [optimizer] bundling dependencies...)
    • Two [vite] connected. messages appear, suggesting both client and workerd SSR environments connect to the Vite server
    • The SSR dep optimizer metadata shows deps like @astrojs/cloudflare/entrypoints/server and astro being optimized

    Potentially Relevant Code Areas

    • @cloudflare/vite-plugin (external) — how the workerd module runner handles dep optimizer invalidation
    • packages/integrations/cloudflare/src/ — how @astrojs/cloudflare configures the Cloudflare vite plugin
    • packages/astro/src/core/create-vite.ts — where optimizeDeps entries are configured
    • packages/astro/src/vite-plugin-environment/index.ts — sets optimizeDeps.entries

    This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.

  3. added
    triage: unable to reproduce
    and removed
    triage: in progress
    on Sep 14, 2026
  4. matthewp commented on Sep 14, 2026

    @matthewp
    Contributor

    Which dependencies is it optimizing?

  5. J-Klakus commented on Sep 15, 2026

    @J-Klakus
    Author

    From Claude. I'm not sure if it will help

    What's being optimized: essentially the entire Astro SSR runtime — 208 optimized entries in deps_ssr/_metadata.json (astro, astro/app/, astro/assets/, astro/actions/, astro > clsx/cookie/devalue/es-module-lexer/… subdeps) plus @astrojs/cloudflare/entrypoints/server and @astrojs/cloudflare/image-service-workerd, rolled up into ~34 shared chunks (base-, dist-, server-, runtime-*, …).

    But the interesting part is the sequence — from the repro's dev log with timestamps:
    10:19:51 [optimizer] bundling dependencies...
    10:19:51 dependency optimized: astro/assets/services/noop
    10:19:51 optimized dependencies changed. reloading
    10:19:52 [vite] program reload
    → crash: The file does not exist at ".../deps_ssr/dist-DvzTQe8T.js?v=ed4d8396"
    astro/assets/services/noop (the adapter's dev passthrough image service) is only discoverable while the worker entry graph is being fetched — the adapter entry imports it, so evaluating the worker entry is itself what triggers the final dep discovery. That forces a re-bundle (new browserHash) mid-evaluation, and the runner's already-cached transforms still reference the old hash.
    Proof it's the hash and not a missing file: run 2 requested dist-DvzTQe8T.js?v=ed4d8396 — that exact chunk does exist in the final _metadata.json (current browserHash: b36e4886); the ?v= is just from the previous optimizer commit. Run 3 requested base-Co5EOvYG.js, which no longer exists at all in the final bundle (final: base-xYeEGpw7.js) — chunk names also shift between commits.
    So the runner never gets invalidated/re-fetches when optimized dependencies changed fires — it keeps serving transforms keyed to a dead optimizer generation.

  6. self-assigned this
    on Sep 16, 2026
  7. added
    triage: needs triage
    and removed
    triage: unable to reproduce
    on Sep 16, 2026
  8. added
    triage: in progress
    triage: unable to reproduce
    and removed
    triage: needs triage
    triage: in progress
    on Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

triage: unable to reproduce

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions