Cache Rules vs Workers Cache API for conditional HTML caching on Cloudflare
0 reputation · 12 Oct 2020, 19:24 UTC
0 reputation · 12 Oct 2020, 19:24 UTC
We run a zone where most traffic is HTML pages generated at the origin. Some responses are safe to cache at the edge (anonymous, cacheable status codes), while others must never be cached (responses carrying Set-Cookie or personalized content). The goal is fine-grained, conditional cache control without serving stale personalized pages.
Two documented paths seem available. Cache Rules (the newer replacement for legacy Page Rules) can set cache eligibility, edge TTL, and bypass-on-cookie through dashboard configuration, with no per-request compute cost. The Workers Cache API (caches.default) plus cf properties like cacheKey and cacheTtlByStatus allow programmatic decisions per response, but add invocation cost, code to maintain, and latency on misses.
What is unclear is where the practical line sits: Cache Rules matching appears limited to URL patterns and a fixed set of request fields, so it may not express "cache only 200 responses without Set-Cookie" the way a Worker can. Feature parity between the two also seems to be evolving, and plan limits differ.
Before committing, I would check cf-cache-status on test URLs and review cache analytics hit ratio by content type to confirm misses are the real bottleneck.
33775 reputation · 13 Oct 2020, 00:06 UTC
Cache Rules cannot inspect response‑level data such as HTTP status code or the presence of a Set‑Cookie header, so they cannot express the rule “cache only 200 responses without Set‑Cookie”. A Cloudflare Worker using the Cache API is required for that logic.
For high‑traffic zones the per‑request CPU cost of a Worker is noticeable only when the edge‑cache miss ratio exceeds roughly 15‑20 % (i.e., hit ratio < 80‑85 %). Below that threshold the added latency and cost are usually negligible compared with the flexibility gain.
Cloudflare provides a one‑click “Migrate Page Rules to Cache Rules” tool in the dashboard. It reproduces matching logic, TTL, cache level and bypass‑on‑cookie settings for pure caching rules. Behavior is preserved as long as the rule does not rely on legacy actions such as URL forwarding, Always Online, or custom headers.
curl -I -H "Host: example.com" https://example.com/your-html-path and look at the cf-cache-status header (HIT, MISS, BYPASS, EXPIRED).cf-cache-status and response headers.Missing diagnostic detail: If you do not know the observed HTML miss ratio (or hit ratio), please provide that number; it determines whether the Worker’s per‑request cost outweighs its flexibility.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.