Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does vary:accept-encoding mean?

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?

like image 546
Oliver Avatar asked Oct 21 '11 11:10

Oliver


People also ask

What does accept-encoding mean?

The Accept-Encoding request HTTP header indicates the content encoding (usually a compression algorithm) that the client can understand. The server uses content negotiation to select one of the proposals and informs the client of that choice with the Content-Encoding response header.

Is accept-encoding required?

Accept-encoding header is an HTTP header which must be included on every origin server response. Its main job is to inform the browsers if the client can handle the compressed version of the website. The warning can appear when you don't use the Vary: Accept-Encoding in your header on a server or CDN.

What does accept-encoding gzip mean?

The Accept-Encoding header is used for negotiating content encoding. Accept-Encoding: gzip, deflate. The server responds with the scheme used, indicated by the Content-Encoding response header. Content-Encoding: gzip. Note that the server is not obligated to use any compression method.

What is the vary in the header response?

The Vary HTTP response header describes the parts of the request message aside from the method and URL that influenced the content of the response it occurs in. Most often, this is used to create a cache key when content negotiation is in use.


2 Answers

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.

like image 73
spender Avatar answered Sep 23 '22 12:09

spender


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

like image 35
cori Avatar answered Sep 20 '22 12:09

cori