Cloudflare Workers Cache introduces a tiered cache directly in front of Workers, allowing server-rendered applications to leverage HTTP caching mechanisms like `Cache-Control` and `stale-while-revalidate` for improved performance and reduced CPU costs. This shifts the architecture where Workers act as the origin, providing a static-like experience without complex build-time prerendering or per-request rendering overhead. The cache is Worker-scoped, offering programmatic control and supporting content negotiation via `Vary` headers.
Read original on Cloudflare BlogCloudflare Workers Cache fundamentally alters the caching paradigm for applications running on Cloudflare Workers, especially those acting as the origin for server-side rendering (SSR). Previously, Workers sat in front of the cache and origin, giving control over what *reached* the origin. With Workers Cache, the cache now sits *in front of the Worker*, allowing Cloudflare to serve cached responses directly without invoking the Worker, significantly reducing latency and CPU costs for cacheable content.
The introduction of Workers Cache addresses a critical gap for server-rendered applications deployed on Cloudflare Workers. When Workers evolved from being a proxy to becoming the primary origin, the benefits of Cloudflare's existing edge cache for the Workers' output were limited. Every request would hit the Worker, incurring latency and CPU costs. The new architecture enables a true SSR experience with edge caching, providing a third option between static site generation (SSG) and pure server-side rendering.
A key feature is the full support for standard HTTP `Cache-Control` headers, particularly `stale-while-revalidate`. This directive allows Cloudflare to immediately serve a stale cached response while asynchronously refreshing it in the background by invoking the Worker. This pattern ensures users almost always receive a cache-speed response, even for content that is being updated, bridging the gap between static content speed and dynamic content freshness.
export default {
async fetch(request) {
const html = await renderPage(request);
return new Response(html, {
headers: {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "public, max-age=300, stale-while-revalidate=3600",
},
});
},
};Unlike Cloudflare's traditional zone-level cache, Workers Cache is scoped to the individual Worker. This means caching behavior is controlled entirely by the Worker's code and its `Cache-Control` headers, rather than separate zone configurations or Page Rules. This isolation prevents conflicts between different Workers or other zone content and allows for programmatic purges specific to the Worker.
Advanced Content Negotiation
The `Vary` HTTP header is fully supported, enabling the cache to store and serve different representations of a single URL (e.g., HTML vs. JSON, WebP vs. JPEG, different languages) based on request headers like `Accept` or `Accept-Language`. This is crucial for building robust, performant APIs and multi-format content delivery on the edge.