A field note from DateSpots: Astro on Cloudflare Workers, hashed /_astro CSS, and the long road to “it shouldn’t go blank after ship.”

The symptom

Every so often, right after a deploy, a phone or a cold edge would show the site unstyled: system serif instead of our type, category chips as plain blue links, header half-OK.

It looked intermittent. Waiting or “try a hard refresh” felt like the folklore fix. In practice a hard refresh often did not fix it.

City and apex HTML lived in our platform Workers Cache (dsc-html). A refresh revalidated from the browser’s point of view, but the edge could still hand back the same cached HTML: markup that still pointed at prior hashed CSS files. Those files were gone from the new Worker’s Static Assets. Same document, same dead stylesheet URLs, same broken page, until HTML actually moved to a new version and those CSS URLs resolved.

This was not a flaky CSS bug. It was version skew.

The root cause

Astro ships CSS as content-hashed files (Layout.abc123.css). HTML points at those URLs.

Cloudflare deploys the Worker and Static Assets as one version. A normal wrangler deploy puts that version on 100% of traffic. The live version only serves that version’s /_astro set. Prior hashes are not retained in ASSETS.

TimeBrowserCloudflare
T0Loads HTML → Layout.abc.cssDeployment A
T1Tab stays open (or later re-fetches HTML)Deployment B goes live (ASSETS = B only)
T2Requests Layout.abc.css, or gets cached HTML that still asks for itNot in B’s ASSETS → miss → unstyled

So two ways to stay broken:

  1. An open tab with A’s HTML after B ships.
  2. A “fresh” navigation or hard refresh that still receives A’s HTML from the HTML platform cache.

I confirmed in production: current hash → 200 text/css; prior hash → 404 text/plain.

What I tried along the way

1. Treat it like a cache / “warm the edges” problem

Post-deploy warm of HTML and CSS on high-traffic city and category URLs. Helps the new hashes get onto nearby POPs. Doesn’t keep old hashes alive, and doesn’t stop the HTML cache from serving bodies that still name those old hashes.

2. Purge platform HTML after UI/CSS deploys

Clear dsc-html so people get new HTML sooner, not hours of stale documents pointing at dead CSS. Essential for shipping the new UI, and for making refresh actually mean something after a deploy. Complementary to asset retention, not a substitute. (Today the post-deploy warm only fleet-purges when /_astro hashes actually changed — or when I force it — so Worker-only deploys keep warm POP HTML.)

3. Inlined critical CSS

Shell and category-intro rules inlined so first paint isn’t raw Times even if hashed CSS is late. Softens the flash. Doesn’t load the full design system.

4. Move big JSON-LD out of <head>

Astro puts stylesheet links at the end of <head>. A huge ItemList JSON-LD in front delayed CSS discovery. I moved JSON-LD into <body>. Faster discovery. Not the root cause.

5. Preload CSS via Vite ?url (failed)

Preload used a different hash than Astro’s real <link rel="stylesheet">, so preload 404s while the real file was fine. Removed.

6. Never serve HTML as CSS

On /_astro miss, return plain 404 text/plain plus Cache-Control: no-store. Never SPA or HTML fallback. Browsers get a real stylesheet error. HTML-as-CSS is a silent death. Sticky wrong bodies make “refresh” even more confusing.

7. One-shot “boot-heal” reload

On /_astro stylesheet error, reload once (with a cooldown). Sometimes helped once HTML and assets had caught up. Useless if refresh kept pulling the same cached HTML with dead hashes. Band-aid, not architecture.

8. Observability

Workers logs (astro-asset-miss, later astro-asset-hist), css-health audit UI. Confirmed the pattern in production.

9. Things I considered and didn’t do

  • Carry-forward prior files into every dist/_astro: works, but the Worker asset bundle grows every generation.
  • R2 as hist store: fine long-term; overkill while files are small.
  • Cloudflare Shared Dictionaries: delta compression when both URLs exist. Does not resurrect a 404. Wrong tool for availability.
  • Inline all CSS or unhashed /app.css: bad caching tradeoffs.

10. Investigations that sharpened the diagnosis

  • Consecutive no-op Astro builds → stable CSS hashes (content-addressed).
  • Live probe of old vs current URLs.
  • Docs: Worker and assets are one version; live traffic doesn’t keep prior ASSETS.
  • Separating browser HTML Cache-Control from edge CDN-Cache-Control so browsers revalidate while the platform cache still owns freshness. That’s why “hard refresh” was such a misleading mental model.

What I shipped as the real fix

Historical /_astro in Workers KV.

  1. Before wrangler deploy, snapshot each dist/_astro/* into CACHE KV (ds:astro-hist:{basename}, about 90 days TTL). Same basename on a later deploy overwrites and slides the TTL — so a long-lived hash doesn’t expire while it’s still current, then vanish the day it finally leaves ASSETS. There is no fixed “keep last N deploys” count; distinct hashes accumulate until each key’s TTL elapses.
  2. Read one blob back (size check). Put or get failure aborts CI: no cutover, previous Worker and ASSETS stay live. Don’t ship a new asset set if hist can’t be written or read.
  3. Then deploy. Routing stays ASSETS-first: current hash → no Worker.
  4. On miss → Worker → KV → 200 (or plain 404 if unknown).
  5. Upload validates shared basename safety and ≤ 25 MiB per file (KV limit).
  6. Browser: hashed assets get long-lived immutable caching so a hash you already downloaded isn’t needlessly revalidated.

HTML purge stays on purpose after cutover when /_astro hashes changed (or I force it). Hist keeps old CSS URLs alive. Purge makes new HTML show up soon so refresh isn’t stuck on yesterday’s document for up to the HTML edge TTL (12 hours fresh, then stale-while-revalidate).

Layer cake (what still earns its keep)

LayerRole
KV hist (pre-deploy + read-back)Old hash still available; KV sick → don’t cut over
HTML purge + warmFresh HTML when hashes change (or forced); prime new CSS
Immutable browser cache on /_astroDon’t re-fetch a hash you already have
Critical CSSSoft FOUC
Plain 404 + no-storeNo HTML-as-CSS; 404s aren’t sticky
Boot-healLast-resort self-reload
Logsastro-asset-hist vs astro-asset-miss

Lessons

  1. Hashed assets + atomic Worker versions = retention problem, not “CSS is flaky.”
  2. Cached HTML is part of the bug. If HTML can outlive the assets it names, refresh will gaslight you.
  3. Compression is not availability (Shared Dictionaries).
  4. Purge HTML is not keep old CSS. You usually want both.
  5. ASSETS-first means the Worker only sees misses. That’s the right place for a hist fallback.
  6. Upload hist before cutover. If KV can’t take the snapshot, keep the old site.
  7. Don’t cache those 404s. Always re-check.
  8. Measure: hash stability, live old-URL 404, then build the store.