Skip to the code

HTTP freshness

MediumCaching

Before it reuses a stored response, the browser's HTTP cache (like every CDN) asks one question: is it still fresh? Write freshness(stored, now, options). stored is a response in the cache, { status, headers, requestTime, responseTime }: headers is an object with lowercase names ("cache-control", date, expires, age, "last-modified"), and the two times are when the cache sent the request and when the response arrived, in milliseconds on the cache's clock, as Date.now() gives them. now is the current time on the same clock. options.shared is true for a shared cache, like a CDN, and false (the default) for a private one, like the browser's. Return { state, lifetime, age }: the freshness lifetime and the current age, both in seconds, and the state, "fresh", "stale-while-revalidate", or "stale".

Read Cache-Control as a list of directives separated by commas, each a name or name=value. Names are case-insensitive, a value may be in quotes (max-age="60"), and only the first occurrence of a directive counts. A max-age, s-maxage, or stale-while-revalidate whose value isn't a whole number written in digits counts as 0, and is still there. The dates (date, expires, "last-modified") are HTTP dates like Wed, 25 Nov 2026 07:00:00 GMT, which Date.parse() reads; a date it can't read is invalid.

The freshness lifetime is the first of these that applies, in the order RFC 9111 section 4.2.1 gives them. In a shared cache, s-maxage. Then max-age. Then Expires minus Date, never less than 0, and 0 when Expires is invalid (an Expires of 0 means already expired). Otherwise, a heuristic: a tenth of the time from Last-Modified to Date, the fraction RFC 9111 suggests, so a file unchanged for 10 days stays fresh for 1 day. The heuristic applies only when the status is one RFC 9110 calls heuristically cacheable (200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, or 501) or Cache-Control has public, and only with a valid Last-Modified no later than Date. Anything else has a lifetime of 0. Wherever Date is missing or invalid, use responseTime in its place.

The current age follows RFC 9111 section 4.2.3. The apparent age is responseTime minus Date, or 0 if that's negative. The corrected age is the Age header plus the response delay, responseTime minus requestTime. The age when the response arrived is the larger of the two, and the current age is that plus the time the response has been stored, now minus responseTime. When Age lists several values, use the first; an Age that isn't a whole number written in digits counts as 0, as a missing one does. The times are in milliseconds, so an age can have a fraction.

A response is fresh while its lifetime is greater than its age. After that, stale-while-revalidate=N (RFC 5861) lets a cache go on answering with it for N more seconds while it revalidates in the background: the state is "stale-while-revalidate" while the age is less than the lifetime plus N. That doesn't apply when another directive forbids serving the response stale, as RFC 9111 section 4.2.4 says: must-revalidate in any cache, and proxy-revalidate or s-maxage in a shared cache. Some responses are never fresh: one with no-store, one with no-cache (with or without a list of fields, which demands revalidation before every use), and, in a shared cache, one with private, which a shared cache mustn't store. Their state is "stale", whatever their lifetime.

const sent = Date.parse("Wed, 25 Nov 2026 07:00:00 GMT");
freshness(
  {
    status: 200,
    headers: { "cache-control": "max-age=600, stale-while-revalidate=30", date: "Wed, 25 Nov 2026 07:00:00 GMT" },
    requestTime: sent,
    responseTime: sent + 1000, // the menu took a second to arrive
  },
  sent + 610_000, // a little over 10 minutes later
);
// { state: "stale-while-revalidate", lifetime: 600, age: 610 }

What the tests check

  1. with max-age, fresh while the lifetime is greater than the age, and stale once they're equal
    const start = Date.parse("Wed, 25 Nov 2026 07:00:00 GMT");
    const menu = { status: 200, headers: { "cache-control": "max-age=600", date: "Wed, 25 Nov 2026 07:00:00 GMT" }, requestTime: start, responseTime: start };
    expect(freshness(menu, start + 599_000)).toEqual({ state: "fresh", lifetime: 600, age: 599 });
    expect(freshness(menu, start + 600_000)).toEqual({ state: "stale", lifetime: 600, age: 600 });
    
  2. s-maxage counts only in a shared cache, max-age beats Expires, and Expires counts from Date
    const start = Date.parse("Wed, 25 Nov 2026 07:00:00 GMT");
    const http = (seconds) => new Date(start + seconds * 1000).toUTCString();
    const teas = { status: 200, headers: { "cache-control": "max-age=60, s-maxage=3600", date: http(0), expires: http(86400) }, requestTime: start, responseTime: start };
    expect(freshness(teas, start).lifetime).toBe(60);
    expect(freshness(teas, start, { shared: true }).lifetime).toBe(3600);
    expect(freshness({ ...teas, headers: { date: http(0), expires: http(86400) } }, start).lifetime).toBe(86400);
    expect(freshness({ ...teas, headers: { "cache-control": "public", date: http(-30), expires: http(270) } }, start).lifetime).toBe(300);
    
  3. Expires without a Date counts from when the response arrived, and an invalid or past Expires has already expired
    const start = Date.parse("Wed, 25 Nov 2026 07:00:00 GMT");
    const http = (seconds) => new Date(start + seconds * 1000).toUTCString();
    const stored = (headers) => ({ status: 200, headers, requestTime: start, responseTime: start });
    expect(freshness(stored({ expires: http(120) }), start + 60_000)).toEqual({ state: "fresh", lifetime: 120, age: 60 });
    expect(freshness(stored({ date: http(0), expires: "0" }), start)).toEqual({ state: "stale", lifetime: 0, age: 0 });
    expect(freshness(stored({ date: http(0), expires: "next week" }), start)).toEqual({ state: "stale", lifetime: 0, age: 0 });
    expect(freshness(stored({ date: http(0), expires: http(-60) }), start)).toEqual({ state: "stale", lifetime: 0, age: 0 });
    
  4. the age adds the Age header, the response delay, and the time stored, or uses the apparent age when that's larger
    const start = Date.parse("Wed, 25 Nov 2026 07:00:00 GMT");
    const http = (seconds) => new Date(start + seconds * 1000).toUTCString();
    // A CDN had kept the menu for 100 s, and it took 2 s to arrive.
    const fromCdn = { status: 200, headers: { "cache-control": "max-age=600", date: http(0), age: "100" }, requestTime: start, responseTime: start + 2000 };
    expect(freshness(fromCdn, start + 12_000).age).toBe(112);
    // Date says it was made 50 s before it arrived, which is more than Age and the delay.
    const old = { status: 200, headers: { "cache-control": "max-age=600", date: http(-50), age: "10" }, requestTime: start, responseTime: start + 1000 };
    expect(freshness(old, start + 1000).age).toBe(51);
    // The server's clock is ahead: a negative apparent age counts as 0.
    const ahead = { status: 200, headers: { "cache-control": "max-age=600", date: http(30) }, requestTime: start, responseTime: start + 500 };
    expect(freshness(ahead, start + 500).age).toBe(0.5);
    
  5. an Age that lists several values uses the first, and one that isn't a whole number counts as 0
    const start = Date.parse("Wed, 25 Nov 2026 07:00:00 GMT");
    const stored = (age) => ({ status: 200, headers: { "cache-control": "max-age=600", date: "Wed, 25 Nov 2026 07:00:00 GMT", age }, requestTime: start, responseTime: start });
    expect(freshness(stored("30, 60"), start).age).toBe(30);
    expect(freshness(stored("-5"), start).age).toBe(0);
    expect(freshness(stored("1.5"), start).age).toBe(0);
    expect(freshness(stored("a minute"), start).age).toBe(0);
    
  6. with no explicit lifetime, a heuristically cacheable response gets a tenth of the time since Last-Modified
    const start = Date.parse("Wed, 25 Nov 2026 07:00:00 GMT");
    const http = (seconds) => new Date(start + seconds * 1000).toUTCString();
    const stored = (status, headers) => ({ status, headers, requestTime: start, responseTime: start });
    const tenDaysOld = { date: http(0), "last-modified": http(-10 * 86400) };
    expect(freshness(stored(200, tenDaysOld), start + 3_600_000)).toEqual({ state: "fresh", lifetime: 86400, age: 3600 });
    expect(freshness(stored(404, tenDaysOld), start).lifetime).toBe(86400);
    expect(freshness(stored(302, tenDaysOld), start).lifetime).toBe(0);
    expect(freshness(stored(302, { ...tenDaysOld, "cache-control": "public" }), start).lifetime).toBe(86400);
    expect(freshness(stored(200, { ...tenDaysOld, "cache-control": "max-age=0" }), start).lifetime).toBe(0);
    expect(freshness(stored(200, { date: http(0), "last-modified": http(60) }), start).lifetime).toBe(0);
    expect(freshness(stored(200, { date: http(0) }), start).lifetime).toBe(0);
    
  7. stale-while-revalidate keeps a response usable for N more seconds, unless a directive forbids serving it stale
    const start = Date.parse("Wed, 25 Nov 2026 07:00:00 GMT");
    const stored = (cacheControl) => ({ status: 200, headers: { "cache-control": cacheControl, date: "Wed, 25 Nov 2026 07:00:00 GMT" }, requestTime: start, responseTime: start });
    const reviews = stored("max-age=60, stale-while-revalidate=30");
    expect(freshness(reviews, start + 60_000).state).toBe("stale-while-revalidate");
    expect(freshness(reviews, start + 89_000).state).toBe("stale-while-revalidate");
    expect(freshness(reviews, start + 90_000).state).toBe("stale");
    expect(freshness(stored("max-age=60, stale-while-revalidate=30, must-revalidate"), start + 70_000).state).toBe("stale");
    expect(freshness(stored("max-age=60, s-maxage=60, stale-while-revalidate=30"), start + 70_000, { shared: true }).state).toBe("stale");
    expect(freshness(stored("max-age=60, s-maxage=60, stale-while-revalidate=30"), start + 70_000).state).toBe("stale-while-revalidate");
    expect(freshness(stored("max-age=60, proxy-revalidate, stale-while-revalidate=30"), start + 70_000, { shared: true }).state).toBe("stale");
    expect(freshness(stored("max-age=60, proxy-revalidate, stale-while-revalidate=30"), start + 70_000).state).toBe("stale-while-revalidate");
    
  8. no-store and no-cache are never fresh, and neither is private in a shared cache
    const start = Date.parse("Wed, 25 Nov 2026 07:00:00 GMT");
    const stored = (cacheControl) => ({ status: 200, headers: { "cache-control": cacheControl, date: "Wed, 25 Nov 2026 07:00:00 GMT" }, requestTime: start, responseTime: start });
    expect(freshness(stored("max-age=600, no-cache"), start)).toEqual({ state: "stale", lifetime: 600, age: 0 });
    expect(freshness(stored('no-cache="Set-Cookie", max-age=600'), start).state).toBe("stale");
    expect(freshness(stored("no-store, max-age=600, stale-while-revalidate=60"), start + 700_000).state).toBe("stale");
    expect(freshness(stored("private, max-age=600"), start).state).toBe("fresh");
    expect(freshness(stored("private, max-age=600"), start, { shared: true }).state).toBe("stale");
    
  9. directive names are case-insensitive, the first occurrence counts, and an invalid max-age is 0
    const start = Date.parse("Wed, 25 Nov 2026 07:00:00 GMT");
    const stored = (cacheControl) => ({ status: 200, headers: { "cache-control": cacheControl, date: "Wed, 25 Nov 2026 07:00:00 GMT", expires: "Wed, 25 Nov 2026 08:00:00 GMT" }, requestTime: start, responseTime: start });
    expect(freshness(stored("Max-Age=300"), start).lifetime).toBe(300);
    expect(freshness(stored("max-age=300, max-age=0"), start).lifetime).toBe(300);
    expect(freshness(stored('max-age="300"'), start).lifetime).toBe(300);
    expect(freshness(stored("max-age=five"), start)).toEqual({ state: "stale", lifetime: 0, age: 0 });
    expect(freshness(stored("public"), start).lifetime).toBe(3600);
    

Learn more

solution.jsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).