Skip to content

Fix Astro + Cloudflare static/prerendered builds#894

Merged
theoephraim merged 1 commit into
mainfrom
fix-astro-cf-static-prerender-build
Jul 17, 2026
Merged

Fix Astro + Cloudflare static/prerendered builds#894
theoephraim merged 1 commit into
mainfrom
fix-astro-cf-static-prerender-build

Conversation

@theoephraim

@theoephraim theoephraim commented Jul 17, 2026

Copy link
Copy Markdown
Member

Fixes #893

Problem

An Astro static build using the Cloudflare adapter fails. There are two distinct defects behind the report:

  1. Deterministic regression (since astro-integration 1.2.0 / Fix redundant resolved-env injection on Cloudflare, warn on unencrypted Vercel deploys #881). @astrojs/cloudflare v14 prerenders static/prerendered routes inside a real build-time workerd worker (@cloudflare/vite-plugin's experimental prerenderWorker, a Vite environment named prerender). Since Fix redundant resolved-env injection on Cloudflare, warn on unencrypted Vercel deploys #881 stopped baking resolved-env into Cloudflare builds, that build-time worker has no env source — no bindings, and process.env.__VARLOCK_ENV doesn't exist inside workerd — so initVarlockEnv() throws initVarlockEnv failed and any static or prerendered-route CF build fails.

  2. The reporter's REQUIRE_TLA variant (nondeterministic). The Vite plugin's entry-detection fallback (moduleIds[0] === id) can misfire under Rolldown's parallel transforms and inject the top-level-await varlock-ssr-init module into a require()-reachable module (e.g. react-dom's CJS internals), which Rolldown rejects with REQUIRE_TLA. This is why the same commit built on Linux CI but failed locally.

Fix

  • In the virtual init module, when it loads inside the Cloudflare prerender Vite environment during a build, bake the resolved env directly (globalThis.__varlockLoadedEnv) and skip the TLA bindings loader. This artifact only generates static HTML at build time and is discarded afterward (not deployed), and the generated HTML is still leak-scanned by the framework integrations. @encryptInjectedEnv is intentionally not applied there — the decryption key isn't readable inside the build-time worker.
  • Make the moduleIds[0] entry-detection fallback dev-only. During builds isEntry is authoritative, so we no longer rely on the timing-dependent heuristic that caused the REQUIRE_TLA misfire.

Tests

Adds three Cloudflare-adapter framework-test scenarios to the Astro suite:

  • static output + React (output: 'static') — the reporter's repro; a CJS dep (react-dom) in the prerender build must not receive the TLA init module (guards against REQUIRE_TLA), and env is injected into the static HTML.
  • server output + a prerendered page (output: 'server') — the production-representative case. Exercises both env paths in one build: the build-time prerender worker (env baked in) and the deployed SSR worker (runtime bindings loader, no baked env), asserting they don't cross-contaminate (SSR worker keeps its cloudflare:workers loader and bakes no secret).
  • leaky prerendered page — confirms leak protection still runs inside the now-env-baked prerender worker (a leaked secret fails the build).

Full test:astro (v5/v6/v7) plus vite, cloudflare, sveltekit, and tanstack-start suites are green.

@github-actions

github-actions Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

bumpy-frog

The changes in this PR will be included in the next version bump.

patch Patch releases

  • @varlock/astro-integration 1.2.0 → 1.2.1 (cascade)
  • @varlock/cloudflare-integration 1.3.0 → 1.3.1 (cascade)
  • @varlock/vite-integration 1.3.0 → 1.3.1

Bump files in this PR

Click here if you want to add another bump file to this PR


This comment is maintained by bumpy.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 17, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
varlock-website 31a2295 Commit Preview URL

Branch Preview URL
Jul 17 2026, 11:44 PM

@pkg-pr-new

pkg-pr-new Bot commented Jul 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

@env-spec/parser

npm i https://pkg.pr.new/@env-spec/parser@894

varlock

npm i https://pkg.pr.new/varlock@894

@varlock/astro-integration

npm i https://pkg.pr.new/@varlock/astro-integration@894

@varlock/cloudflare-integration

npm i https://pkg.pr.new/@varlock/cloudflare-integration@894

@varlock/vite-integration

npm i https://pkg.pr.new/@varlock/vite-integration@894

commit: 31a2295

@theoephraim
theoephraim force-pushed the fix-astro-cf-static-prerender-build branch from 427b7fc to c016b42 Compare July 17, 2026 23:26
@astrojs/cloudflare v14 prerenders static routes inside a build-time workerd
worker, which has no bindings and no process.env.__VARLOCK_ENV, so the runtime
env loader threw "initVarlockEnv failed" and any static or prerendered-route CF
build failed. Bake the resolved env directly into the virtual init module when
it loads in the CF 'prerender' vite environment (a build-only artifact Astro
discards after prerendering; generated HTML is still leak-scanned).

Also make the moduleIds[0] entry-detection fallback dev-only. During builds
isEntry is authoritative, and under Rolldown's parallel transforms moduleIds[0]
is timing-dependent and could inject the TLA init module into CJS-reachable
modules like react-dom, producing REQUIRE_TLA errors.
@theoephraim
theoephraim force-pushed the fix-astro-cf-static-prerender-build branch from c016b42 to 31a2295 Compare July 17, 2026 23:42
@theoephraim
theoephraim merged commit 4e37ef7 into main Jul 17, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Astro static build fails with REQUIRE_TLA from varlock-ssr-init

1 participant