I'm building a unified caching layer — a configurable system that sits between the application code and the network, automatically applying the right caching strategy for every request. Think of it as a lightweight, framework-agnostic alternative to SWR or React Query's caching, but designed to work across all four caching layers: in-memory, Service Worker, CDN, and the browser's HTTP cache.
The core abstraction is a CacheClient class that accepts a set of URL-matching policies and a storage backend. Each policy declares which caching strategy to use — cache-first, network-first, stale-while-revalidate, or just pass through. The storage backend can be in-memory (for React state), the Cache Storage API (for Service Workers), or IndexedDB (for long-lived offline storage). Same API, different backends.
Here is the API shape I settled on — a policy-matching cache client that resolves the right strategy for each URL:
Three policies cover most applications: fingerprinted assets get year-long cache-first; API routes use stale-while-revalidate with a 1-minute stale window; HTML pages prefer network-first but fall back to a 5-minute-old cache on failure. The consumer configures policies declaratively and the layer handles the rest.