google-chrome – 为什么Chrome仍然会缓存此请求?

我有一个页面,其中包含所有缓存控制设置,然而,谷歌浏览器不断将其从缓存中拉出来.我们清空了所有导航历史记录,但在重新加载后,Chrome会再次缓存它:

Request URL:http://stuf.com/path/to/foo
Request Method:GET
Status Code:200 OK (from cache)
Response Headers
Accept-Ranges:bytes
Age:0
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Encoding:gzip
Content-Language:fr
Content-Length:7289
Content-Type:text/html; charset=utf-8
Date:Fri, 17 Jul 2015 23:19:54 GMT
Expires:Fri, 01 Jan 2010 00:00:00 GMT
Server:nginx
Vary:Accept-Language, Cookie, Accept-Encoding
Via:1.1 varnish
X-Varnish:1867509088
X-Varnish-Cache:MISS
Request Headers
Provisional headers are shown
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36

我们确实有清漆设置,但正如您从X-Varnish-Cache中看到的那样,这是一个未命中.此外,状态代码部分确实指出Chrome正在使用缓存.

最佳答案 在您的响应标头中,Chrome指出年龄为0,即响应已缓存一秒或更短时间.

它应该可以工作,如果您等待超过一秒或包括缓存验证器:an ETag or a Last-Modified header,它允许浏览器触发重新验证(条件请求)而不是正常的GET请求.

问题可能是必须重新验证(max-age = 0时你不需要):

When the must-revalidate directive is present in a response received by a cache, that cache MUST NOT use the entry after it becomes stale to respond to a subsequent request without first revalidating it with the origin server

如果没有ETag或Last-Modified标头,则无法进行重新验证.

此外,您可以跳过Expires标头:

If a response includes both an Expires header and a max-age directive, the max-age directive overrides the Expires header, even if the Expires header is more restrictive. This rule allows an origin server to provide, for a given response, a longer expiration time to an HTTP/1.1 (or later) cache than to an HTTP/1.0 cache.

RFC开始.

点赞