当同时提供 max-age 和 Last-Modified 标头时,Webkit 不会缓存
我正在使用自定义 Web 服务器编写一个基于 Web 的应用程序,但我遇到了 webkit 浏览器无法缓存图像、样式表和 javascript 的问题。
我已经追踪到 Cache-Control: max-age 和 Last-Modified 之间的某种关系。如果两者都被指定,那么 webkit 似乎会忽略 max-age 标头,并在每次使用资源时检查文件是否已被修改。该网站的首页上有一个 iframe,它会导致样式表等在一秒钟内被请求两次!
如果删除“上次修改时间”,则直到第二天才会重新请求文件;然而,第二天的请求将不再是 if-modified-since 请求,要求服务器重新发送所有内容,而不仅仅是 304 标头。
在 IE9、Firefox 10.0 和 Opera 11.61 上,浏览器会正确缓存,并且不会重新请求图像,而只会重新请求 HTML,该 HTML 具有 Cache-Control: no-cache 标头属性。
在 Chrome 16.0.912.77 m 和 Safari 5.1.2 (7534.52.7) 上,每次都会对每个页面上的每个图像发出条件请求。服务器使用 304 标头进行响应,其中再次包含 max-age 属性,但它们都继续请求。
我随响应发送的 HTTP 标头示例是:
HTTP/1.1 200 OK
Date: Mon, 06 Feb 2012 15:12:12 GMT
Cache-Control: max-age=86400
Content-length: 708
Content-type: image/gif
Last-Modified: Fri, 6 Jan 2012 14:39:07 GMT
Server: Webspring
有没有人对如何让这些浏览器都尊重我的缓存标头有任何建议?
所有浏览器都在 Win7 Pro x64 上运行,上面的 HTTP 标头是 Fiddler 的原始输出,因此这正是浏览器接收到的内容。
注意:在发现这是标头字段之间的交互之前,我已经问过之前的问题。我删除了上一个问题,因为它不再准确。
谢谢莫格
I have a writing a web based application with a custom web server and I'm having problems with the webkit browsers not caching images, stylesheets and javascript.
I have tracked it down to some relationship between Cache-Control: max-age and Last-Modified. If both are specified then webkit seems to ignore the max-age header and check if the file has been modified EVERY time the resource is used. The site has an iframe on the first page and it results in stylesheets, etc. being requested twice within a second!
If you remove the Last-Modified then the files will not be re-requested until the next day; however the requests the next day will no longer be if-modified-since requests, requiring the server to resend everything instead of just a 304 header.
On IE9, Firefox 10.0 and Opera 11.61 the browsers cache correctly and don't re-request the images, only the HTML, which has a Cache-Control: no-cache header attribute.
On Chrome 16.0.912.77 m and Safari 5.1.2 (7534.52.7) a conditional request is made for every image on every page, every time. The server responds with a 304 header, again containing the max-age attribute but they both keep requesting.
An example HTTP header I'm sending with a response is:
HTTP/1.1 200 OK
Date: Mon, 06 Feb 2012 15:12:12 GMT
Cache-Control: max-age=86400
Content-length: 708
Content-type: image/gif
Last-Modified: Fri, 6 Jan 2012 14:39:07 GMT
Server: Webspring
Does anyone have any suggestions of how I can get these browsers to all respect my cache headers?
All the browsers are running on Win7 Pro x64 and the HTTP header above is the raw output of Fiddler, so that is exactly what the browser is receiving.
Note: I had asked a previous question before I discovered this was an interaction between header fields. I have deleted the previous question as it was no longer accurate.
Thanks
Mog
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,但它不是 Webkit 范围内的; Safari 和 Chrome 在隐身模式下运行良好。我尝试禁用所有扩展,这没有什么区别,但清除整个缓存似乎已经修复了它。
我的猜测是,如果您第一次将 Cache-Control 标头添加到您的网站,那么 Chrome 不会正确覆盖其缓存中的旧标头。这会给现有用户正确使用新的缓存设置带来一些问题。
I was having the same problem but it wasn't Webkit wide; Safari and Chrome in incognito work fine. I tried disabling all extensions which made no difference but clearing the entire cache seemed to have fixed it.
My guess is that if you add the Cache-Control header to your site for the first time then Chrome doesn't correctly overwrite the old headers in its cache. This poses some problems with getting existing users over to your new cache settings properly.