如何在保留 Last-Modifed 的同时让 Chrome 等浏览器不要"from cache"?


我有个WEB应用,服务器端数据是动态生成的,通过程序在服务器端缓存,用 Last-Modified 来标明更新时间。但是在 url 上不加随机数的时候,Google Chrome 总是 "from cache" 而不向服务器发起请求。只有在浏览器开启 ”isable cache (while DevTools is open)“ 才会正常的每次发起请求,服务器返回 304 它才从 cache 取。

我试着加了 Cache-Control: no-cache, Pragma: no-cache 都不行,他总是在第二次就"from cache",我又需要用 Last-Modifed 来减少一些服务器响应的数据量,有什么办法解决这个问题吗?


问题补充:

如果单独请求这个地址,是正常的,第二次、第三次浏览器发出 If-Modififed-Since,但是当请求是在页面内发出的,比如 <script type="text/javascript" src="xxxxxx"></script> 则他总是在首次后 from cache,除非加上 Expires 让它过期。


发现有人提 BUG https://code.google.com/p/chromium/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Pri%20M%20Iteration%20ReleaseBlock%20Cr%20Status%20Owner%20Summary%20OS%20Modified&groupby=&sort=&id=48445 ,还发现我去年就被这个问题折腾过了(去年在上面留言了)

from cache


用Etag替代Last-modified就不再有问题了,发现Google正是这么干的。

apache php nginx chrome http

萝莉D逆袭 10 years, 1 month ago

 Cache-Control: no-cache, no-store, max-age=0
Pragma: no-cache
Expires: Sat, 26 Jul 1997 05:00:00 GMT

上面这个是禁止所有缓存的。

不过根据你的描述我更建议这样写


 Cache-Control: max-age=0, private, must-revalidate

巧克力爱情 answered 10 years, 1 month ago

Your Answer