简体   繁体   中英

HTTP Caching: Why use ETag instead of Cache-Control and/or Expires?

I had been reading about HTTP browser caching and playing around with it on my own site when I noticed that jQuery's Media Temple ProCDN backed library : http://code.jquery.com/jquery-1.7.1.min.js doesn't follow the caching best practices according to Google.

Pretty much (I think), there are two types of browser side caching:

  1. Expires and Cache-Control: max-age.

    These are are considered "'strong caching headers' that apply unconditionally; that is, once they're set and the resource is downloaded, the browser will not issue any GET requests for the resource until the expiry date or maximum age is reached."

  2. Last-Modified and ETag

    These both offer caching of sorts, but Last-Modified followes a browser-specific heuristic, and ETag requires more HTTP requests, although they can be light weight when a 304 Not-Modified is returned. Regardless, these both may involve more GET requests.

A static jQuery version should never change. So I'd think that it should be served with a far future Expires header. More so, an asset like this that can be used across many sites would ideally not require a new GET request (even if it's small for checking ETags validity).

It seems there is only a ETag used and no Cache-Control or Expires header:

Status: HTTP/1.1 200 OK
Accept-Ranges:  bytes   
Content-Type:   application/x-javascript; charset=utf-8 
Date:   Mon, 05 Dec 2011 19:45:57 GMT   
ETag:   "9e69008-16eac-5177b900"    
Last-Modified:  Tue, 22 Nov 2011 02:11:16 GMT   
Server: ECS (fra/D59E)  
Vary:   Accept-Encoding 
X-Cache:    HIT 
Content-Length: 93868   
Connection: close

from: http://web-sniffer.net/?url=http%3A%2F%2Fcode.jquery.com%2Fjquery-1.7.1.min.js

So is there a reason not to set a far future Expires header or use Cache-Control for a situation like this? Am I misreading something here? Or is it a misconfiguration by the jQuery folks?

ehc, I'll touch on the problem but respectfully point out that this may not be the best place to voice such issues, since it's not really a SO question? (maybe? but whatevs)

Actually, we aren't using the proCDN right now - we're on the old one that doesn't let us customize anything :/ But! Good news! They're hooking us up with some new stuff, procdn (which has some stats/tracking stuff too which will be kinda neat) and some faster boxes so we should be able to get our caching much better - cuz you're right, it's not that great now. Also, we can't do any custom headers or types or anything, which is nice to do sometimes.

Sounds like you've done a lot of research into this - pop into #jquery-dev sometime (same username for me) if you'd like to offer your help to fix some of these settings 'n stuff, since you seem knowledgeable and interested to help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM