让 IE 缓存资源但总是重新验证 [英] Make IE cache the resources but always revalidate

查看:11
本文介绍了让 IE 缓存资源但总是重新验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

缓存控制标头no-cache, must-revalidate, private"允许浏览器缓存资源,但通过条件请求强制重新验证.这在 FF、Safari 和 Chrome 中按预期工作.

The cache control header "no-cache, must-revalidate, private" allows browsers to cache the resource but forces a revalidate with conditional requests. This works as expected in FF, Safari, and Chrome.

但是,IE7+8 不发送条件请求,即请求标头中缺少If-Modified-Since",因此服务器以 HTTP/200 而不是 HTTP/304 响应.

However, IE7+8 does not send a conditional request, that is, "If-Modified-Since" is missing in the request header and hence the server responds with HTTP/200 instead of HTTP/304.

以下是完整的服务器响应标头:

Here are the full server response headers:

Last-Modified: Wed, 16 Feb 2011 13:52:26 GMT
Content-type: text/html;charset=utf-8
Content-Length: 10835
Date: Wed, 16 Feb 2011 13:52:26 GMT
Connection: keep-alive
Cache-Control: no-cache, must-revalidate, private

这似乎是一个 IE 错误,但我在网上没有找到任何相关的东西,所以我想知道是否存在另一个标题的缺失或存在导致 IE 行为异常?

This seems like an IE bug, but I haven't found anything related on the web, so I wonder whether maybe the absence or existence of another header makes IE behave strangely?

很好地讨论了 no-cache 和 max-age 之间的区别:Cache-Control: max-age=0 和 no-cache 有什么区别?

A good discussion of the difference between no-cache and max-age: What's the difference between Cache-Control: max-age=0 and no-cache?

推荐答案

我终于想通了.这是一个解释和一个经过测试的解决方案.

I've eventually figured it out. Here is an explanation and a tested solution.

以下站点证实了我的观察:http://blog.httpwatch.com/2008/10/15/two-important-differences-between-firefox-and-ie-caching/

The following site confirms my observation: http://blog.httpwatch.com/2008/10/15/two-important-differences-between-firefox-and-ie-caching/

它表示 IE 不会使用no-cache"指令在本地存储页面,因此总是发送无条件请求.

It says that IE does not locally store pages with the 'no-cache' directive and hence always sends an unconditional request.

还有一篇 MS 支持文章 - https://support.microsoft.com/help/234067/ - 这证实了这一点:

There's also a MS support article - https://support.microsoft.com/help/234067/ - which confirms this:

Internet Explorer 支持 HTTP 1.1 Cache-Control 标头,当指定 no-cache 值时,它会阻止对特定 Web 资源的所有缓存..."

"Internet Explorer supports the HTTP 1.1 Cache-Control header, which prevents all caching of a particular Web resource when the no-cache value is specified..."

这种行为并非完全错误——但这不是 RFC 2616(第 14.9.1 节)的意图.关于'no-cache',它说......缓存不得使用响应来满足后续请求,而无需与原始服务器成功重新验证."所以响应可以被缓存,但必须重新验证它.除 IE 之外的主要浏览器都会缓存响应并重新验证它.为了防止存储请求,有 'no-store' Cache-Control 指令.

This behavior is not entirely wrong -- but it is not what RFC 2616 (sec. 14.9.1) intended. About 'no-cache' it says "... a cache MUST NOT use the response to satisfy a subsequent request without successful revalidation with the origin server." So the response CAN be cached but MUST revalidate it. The major browsers, except for IE, do cache the response and revalidate it. To prevent storing the request, there's the 'no-store' Cache-Control directive.

总之,IE 将 'no-cache' 视为 'no-store'.

以下是以一致的方式为 IE 和其他浏览器启用条件请求的解决方案:

不要使用 no-cache,而是将 Expires 标头设置为过去(或 -1,具有相同的效果).IE 以及其他主要浏览器随后将发送条件请求.(注意,您还应该注意 IE Vary 标头错误,它会阻止缓存.)

Don't use no-cache, but instead set the Expires header to the past (or -1, which has the same effect). IE, as well as the other major browsers, will then send conditional requests. (Note, you should also be aware of the IE Vary header bug, which prevents caching.)

这些是关键的标头字段:

These are the critical header fields:

Last-Modified: Wed, 16 Feb 2011 13:52:26 GMT
Expires: -1
Cache-Control: must-revalidate, private

  • 需要 Last-Modified(或 ETag)作为验证器
  • Expires -1 表示资源已过时,必须重新验证
  • Cache-Control 不得包含 no-cache 或 no-store
  • 这篇关于让 IE 缓存资源但总是重新验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆