The tea shop's menu at /menu comes in English and French, compressed with Brotli or gzip. The server says so with Vary: Accept-Language, Accept-Encoding, so a cache may keep several responses for the one URL, and must give each request the right one. Write matchVary(request, stored). request is { headers }. stored lists the responses the cache has for this URL and method, in the order they were stored, each as { request, response }: the request that fetched it ({ headers }) and the response ({ headers, body }). Headers are plain objects whose names can be in any case. Return the stored entry the request can use, or undefined when there's none.
A stored response matches when, for every header name its Vary lists, the new request and the stored request have the same value, as RFC 9111 section 4.1 says. A response without Vary, or with an empty one, matches every request. Vary is a list of header names separated by commas, with optional spaces, and its names, like every header name, are case-insensitive. A Vary that lists * never matches: it means the server chose the response on something outside the request's headers.
Two values are the same when they're equal after normalising their whitespace, which the RFC allows wherever a header's syntax does: trim the ends, remove spaces and tabs next to a , or a ;, and turn any other run of spaces and tabs into one space, so gzip, br and gzip,br match. A value can also be an array of field lines, like ["en-GB", "en;q=0.8"], which counts as one value with the lines joined by , . Nothing else is normalised: values compare case-sensitively, and in order. A header that's absent from one request matches only if it's absent from the other too, and an empty value isn't absent.
When several responses match, choose as RFC 9111 says. If some of the matches have a Vary and some don't, drop the ones that don't: a response without Vary is often a server's default one, sent without it by mistake. Then take the most recent by its Date header, and the one stored last when two have the same Date. Every stored response has a Date, since a cache adds one when the server didn't.
const stored = [
{ request: { headers: { "Accept-Language": "fr" } }, response: { headers: { vary: "Accept-Language", date: "Wed, 25 Nov 2026 07:00:00 GMT" }, body: "Thé vert, menthe, oolong" } },
{ request: { headers: { "Accept-Language": "en-GB,en;q=0.8" } }, response: { headers: { vary: "Accept-Language", date: "Wed, 25 Nov 2026 07:05:00 GMT" }, body: "Green, mint, oolong" } },
];
matchVary({ headers: { "accept-language": "en-GB, en;q=0.8" } }, stored); // stored[1]: the same value once the spaces are normalised
matchVary({ headers: { "accept-language": "de" } }, stored); // undefined: the cache must ask the server
const date = "Wed, 25 Nov 2026 07:00:00 GMT";
const menu = { request: { headers: { "accept-language": "fr" } }, response: { headers: { date }, body: "the menu" } };
expect(matchVary({ headers: { "accept-language": "en" } }, [menu])).toBe(menu);
expect(matchVary({ headers: {} }, [menu])).toBe(menu);
const blank = { request: { headers: {} }, response: { headers: { vary: " ", date }, body: "the menu" } };
expect(matchVary({ headers: { "accept-language": "en" } }, [blank])).toBe(blank);
expect(matchVary({ headers: {} }, [])).toBe(undefined);
const date = "Wed, 25 Nov 2026 07:00:00 GMT";
const entry = (language, encoding) => ({
request: { headers: { "accept-language": language, "accept-encoding": encoding } },
response: { headers: { vary: "Accept-Language, Accept-Encoding", date }, body: language + " menu, " + encoding },
});
const stored = [entry("fr", "br"), entry("en", "gzip"), entry("en", "br")];
expect(matchVary({ headers: { "accept-language": "en", "accept-encoding": "br" } }, stored)).toBe(stored[2]);
expect(matchVary({ headers: { "accept-language": "fr", "accept-encoding": "br", "user-agent": "Kettle/1.0" } }, stored)).toBe(stored[0]);
expect(matchVary({ headers: { "accept-language": "fr", "accept-encoding": "gzip" } }, stored)).toBe(undefined);
const stored = [{ request: { headers: { "ACCEPT-LANGUAGE": "fr" } }, response: { headers: { VARY: "accept-LANGUAGE", Date: "Wed, 25 Nov 2026 07:00:00 GMT" }, body: "le menu" } }];
expect(matchVary({ headers: { "Accept-Language": "fr" } }, stored)).toBe(stored[0]);
expect(matchVary({ headers: { "Accept-Language": "en" } }, stored)).toBe(undefined);
const stored = [{
request: { headers: { "accept-encoding": "gzip, br", "user-agent": "Kettle/1.0 (Leaf)" } },
response: { headers: { vary: "Accept-Encoding,User-Agent", date: "Wed, 25 Nov 2026 07:00:00 GMT" }, body: "the menu" },
}];
const ask = (encoding, agent) => matchVary({ headers: { "accept-encoding": encoding, "user-agent": agent } }, stored);
expect(ask("gzip,br", "Kettle/1.0 (Leaf)")).toBe(stored[0]);
expect(ask(" gzip ,\tbr ", "Kettle/1.0\t(Leaf) ")).toBe(stored[0]);
expect(ask("br, gzip", "Kettle/1.0 (Leaf)")).toBe(undefined);
expect(ask("GZIP, BR", "Kettle/1.0 (Leaf)")).toBe(undefined);
expect(ask("gzip, br", "Kettle/1.0(Leaf)")).toBe(undefined);
const stored = [{ request: { headers: { "accept-language": ["en-GB", "en;q=0.8"] } }, response: { headers: { vary: "Accept-Language", date: "Wed, 25 Nov 2026 07:00:00 GMT" }, body: "the English menu" } }];
expect(matchVary({ headers: { "accept-language": "en-GB, en;q=0.8" } }, stored)).toBe(stored[0]);
expect(matchVary({ headers: { "accept-language": ["en-GB,en; q=0.8"] } }, stored)).toBe(stored[0]);
expect(matchVary({ headers: { "accept-language": ["en;q=0.8", "en-GB"] } }, stored)).toBe(undefined);
const stored = [{ request: { headers: {} }, response: { headers: { vary: "Accept-Language", date: "Wed, 25 Nov 2026 07:00:00 GMT" }, body: "the default menu" } }];
expect(matchVary({ headers: {} }, stored)).toBe(stored[0]);
expect(matchVary({ headers: { "accept-language": "fr" } }, stored)).toBe(undefined);
expect(matchVary({ headers: { "accept-language": "" } }, stored)).toBe(undefined);
const empty = [{ request: { headers: { "accept-language": "" } }, response: { headers: { vary: "Accept-Language", date: "Wed, 25 Nov 2026 07:00:00 GMT" }, body: "the default menu" } }];
expect(matchVary({ headers: { "accept-language": " " } }, empty)).toBe(empty[0]);
expect(matchVary({ headers: {} }, empty)).toBe(undefined);
const star = { request: { headers: {} }, response: { headers: { vary: "*", date: "Wed, 25 Nov 2026 08:00:00 GMT" }, body: "chosen by the server" } };
const starInList = { request: { headers: {} }, response: { headers: { vary: "Accept-Language, *", date: "Wed, 25 Nov 2026 08:00:00 GMT" }, body: "chosen by the server" } };
const plain = { request: { headers: {} }, response: { headers: { vary: "Accept-Encoding", date: "Wed, 25 Nov 2026 07:00:00 GMT" }, body: "the menu" } };
expect(matchVary({ headers: {} }, [star])).toBe(undefined);
expect(matchVary({ headers: {} }, [starInList, plain, star])).toBe(plain);
const entry = (date, body) => ({ request: { headers: { "accept-language": "en" } }, response: { headers: { vary: "Accept-Language", date }, body } });
const request = { headers: { "accept-language": "en" } };
const older = entry("Wed, 25 Nov 2026 07:00:00 GMT", "older");
const newer = entry("Wed, 25 Nov 2026 07:30:00 GMT", "newer");
const sameTime = entry("Wed, 25 Nov 2026 07:30:00 GMT", "stored last");
expect(matchVary(request, [newer, older])).toBe(newer);
expect(matchVary(request, [older, newer, sameTime])).toBe(sameTime);
const december = entry("Tue, 01 Dec 2026 06:00:00 GMT", "December");
expect(matchVary(request, [december, newer])).toBe(december);
const defaultMenu = { request: { headers: { "accept-language": "fr" } }, response: { headers: { date: "Wed, 25 Nov 2026 08:00:00 GMT" }, body: "the default menu" } };
const french = { request: { headers: { "accept-language": "fr" } }, response: { headers: { vary: "Accept-Language", date: "Wed, 25 Nov 2026 07:00:00 GMT" }, body: "le menu" } };
expect(matchVary({ headers: { "accept-language": "fr" } }, [french, defaultMenu])).toBe(french);
expect(matchVary({ headers: { "accept-language": "de" } }, [french, defaultMenu])).toBe(defaultMenu);
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).
I'm here to help with Match a request with Vary. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.