Dev startup triggers heavy internal typegen + SSR dep optimizer race on Cloudflare, causing chunk errors. #1753
khoinguyenpham04
announced in
Roadmap
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
This Roadmap discussion mirrors #484: Dev startup triggers heavy internal typegen + SSR dep optimizer race on Cloudflare, causing chunk errors..
Use this discussion to upvote the roadmap item and discuss priority, use cases, and product feedback. Keep implementation tracking, reproduction details, and PR-specific feedback on the source issue.
bug,area/core,roadmap/1.0,roadmap/cloudflareOriginal Issue
Description
On
astro devwith the Cloudflare adapter andemdash()integration, EmDash performs an internalPOST /_emdash/api/typegenimmediately after the dev server starts listening.That request is not lightweight. It goes through the full EmDash middleware/runtime path, initializes the runtime, builds the admin manifest, and triggers additional SSR dependency optimization on first access.
There are two interrelated issues at play.
server.httpServer.on('listening', ...)insideastro:server:setup, but at that point Vite's SSR dependency optimization may not have completed yet. Vite lazily optimizes SSR dependencies on first access, so the typegen → middleware → runtime chain pulls in a large number of SSR dependencies all at once, causing a race between optimization and request handling. This is what produces the chunk-*.js not found errors.This makes the dev server feel very slow and sometimes temporarily unusable right after startup.
Current behavior
In astro:server:setup, EmDash fetches
/_emdash/api/typegenas soon as the server is listening:emdash/packages/core/src/astro/integration/index.ts
Lines 282 to 301 in c92e7e6
The typegen endpoint requires
locals.emdash.db, so it goes through EmDash middleware first:emdash/packages/core/src/astro/routes/api/typegen.ts
Line 73 in c92e7e6
The middleware does
getRuntimeandgetManifestonsrc/astro/middleware.tsemdash/packages/core/src/astro/middleware.ts
Lines 130 to 157 in c92e7e6
After runtime init, getManifest() reads collections and fields again from the DB:
emdash/packages/core/src/emdash-runtime.ts
Lines 1143 to 1364 in c92e7e6
Then the typegen endpoint reads collections/fields again to generate TS types:
emdash/packages/core/src/astro/routes/api/typegen.ts
Lines 37 to 55 in c92e7e6
So the same internal request does both manifest schema loading and typegen schema loading.
Also, even though EmDash configures ssr.optimizeDeps.include, in my cold-start logs Vite still optimized these later, one by one:
This seems to be the unstable part that causes the deps_ssr missing-file failures during reload.
Steps to reproduce
just start
astro devEnvironment
6.1.50.1.1@astrojs/cloudflare:13.1.8@emdash-cms/cloudflare:0.1.1Logs / error output
The file does not exist at ".../node_modules/.vite/deps_ssr/chunk-*.js" The dependency might be incompatible with the dep optimizer. Try adding it to optimizeDeps.exclude.Suggested Change
Instead of
POST /_emdash/api/typegenthrough the full middleware stack, call the typegen logic directly inastro:server:setup:This eliminates the middleware overhead and avoids triggering SSR dep optimization for middleware-related modules (
emdash/middleware/*, etc.) at startup.All reactions