使IE缓存资源,但始终重新验证 [英] Make IE to cache resources but always revalidate

查看:153
本文介绍了使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之外,主要的浏览器会缓存响应并重新验证它。为了防止存储请求,有无商店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.)

这些是关键的头字段:

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




  • 需要最后修改(或ETag)作为验证器

  • Expires -1告诉资源是陈旧的,必须重新验证

  • Cache-Control不得包含no-cache或no-store

  • 这篇关于使IE缓存资源,但始终重新验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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