Repository navigation
Description
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
astro-factory commentedon Sep 14, 2026
Triage complete
triage: unable to reproduce@astrojs/cloudflareadapter withplatformProxy: { enabled: true }. The reporter confirms it doesn't crash withoutplatformProxy, providing a workaround. The root cause appears to be in@cloudflare/vite-pluginrather 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/cloudflarecould potentially mitigate this by pre-declaring known SSR dependencies inoptimizeDeps.includeto 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(whereoptimizeDepsis configured), andpackages/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 devon 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:
require is not defined— a CJS-only package (like picomatch) gets served into workerd completely untransformedUnexpected token 'export'— same idea, but a raw ESM fileThe file does not exist at ".../node_modules/.vite/deps_ssr/base-Co5EOvYG.js?v=8f3bad24"— stale-hash flavor with different hash on every runReporter's Environment
Key Configuration
platformProxy: { enabled: true }in cloudflare adapter configwrangler.jsoncpresent with KV namespace bindingsReproduction 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
.vitecache and.astrodirectory. Server started successfully in ~9.2 seconds. Page loaded correctly. No crash, no errors.Why It Likely Didn't Reproduce
astroand@astrojs/cloudflare). The reporter's actual project uses many more integrations, widening the race window.Observations
[optimizer] bundling dependencies...)[vite] connected.messages appear, suggesting both client and workerd SSR environments connect to the Vite server@astrojs/cloudflare/entrypoints/serverandastrobeing optimizedPotentially Relevant Code Areas
@cloudflare/vite-plugin(external) — how the workerd module runner handles dep optimizer invalidationpackages/integrations/cloudflare/src/— how@astrojs/cloudflareconfigures the Cloudflare vite pluginpackages/astro/src/core/create-vite.ts— whereoptimizeDepsentries are configuredpackages/astro/src/vite-plugin-environment/index.ts— setsoptimizeDeps.entriesThis 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.
matthewp commentedon Sep 14, 2026
Which dependencies is it optimizing?
J-Klakus commentedon Sep 15, 2026
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.