Vary Header
The vary header is a response header used to create different variations of an object in the cache. In this post I will show how the vary header works, how we can use it and more importantly ways not to use the header.
How the vary header works
The most widely used use case for the vary header is to vary by the Accept-Encoding
header. In this use case, the response returned would be different depending on what Content-Encoding
the browser can support. In this case the response header sent by the origin would look something like the following:
vary: Accept-Encoding
To generate the response, the cache will compare the Accept-Encoding request header value with the content-encoding
returned by the origin
# request header:
Accept-Encoding: gzip, deflate
# response header:
content-encoding: gzip
In this case we can see the browser supports gzip
and the resource stored in cache is of type gzip
so we are able to return this without further processing.
Where the vary header does not work well
Issues with caches supporting the vary header come from more advanced use-cases. Here’s an example where we cache variations on the Accept-Language
header on top of the previous Accept-Encoding
header.
vary: Accept-Language Accept-Encoding
This would allow us to serve different versions to say English and French speakers based on what their Accept-Language header is:
Accept-Language: en
Accept-Language: fr
The problems come from the fact there are many different ways of browsers saying the same thing. All of the below examples are where English should be returned:
en
en-us
en-US,en;q=0.5
en-US
en-US,en;q=0.8
en-US;q=0.8,en;q=0.6,fr-CA,fr;q=0.4
The last one is an example of one where the user speaks both English and
Read Full Post...