What does vary:accept-encoding mean?
Asked Answered
K

2

59

the google page speed addon informs me:

The following publicly cacheable, compressible resources should have a "Vary: Accept-Encoding" header:
//some .js and .css files

I don't understand what this means. I've already compressed these files like so:

if (encodings.Contains("gzip") || encodings == "*")
{
    app.Response.Filter = new GZipStream(baseStream, CompressionMode.Compress);
    app.Response.AppendHeader("Content-Encoding", "gzip");
}

And this all seems to work. Why is having Vary: Accept-Encoding necessary?

Kellogg answered 21/10, 2011 at 11:33 Comment(0)
C
36

It is allowing the cache to serve up different cached versions of the page depending on whether or not the browser requests GZIP encoding or not. The vary header instructs the cache to store a different version of the page if there is any variation in the indicated header.

As things stand, there will be one (possibly compressed) copy of the page in cache. Say it is the compressed version: If somebody requests the resource but does not support gzip encoding, they'll be served the wrong content.

Clavius answered 21/10, 2011 at 11:42 Comment(0)
L
13

Vary: Accept-Encoding informs the behavior of the server with respect to caching the representation of the requested resource. If a new request for a previously cached resource is received, it will be served from the cache unless the Accept-Encoding header of the new request is different from the previously cached representation, at which point the request will be treated as a new request and will not be served from cache.

** EDIT ** As spender points out - if you're serving a compressed file from cache and the client doesn't accept your compression mechanism they'll get a page of junk, so yes, it's necessary. You wouldn't necessarily notice the difference through normal testing, though.

See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.44 and http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3

Landmass answered 21/10, 2011 at 11:41 Comment(2)
Definitely necessary. If GZIPPED version is in cache and a client does not accept GZIP, they'll be served gobbledegook.Clavius
Nitpick: the cache can store multiple replies and doesn't need to treat requests as new if it has a matching cached response already.Carvel

© 2022 - 2024 — McMap. All rights reserved.