You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Unified, modernHTTP caching + invalidation + conditional requests built directly on standard Web APIs (`Request`, `Response`, `CacheStorage`). One small API: `createCacheHandler` – works on Cloudflare Workers, Netlify Edge, Deno, workerd, and Node 20+ (with Undici polyfills).
3
+
Fully-featured, modern, standards-based HTTP caching library designed for server-side rendered web apps. Get the features of a modern CDN built into your app.
4
4
5
-
## Highlights
5
+
Modern CDNs such as Cloudflare, Netlify and Fastly include powerful features that allow you to cache responses, serve stale content while revalidating in the background, and invalidate cached content by tags or paths. This library brings those capabilities to your server-side code with a simple API. This is particularly useful if you're not running your app behind a modern caching CDN. Ironically, this includes Cloudflare, because Workers run in front of the cache.
- Uses only standard headers for core caching logic: `Cache-Control` (+ `stale-while-revalidate`), `CDN-Cache-Control`, `Cache-Tag`, `Vary`, `ETag`, `Last-Modified`
9
-
- Optional custom extension header: `Cache-Vary` (library-defined – lets your backend declare specific header/cookie/query components for key derivation without bloating the standard `Vary` header)
10
-
- Stale-While-Revalidate implemented purely via directives (no custom headers)
- Zero runtime dependencies, ESM only, fully typed
15
-
- Same code everywhere (Edge runtimes, Deno, Node + Undici)
7
+
## How it works
8
+
9
+
Set standard HTTP headers in your SSR pages or API responses, and this library will handle caching. It will cache responses as needed, and return cached data if available. It supports standard headers like `Cache-Control`, `CDN-Cache-Control`, `Cache-Tag`, `Vary`, `ETag`, and `Last-Modified`. It can handle conditional requests using `If-Modified-Since` and `If-None-Match`. This also supports a custom `Cache-Vary` header (inspired by [`Netlify-Vary`](https://www.netlify.com/blog/netlify-cache-key-variations/)) that allows you to specify which headers, cookies, or query parameters should be used for caching.
10
+
11
+
## Supported runtimes
12
+
13
+
This library uses the web stqndard [`CacheStorage`](https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage) API for storage, which is available in modern runtimes like Cloudflare Workers, Netlify Edge and Deno. It can also be used in Node.js using the Node.js [`undici`](https://undici.nodejs.org/) polyfill.
1. Request arrives; cache checked (GET only is cached)
55
-
2. Miss -> `handler` runs, response cached
56
-
3. Hit & still fresh -> served instantly
57
-
4. Expired but inside `stale-while-revalidate` window -> stale response served, background revalidation queued
58
-
5. Conditional client request (If-None-Match / If-Modified-Since) may yield a 304
59
-
60
48
## Node 20+ Usage (Undici Polyfill)
61
49
62
-
Node 20 ships `fetch` et al, but _not_`caches` yet. Use `undici` to polyfill CacheStorage.
50
+
Node.js ships `CacheStorage` as part of `Undici`, but it is not available by default. To use it, you need to install the `undici` polyfill and set it up in your code:
No custom headers are added. While inside the SWR window the _stale_ cached response is returned immediately and a background revalidation run is triggered (if a `handler` was supplied).
126
+
While inside the SWR window the _stale_ cached response is returned immediately and a background revalidation run is triggered.
138
127
139
128
To use a runtime scheduler (eg Workers' `event.waitUntil`):
Each listed dimension becomes part of the derived cache key. Standard `Vary` remains fully respected; `Cache-Vary` is additive and internal – safe to use even if unknown to intermediaries.
242
237
238
+
## Cache-Status Header (optional)
239
+
240
+
You can opt-in to emitting the [RFC 9211 `Cache-Status`](https://www.rfc-editor.org/rfc/rfc9211) response header to aid debugging and observability.
241
+
242
+
Enable it with a boolean (uses the default cache name `cache-handlers`) or provide a custom cache identifier string:
0 commit comments