Ran
All writing

Published  · 6 min read

Fully static, even with an admin: a Durable Object redeploys the Worker after every save

A site with an admin usually runs a Worker and queries the database on every page view, so heavy traffic blows through the free quota. We moved page rendering from view time to save time: on each save, a Durable Object renders the site and redeploys its own Worker as static files.

  • Cloudflare Workers
  • Durable Objects
  • D1
On this page

What does one page view cost?

On a site with an admin, public pages are usually built when someone views them: each view runs a Worker and makes two D1 queries. A thousand views means a thousand Worker requests, and the free plan allows 100,000 a day. A crawler or a flood of junk traffic can use that up fast.

A fully static site doesn’t have this problem, since static file requests are free. But it has no admin: editing means bolting on a CMS and waiting for a CI rebuild. We wanted both: static public pages, a dynamic admin, and no bill.

The turn: when do the pages actually change?

Only when an editor saves. Visitors decide how many views there are, with no ceiling. Editors decide how many saves there are, a few dozen a day at most.

So build the public pages when content is saved, not when it’s viewed.

Then quota depends only on saves, and traffic doesn’t touch it. One question is left: after a save, how do the new pages become free static files?

Two features that connect

Two things about Cloudflare fit together:

  • Static asset requests are free and unlimited. A request answered straight from static assets never runs the Worker.
  • A Worker can deploy itself. Through the Cloudflare API, a Worker can redeploy itself with new static assets.

So the admin stays a dynamic Workers + D1 site. After each save, a Durable Object renders every public page inside that same Worker, then redeploys the Worker with the new static files.

Every page a visitor sees is a static file. Only what has to be dynamic, such as search and the admin, reaches the Worker.

After a save

An 8-page site goes live in about 5 seconds; 300 documents take about 15.

  • Same code, same pages. The Durable Object renders with Astro’s handle() in-process, so the static and dynamic versions are identical. No CI, no separate build.
  • A Durable Object does the work. A regular Worker invocation gets only 10 ms of CPU on the free plan, not enough to render a site. A Durable Object gets 30 seconds per invocation (alarms included) by default, and publishing is split into slices of at most 50 outbound requests each.
  • Only changed files are sent. File hashes match wrangler’s scheme, so CSS, JS and images are never re-uploaded, and if nothing changed the deploy is skipped.
  • Everything swaps at once. Deploying is the last step, so a failure never leaves a half-updated site, and it retries on its own.
  • The admin shows progress. After a save, the admin says the site is updating and when it’s done, and only speaks up separately if it takes longer than expected.

Three approaches compared

GoodTrade-offNo

ItemOur approachStatic + CI rebuildRendered on every view
Quota per page viewGood: 0Good: 0No: 1 Worker request, ~2 D1 queries
Under a flood of requestsGood: FineGood: FineNo: Quota runs out, pages start failing
Cost of each saveGood: A few Durable Object requestsTrade-off: One CI build (free plan: 3,000 minutes a month, 1 at a time)Good: 0
Save to liveGood: About 5 sTrade-off: Once the CI build finishesGood: Instant
AdminGood: Built inTrade-off: A separate CMS writing to GitGood: Built in
Logged-in or personalized pagesGood: Handled by the Worker, still dynamicNo: Not possibleGood: Yes
CostGood: FreeGood: FreeNo: Over the free quota under heavy traffic

Why it costs nothing

Quota is spent only on saves. Each limit, and what this setup uses it for:

Item Free-plan limit In this setup
Workers requests 100,000 a day; 10 ms CPU and 50 outbound requests per invocation Only search, the admin and the API
Static asset requests Free and unlimited Every public page
Static asset files 20,000 per version, 25 MiB each Pages plus build files must stay under 20,000
Durable Objects 100,000 requests and 13,000 GB-s a day; SQLite-backed only on the free plan A handful of requests per publish, progress kept in SQLite
D1 5 million rows read and 100,000 written a day, 5 GB total; queries fail past the daily limit Only the admin and publishing, about 2 queries per page per publish

Three things that can’t go wrong

  • The path alone must decide the content. Static assets ignore the query string, so /news?page=2 becomes /news/page/2. What can’t be enumerated, like search, lives under a fixed prefix the Worker answers.
  • Redeploying itself must keep everything. Keep secrets with keep_bindings and send the Durable Object exports; miss either and secrets vanish or the Durable Object’s data is deleted.
  • CSP moves from nonces to hashes. A static file can’t change per request, so rendering collects hashes of inline scripts and styles into _headers.

The trickiest bug

After launch, the admin sometimes said an update had failed while the site already showed the new content.

After a Durable Object redeploys its own Worker, that same instance keeps seeing the old static assets for minutes. The code concluded the deploy hadn’t landed, resent it with the same token, and Cloudflare refused with 100312.

The fix: a 2xx from the PUT means done, and a 100312 on a resend means the earlier deploy already landed, so that counts as success too.

When it doesn’t fit

  • Content that differs per visitor, such as pages behind a login.
  • Saves that must be live instantly: this takes a few seconds.
  • Tens of thousands of pages: rebuilding everything takes too long, so you’d need to re-render only what changed.

References