Skip to the code

Parse Cache-Control

Every response the tea shop's server sends says how it may be cached in its Cache-Control header, and every cache on the way (the browser's, a CDN's, a service worker) has to read it the same way. Write parseCacheControl(value), which turns the header's value into an object of directives, such as { "max-age": 600, public: true }. value is a string, or null when the response has no Cache-Control (which is what headers.get() gives then); null and a header with nothing in it both give {}.

RFC 9111 defines the syntax. The value is a list of directives separated by commas: ignore spaces and tabs around each one, and empty items like the one in public,,max-age=60. A directive is a name, optionally followed by = and an argument. Names are case-insensitive: return them in lowercase. An argument is either a token (max-age=600) or a quoted string (no-cache="Set-Cookie"). Drop the quotes; inside them, a comma doesn't end the directive, and a backslash means the character after it is taken as it is (\" is a quote). A directive without an argument is true, and any other argument is a string, kept as it's written.

Six directives take a number of seconds, which the RFC calls delta-seconds: max-age, s-maxage, stale-while-revalidate, stale-if-error, min-fresh, and max-stale. Give their argument as a number, whether it's a token or quoted (max-age="60" is 60), since the RFC asks recipients to accept both forms. An argument that isn't a whole number of 0 or more, written in digits (max-age=ten, max-age=-1, max-age=1.5, max-age=0x10, or a bare max-age), is invalid: make it 0, since RFC 9111 encourages caches to treat a response with invalid freshness information as stale. The one exception is a bare max-stale, which means a stale response of any age will do: keep that true. Cap a number at 2147483648 (2^31 seconds, over 68 years), the value RFC 9111 gives a delta-seconds too large to represent.

When a directive appears more than once, keep the first, as RFC 9111 advises. Keep directives you don't know, like immutable or an extension such as community="UCI", the same way as the rest: a cache ignores what it doesn't understand, but the parser reports everything that's there.

parseCacheControl('public, Max-Age=600, stale-while-revalidate=30, no-cache="Set-Cookie, Authorization"');
// { public: true, "max-age": 600, "stale-while-revalidate": 30, "no-cache": "Set-Cookie, Authorization" }
parseCacheControl("max-age=60, max-age=0"); // { "max-age": 60 }: the first one counts
parseCacheControl("max-age=ten"); // { "max-age": 0 }: invalid, so the response is stale
parseCacheControl(null); // {}

What the tests check

  1. reads directives with and without arguments
    expect(parseCacheControl("public, max-age=600")).toEqual({ public: true, "max-age": 600 });
    expect(parseCacheControl("no-store")).toEqual({ "no-store": true });
    expect(parseCacheControl("private, no-cache, must-revalidate")).toEqual({ private: true, "no-cache": true, "must-revalidate": true });
    expect(parseCacheControl("max-age=31536000, immutable, tea=Oolong")).toEqual({ "max-age": 31536000, immutable: true, tea: "Oolong" });
    
  2. names are case-insensitive and come back lowercase, and spaces and empty items don't count
    expect(parseCacheControl("  Public ,, MAX-AGE=60 ,\tImmutable ,")).toEqual({ public: true, "max-age": 60, immutable: true });
    
  3. every delta-seconds directive is a number, written as a token or in quotes
    expect(parseCacheControl('max-age="60", s-maxage=0, stale-while-revalidate=30, stale-if-error=86400, min-fresh=5, max-stale=120')).toEqual({
      "max-age": 60,
      "s-maxage": 0,
      "stale-while-revalidate": 30,
      "stale-if-error": 86400,
      "min-fresh": 5,
      "max-stale": 120,
    });
    
  4. a quoted argument can hold commas, and a backslash takes the next character as it is
    expect(parseCacheControl('no-cache="Set-Cookie, Authorization", max-age=60')).toEqual({ "no-cache": "Set-Cookie, Authorization", "max-age": 60 });
    expect(parseCacheControl('community="the \\"Leaf, Kettle\\" club", private')).toEqual({ community: 'the "Leaf, Kettle" club', private: true });
    
  5. the first occurrence of a directive counts, whatever the case of its name
    expect(parseCacheControl("max-age=60, MAX-AGE=0, public")).toEqual({ "max-age": 60, public: true });
    expect(parseCacheControl('no-cache="Set-Cookie", no-cache')).toEqual({ "no-cache": "Set-Cookie" });
    
  6. an invalid number of seconds is 0, so the response counts as stale, but a bare max-stale is true
    expect(parseCacheControl("max-age=ten")).toEqual({ "max-age": 0 });
    expect(parseCacheControl("max-age=-1, s-maxage=1.5, min-fresh=0x10, stale-if-error=+60")).toEqual({ "max-age": 0, "s-maxage": 0, "min-fresh": 0, "stale-if-error": 0 });
    expect(parseCacheControl("max-age, stale-while-revalidate=")).toEqual({ "max-age": 0, "stale-while-revalidate": 0 });
    expect(parseCacheControl("max-stale")).toEqual({ "max-stale": true });
    
  7. a number too large to represent is capped at 2^31 seconds
    expect(parseCacheControl("max-age=99999999999999999999")).toEqual({ "max-age": 2147483648 });
    expect(parseCacheControl("s-maxage=2147483649")).toEqual({ "s-maxage": 2147483648 });
    expect(parseCacheControl("max-age=2147483647")).toEqual({ "max-age": 2147483647 });
    
  8. no header, or one with nothing in it, is no directives
    expect(parseCacheControl(null)).toEqual({});
    expect(parseCacheControl("")).toEqual({});
    expect(parseCacheControl(" , ")).toEqual({});
    

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).