Stale-While-Revalidate Cache With Background Refresh
Serve fresh entries instantly, return stale values while one background refresh runs, and surface refresh failures without breaking reads.
Code
export interface SwrOptions<K> {
ttlMs: number;
staleMs: number;
now?: () => number;
onError?: (error: unknown, key: K) => void;
}
interface CacheEntry<V> {
value: V;
freshUntil: number;
staleUntil: number;
}
export class SwrCache<K, V> {
private readonly entries = new Map<K, CacheEntry<V>>();
private readonly inflight = new Map<K, Promise<V>>();
private readonly ttlMs: number;
private readonly staleMs: number;
private readonly now: () => number;
private readonly onError?: (error: unknown, key: K) => void;
constructor(options: SwrOptions<K>) {
if (!Number.isFinite(options.ttlMs) || options.ttlMs <= 0) {
throw new RangeError("ttlMs must be a positive finite number");
}
if (!Number.isFinite(options.staleMs) || options.staleMs < 0) {
throw new RangeError("staleMs must be a non-negative finite number");
}
this.ttlMs = options.ttlMs;
this.staleMs = options.staleMs;
this.now = options.now ?? Date.now;
this.onError = options.onError;
}
get(key: K, loader: () => Promise<V>): Promise<V> {
if (typeof loader !== "function") {
return Promise.reject(new TypeError("loader must be a function"));
}
const now = this.now();
const entry = this.entries.get(key);
if (entry && now < entry.freshUntil) return Promise.resolve(entry.value);
if (entry && now < entry.staleUntil) {
void this.refresh(key, loader).catch((error) => {
try {
this.onError?.(error, key);
} catch {
// Observers must not break stale reads.
}
});
return Promise.resolve(entry.value);
}
return this.refresh(key, loader);
}
invalidate(key?: K): void {
if (key === undefined) this.entries.clear();
else this.entries.delete(key);
}
private refresh(key: K, loader: () => Promise<V>): Promise<V> {
const existing = this.inflight.get(key);
if (existing) return existing;
const promise = Promise.resolve()
.then(loader)
.then((value) => {
const now = this.now();
this.entries.set(key, {
value,
freshUntil: now + this.ttlMs,
staleUntil: now + this.ttlMs + this.staleMs,
});
return value;
})
.finally(() => {
this.inflight.delete(key);
});
this.inflight.set(key, promise);
return promise;
}
}How the snippet works
Stale-While-Revalidate Cache With Background Refresh keeps everything in the class SwrCache.
The asynchronous flow is linearized with await, keeping failure handling local. The failure path is explicit — RangeError is raised when the guard condition holds.
What to watch for
- The guard
typeof loader !== "function"short-circuits the flow with an earlyreturnwhen its condition holds.
Using it
SwrCache is the public entry point; everything else in the file exists to support it.
Key statements
if (entry && now < entry.freshUntil) return Promise.resolve(entry.value);
return Promise.reject(new TypeError("loader must be a function"));
private readonly inflight = new Map<K, Promise<V>>();
Interactive Tools for This Topic
Cache-Control BuilderCompose a valid Cache-Control header from directives such as no-store, private, max-age, and immutable.Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.Basic & Bearer Auth Header ToolEncode and decode HTTP Authorization Basic and Bearer headers locally with UTF-8 Base64 validation and automatic JWT claim and expiry inspection.Set-Cookie ParserParse one or more Set-Cookie header values into the cookie name, value, and attributes like Path, Domain, Expires, Max-Age, Secure, HttpOnly, and SameSite.
FAQ
- How does the snippet fail?
- `RangeError` is raised at the guard (`typeof loader !== "function"`), so invalid state never reaches the main logic.
- Is the dependency story complicated?
- Not at all: zero dependencies, so there is nothing to version or audit.
Report a problem with this exampleOpens a pre-filled issue on GitHub.