How does a browser decide whether it can reuse a cached response, and what happens when the response is stale?
Think it through first
Answer out loud or in your head as if you were in an interview, then reveal.
Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.
Questions up to level
Shows questions up to the selected level.
No practice questions for this level yet.
Think it through first
Answer out loud or in your head as if you were in an interview, then reveal.
function directives(header = "") {
const result = {};
for (const part of header.split(",")) {
const [name, value] = part.trim().toLowerCase().split("=");
if (name) result[name] = value === undefined ? true : Number(value);
}
return result;
}
function lifetime(headers, shared) {
const cc = directives(headers["cache-control"]);
if (shared && cc["s-maxage"] !== undefined) return ["s-maxage", cc["s-maxage"]];
if (cc["max-age"] !== undefined) return ["max-age", cc["max-age"]];
const date = Date.parse(headers.date);
if (headers.expires !== undefined) {
const expires = Date.parse(headers.expires);
if (Number.isNaN(expires)) return ["expires", 0];
return ["expires", Math.max(0, (expires - date) / 1000)];
}
if (headers["last-modified"] !== undefined) {
const sinceModified = (date - Date.parse(headers["last-modified"])) / 1000;
return ["heuristic", Math.floor(sinceModified * 0.1)];
}
return ["none", 0];
}
function check(headers, age, shared = false) {
const [rule, seconds] = lifetime(headers, shared);
return `${rule} ${seconds} ${seconds > age ? "fresh" : "stale"}`;
}
const date = "Mon, 16 Nov 2026 10:00:00 GMT";
const api = { "cache-control": "max-age=60, s-maxage=600", date };
console.log(check(api, 120, true));
console.log(check(api, 120));
const both = { "cache-control": "max-age=300", expires: "Mon, 16 Nov 2026 12:00:00 GMT", date };
console.log(check(both, 400));
console.log(check({ expires: "0", date }, 0));
const page = { date, "last-modified": "Fri, 06 Nov 2026 10:00:00 GMT" };
console.log(check(page, 3600));const prices = { EUR: "30.00", USD: "32.50" };
const currencyOf = (request) => request.cookie.match(/currency=(\w+)/)[1];
function origin(request) {
const currency = currencyOf(request);
return `${currency} ${prices[currency]}`;
}
function createCache(keyOf) {
const store = new Map();
return (request) => {
const key = keyOf(request);
if (store.has(key)) return `HIT ${store.get(key)}`;
const body = origin(request);
store.set(key, body);
return `MISS ${body}`;
};
}
const byUrl = createCache((request) => request.url);
const byUrlAndCurrency = createCache((request) => `${request.url} ${currencyOf(request)}`);
const eur = { url: "/api/products/lamp-l3", cookie: "sid=a1; currency=EUR" };
const usd = { url: "/api/products/lamp-l3", cookie: "sid=b2; currency=USD" };
const usd2 = { url: "/api/products/lamp-l3", cookie: "sid=c3; currency=USD" };
console.log(byUrl(eur));
console.log(byUrl(usd));
console.log(byUrlAndCurrency(eur));
console.log(byUrlAndCurrency(usd));
console.log(byUrlAndCurrency(usd2));Think it through first
Answer out loud or in your head as if you were in an interview, then reveal.
Cache-Control, the Age the CDN reported and the seconds the response has spent in this cache. What does it log?function directives(header) {
const result = {};
for (const part of header.split(",")) {
const [name, value] = part.trim().toLowerCase().split("=");
result[name] = value === undefined ? true : Number(value);
}
return result;
}
function decide(cacheControl, ageHeader, secondsInCache) {
const cc = directives(cacheControl);
if (cc["no-store"]) return "not stored";
const age = ageHeader + secondsInCache;
const lifetime = cc["max-age"] ?? 0;
if (cc["no-cache"]) return "validate first";
if (lifetime > age) return `reuse, ${lifetime - age}s left`;
const staleFor = age - lifetime;
const swr = cc["stale-while-revalidate"] ?? 0;
if (!cc["must-revalidate"] && staleFor < swr) return "reuse stale, validate in background";
return "validate first";
}
console.log(decide("max-age=600", 540, 30));
console.log(decide("max-age=600", 540, 90));
console.log(decide("max-age=600, stale-while-revalidate=120", 540, 90));
console.log(decide("max-age=600, must-revalidate, stale-while-revalidate=120", 540, 90));
console.log(decide("max-age=600, no-cache", 0, 0));
console.log(decide("no-store, max-age=600", 0, 0));Think it through first
Answer out loud or in your head as if you were in an interview, then reveal.
Think it through first
Answer out loud or in your head as if you were in an interview, then reveal.