When the tea shop's menu in the cache has gone stale, the browser doesn't download it again blindly: it asks the server whether it has changed, and a 304 Not Modified, with no body, says it hasn't. Write both halves of that exchange. A response here is an object { status, headers, body }, where headers is a plain object whose names can be in any case, since HTTP header names are case-insensitive.
conditionalHeaders(stored) returns the headers that make the request conditional, with lowercase names: if-none-match set to the stored ETag, and if-modified-since set to the stored Last-Modified, each only when the stored response has it, so {} when it has neither. RFC 9111 section 4.3.1 says to send the entity tag whenever there is one, and the date as well, for servers and proxies that don't understand entity tags. Copy each value as it is, a weak tag like W/"3f9a" included, without spaces around it, and never make up a date from Date.
freshen(stored, notModified) takes the stored response and the 304 the server sent, and returns the response to use and keep from now on: a new object with the stored status and body, and the stored headers updated with the 304's, as RFC 9111 section 4.3.4 says. Every header of the 304 is added, replacing the stored header of the same name, whatever the case of either name, and every name in the result is lowercase. Four headers are never taken from a 304: Content-Length, which RFC 9111 says not to update, and Content-Type, Content-Encoding, and Content-Range, which describe the body as it was stored, and which the RFC lets a browser keep. Don't change stored or its headers.
A 304 only describes the stored response if it has the same validator. If the 304 has an ETag that isn't exactly the stored one (or the stored response has none), it's about another version of the menu: RFC 9111 says not to update with it, so return stored itself, unchanged. A 304 without an ETag updates the stored response.
const menu = {
status: 200,
headers: { "Content-Type": "application/json", ETag: '"menu-v7"', "Last-Modified": "Mon, 23 Nov 2026 09:00:00 GMT", "Cache-Control": "max-age=60" },
body: '{"teas":["green","mint","oolong"]}',
};
conditionalHeaders(menu);
// { "if-none-match": '"menu-v7"', "if-modified-since": "Mon, 23 Nov 2026 09:00:00 GMT" }
freshen(menu, { status: 304, headers: { etag: '"menu-v7"', "cache-control": "max-age=300", date: "Wed, 25 Nov 2026 07:00:00 GMT" } });
// { status: 200, body: '{"teas":["green","mint","oolong"]}', headers: { "content-type": "application/json", etag: '"menu-v7"',
// "last-modified": "Mon, 23 Nov 2026 09:00:00 GMT", "cache-control": "max-age=300", date: "Wed, 25 Nov 2026 07:00:00 GMT" } }
const menu = { status: 200, headers: { etag: '"menu-v7"', "last-modified": "Mon, 23 Nov 2026 09:00:00 GMT", "content-type": "application/json" }, body: "{}" };
expect(conditionalHeaders(menu)).toEqual({ "if-none-match": '"menu-v7"', "if-modified-since": "Mon, 23 Nov 2026 09:00:00 GMT" });
expect(conditionalHeaders({ status: 200, headers: { ETag: ' W/"3f9a" ' }, body: "" })).toEqual({ "if-none-match": 'W/"3f9a"' });
expect(conditionalHeaders({ status: 200, headers: { "LAST-MODIFIED": "Mon, 23 Nov 2026 09:00:00 GMT" }, body: "" })).toEqual({ "if-modified-since": "Mon, 23 Nov 2026 09:00:00 GMT" });
expect(conditionalHeaders({ status: 200, headers: { Date: "Wed, 25 Nov 2026 07:00:00 GMT", "Cache-Control": "max-age=60" }, body: "" })).toEqual({});
const menu = { status: 200, headers: { etag: '"menu-v7"', "cache-control": "max-age=60", date: "Mon, 23 Nov 2026 09:00:00 GMT" }, body: "green, mint, oolong" };
const fresh = freshen(menu, { status: 304, headers: { etag: '"menu-v7"', "cache-control": "max-age=300", date: "Wed, 25 Nov 2026 07:00:00 GMT", vary: "Accept-Encoding" } });
expect(fresh).toEqual({
status: 200,
headers: { etag: '"menu-v7"', "cache-control": "max-age=300", date: "Wed, 25 Nov 2026 07:00:00 GMT", vary: "Accept-Encoding" },
body: "green, mint, oolong",
});
const menu = { status: 200, headers: { ETag: '"menu-v7"', "Cache-Control": "max-age=60", "X-Tea": "oolong" }, body: "menu" };
const fresh = freshen(menu, { status: 304, headers: { etag: '"menu-v7"', "CACHE-CONTROL": "max-age=300" } });
expect(fresh.headers).toEqual({ etag: '"menu-v7"', "cache-control": "max-age=300", "x-tea": "oolong" });
const photo = {
status: 200,
headers: { "content-type": "image/avif", "content-length": "48213", "content-encoding": "identity", "cache-control": "max-age=60" },
body: "(the photo)",
};
const fresh = freshen(photo, {
status: 304,
headers: { "Content-Type": "text/html", "Content-Length": "0", "Content-Encoding": "br", "Content-Range": "bytes 0-99/48213", "Cache-Control": "max-age=86400" },
});
expect(fresh.headers).toEqual({ "content-type": "image/avif", "content-length": "48213", "content-encoding": "identity", "cache-control": "max-age=86400" });
const menu = { status: 200, headers: { etag: '"menu-v7"', "cache-control": "max-age=60" }, body: "menu" };
expect(freshen(menu, { status: 304, headers: { etag: '"menu-v8"', "cache-control": "max-age=300" } })).toBe(menu);
expect(freshen(menu, { status: 304, headers: { etag: 'W/"menu-v7"', "cache-control": "max-age=300" } })).toBe(menu);
const untagged = { status: 200, headers: { "last-modified": "Mon, 23 Nov 2026 09:00:00 GMT" }, body: "menu" };
expect(freshen(untagged, { status: 304, headers: { ETag: '"menu-v7"' } })).toBe(untagged);
const menu = { status: 200, headers: { "last-modified": "Mon, 23 Nov 2026 09:00:00 GMT", "cache-control": "max-age=60" }, body: "menu" };
const fresh = freshen(menu, { status: 304, headers: { "cache-control": "max-age=300", date: "Wed, 25 Nov 2026 07:00:00 GMT" } });
expect(fresh).toEqual({ status: 200, headers: { "last-modified": "Mon, 23 Nov 2026 09:00:00 GMT", "cache-control": "max-age=300", date: "Wed, 25 Nov 2026 07:00:00 GMT" }, body: "menu" });
const headers = { ETag: '"menu-v7"', "Cache-Control": "max-age=60" };
const menu = { status: 200, headers, body: "menu" };
const fresh = freshen(menu, { status: 304, headers: { etag: '"menu-v7"', "cache-control": "max-age=300" } });
expect(fresh).not.toBe(menu);
expect(menu.headers).toBe(headers);
expect(headers).toEqual({ ETag: '"menu-v7"', "Cache-Control": "max-age=60" });
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 Revalidation headers. 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.