How On-Demand Revalidation Works
How On-Demand Revalidation Works
The Flow
1. First request — middleware checks caches.default (miss), renders the page, stores it, returns it with Cache-Control: public, s-maxage=31536000.
2. Subsequent requests — middleware finds the cached entry (hit) and returns it instantly — no D1 query, no rendering.
3. Publishing a post — the /api/posts endpoint inserts into D1, then calls caches.default.delete() for / and /blog.
4. Next request — cache miss again, fresh render picks up the new post, re-cached.
Why not Cloudflare CDN cache headers alone?
Cache-Control headers tell Cloudflare's CDN to cache, but you cannot purge individual URLs without a paid Cache Purge API plan. The Cache API (caches.default) is per-worker, gives you programmatic put / delete control, and is free.