简体   繁体   中英

No “Expires” and no “Cache-Control” header set, yet “Last-Modified” or “ETag” are being ignored, why?

Chrome still loads the files from cache although the "ETag" changed and the "Last-Modified" is set to a newer date than the cached resource by the server. Why is that??

It's good practice to always set Expires or the max-age in Cache-Control. If you don't specify when the resource expires browsers are allowed to use their own heuristic for caching. See section 13.2.4 of RFC 2616 :

If none of Expires, Cache-Control: max-age, or Cache-Control: s- maxage (see section 14.9.3) appears in the response, and the response does not include other restrictions on caching, the cache MAY compute a freshness lifetime using a heuristic. The cache MUST attach Warning 113 to any response whose age is more than 24 hours if such warning has not already been added.

Also, if the response does have a Last-Modified time, the heuristic expiration value SHOULD be no more than some fraction of the interval since that time. A typical setting of this fraction might be 10%.

The calculation to determine if a response has expired is quite simple:

  response_is_fresh = (freshness_lifetime > current_age) 

Chrome has cached the resource because you haven't indicated whether it should be cached or not. Last-Modified is only used by the browser to verify a resource once the browser has determined that it might be stale.

You'll need to set Expires or Cache-Control to get the caching behaviour that you require.

BTW, for most web sites a good rule of thumb is to expire HTML immediately but cache images, CSS and JavaScript forever:

http://blog.httpwatch.com/2007/12/10/two-simple-rules-for-http-caching/

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