diff --git a/.changeset/prelude-esbuild-only.md b/.changeset/prelude-esbuild-only.md new file mode 100644 index 00000000..70ad8902 --- /dev/null +++ b/.changeset/prelude-esbuild-only.md @@ -0,0 +1,5 @@ +--- +"@cloudhal/cloudflare": patch +--- + +Docs: `workerd-prelude` is only for entries that wrangler bundles with esbuild, such as the admin entry from `cloudhal deploy`. An entry that Astro's Vite build bundles, such as a playground, must not import it: with Astro 7.4.13 its top-level await deadlocks the Worker at startup (#392). diff --git a/docs/adr/0012-cloudflare-deployment.md b/docs/adr/0012-cloudflare-deployment.md index cf79b64c..6263a166 100644 --- a/docs/adr/0012-cloudflare-deployment.md +++ b/docs/adr/0012-cloudflare-deployment.md @@ -28,7 +28,7 @@ Sprint 6 puts a Cloudhal site on Cloudflare. Publishing and rollback must be ins ## Consequences - Positive: agent → approval → live in one request; rollback is instant; previews are safe; the whole loop is tested on workerd in CI. -- Negative: on-demand SSR on cache misses (bounded by the edge cache); two Workers to deploy; the generated admin entry needs `@cloudhal/cloudflare/workerd-prelude` first — esbuild does not initialise lazily-wrapped zod before `@modelcontextprotocol/core` evaluates (remove when fixed upstream). +- Negative: on-demand SSR on cache misses (bounded by the edge cache); two Workers to deploy; the generated admin entry needs `@cloudhal/cloudflare/workerd-prelude` first — esbuild does not initialise lazily-wrapped zod before `@modelcontextprotocol/core` evaluates (remove when fixed upstream). Entries that Vite/Rollup bundles (the Astro site Worker, a playground) must not import it: Rollup orders zod correctly, and from Astro 7.4.13 the prelude's top-level `await import()` can form a chunk cycle that stops the Worker at startup (#392, 2026-12-03). - Risks: Workers Cache purge rate limits are the Free-tier limits — purges are per publish, not per edit (and at most three tag requests per publish, see the addendum). Enabling D1 read replication later requires `withSession("first-primary")` for the pointer read. Real deployment was not exercised from CI (no account); the dry run bundles both Workers with the real wrangler. ## Addendum (2026-10-08): custom domains diff --git a/packages/cloudflare/README.md b/packages/cloudflare/README.md index d558854b..d659d493 100644 --- a/packages/cloudflare/README.md +++ b/packages/cloudflare/README.md @@ -22,6 +22,8 @@ export default createAdminWorker({ skills }); | `CLOUDHAL_AGENT_UPLOADS`, `CLOUDHAL_AGENT_UPLOAD_BYTES` | `off` turns agents' upload links off; their cap per file in bytes | | `CLOUDHAL_SIGNUP` | `first-user` (default — only the first account, the owner, can sign up), `closed` or `open` | +`workerd-prelude` is only for entries that wrangler bundles with esbuild, like the generated admin entry above. An entry that Astro's Vite build bundles (the site Worker, a playground) must not import it: Rollup orders zod correctly, and with Astro 7.4.13 the prelude's top-level `await import()` forms a chunk cycle that stops the Worker at startup (#392). + The site Worker itself is the project's Astro build (`@cloudhal/astro-renderer/cloudflare-entry`). Both are deployed, configured and tested by `@cloudhal/cli` (`cloudhal deploy`, `test/cloudflare*.test.ts` on workerd). ## Playground Worker (RFC-0036) @@ -29,8 +31,7 @@ The site Worker itself is the project's Astro build (`@cloudhal/astro-renderer/c `@cloudhal/cloudflare/playground` runs a public playground: every visitor gets a disposable Cloudhal in their own SQLite-backed Durable Object (`PlaygroundSite`), seeded on first use and deleted an hour after the last request (two hours at most), or at once on Reset. ```ts -// worker.ts in the deploying repo (e.g. cloudhal-dev/try) -import "@cloudhal/cloudflare/workerd-prelude"; +// worker.ts in the deploying repo (e.g. cloudhal-dev/try), bundled by Astro's Vite build import { handle } from "@astrojs/cloudflare/handler"; import { serveMedia, withRequestStore } from "@cloudhal/astro-renderer/runtime"; import { createPlaygroundWorker } from "@cloudhal/cloudflare/playground"; diff --git a/packages/cloudflare/src/workerd-prelude.ts b/packages/cloudflare/src/workerd-prelude.ts index 1d76232d..be8d52c7 100644 --- a/packages/cloudflare/src/workerd-prelude.ts +++ b/packages/cloudflare/src/workerd-prelude.ts @@ -7,6 +7,11 @@ * Worker then fails at startup with "ZodLazy is not a constructor". Awaiting a dynamic import here * forces the initializer to run before any later module in the bundle evaluates. * Remove once the bundler orders wrapped modules correctly (ADR-0012). + * + * Only for entries that wrangler bundles with esbuild (the admin entry `cloudhal deploy` writes). + * Never import it from an entry that Vite/Rollup bundles (an Astro build, a playground): Rollup + * orders zod correctly, and it may put zod in a chunk that imports the entry chunk back, so this + * top-level await never settles and the Worker fails at startup (#392). */ const z = await import("zod/v4");