brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Vary tells caches which request headers change the response

Cache
~/tips/tip-cache-vary-header

Vary: Accept-Language

The Vary response header adds request headers to the cache key, so one URL can safely serve different versions.

A cache normally files a response under its URL. That breaks down when one URL answers differently depending on who asks, for instance Dutch for me and English for everyone else. A shared cache would hand my Dutch page to the next visitor. The Vary response header is the fix.

Vary: Accept-Language

The header lists the request headers that influenced the response. RFC 9110 says a cache must not reuse the response for a later request unless that request has the same values for the listed headers, or the origin server has validated the reuse. The listed headers become part of the cache key.

Here is a small Python test server that answers in Dutch when the request asks for it and sends the header along:

curl -si -H 'Accept-Language: nl' http://localhost:8000/hello

On curl 8.5.0 against that server the response carries Vary: Accept-Language and the body is Hallo. Without the Accept-Language header the body is Hello, and a cache that honours Vary keeps the two apart.

Two caveats. Every header you list can split the cache into more stored copies, so list only the headers that really change the body. And Vary: * tells a cache that something outside the request headers played a role. It can then not tell whether a stored response fits a later request without asking the origin, which makes it practically uncacheable.

MDN adds that the same Vary value should go on every response for a URL, including 304 Not Modified responses. You don’t need to list Authorization: the RFC says reuse of such a response for a different user is already prohibited.

Further reading: MDN: Vary, RFC 9110, section 12.5.5

Going deeper: Guide: your Unraid cache is full and Docker stopped, how to find out why and fix it

More Cache tips curl -s -o /dev/null -D – https://example.com/style.css | grep -i -E '^(cf-cache-status|age):' Cloudflare Development Mode bypasses the edge cache for three hours fincore /var/lib/example/data.db npm cache verify fc-cache -f -v ~/.local/share/fonts All Cache tips (27) All topics

Link