保持浏览器中资源的新鲜度并取消缓存的新鲜度检查请求[适用于heroku上的rails 3.1应用程序]
我有很多小图像(大小约为 3kb 左右)和很多 css 和 js 文件。在第一个请求之后,它们被缓存在浏览器上,但是当我重新加载页面时,浏览器会尝试检查缓存内容的新鲜度(通过设置 If-Modified-Since 等)并获取响应 304 未修改。每个验证请求都会严重增加页面加载时间(例如 20 次 300 毫秒)。
如何从浏览器取消与服务器的缓存新鲜度检查?如何指示浏览器在特定时间(例如 1 小时)内使用本地缓存文件/图像,而无需在该时间段内每次重新加载时使用远程服务器重新验证或检查本地缓存的新鲜度?
下面的示例小图像获取标头详细信息[使用rails 3.1,在heroku上]:
响应标头
HTTP/1.1 304 Not Modified
Server: nginx/0.7.67
Date: Thu, 10 Nov 2011 17:53:33 GMT
Connection: keep-alive
Via: 1.1 varnish
X-Varnish: 1968827848
Last-Modified: Tue, 08 Nov 2011 07:36:04 GMT
Cache-Control: public, max-age=31536000
Etag: "5bda917d22f8a144c293f3f19723dbc6"
请求标头
GET /assets/icons/flash_close_button-5bda917d22f8a144c293f3f19723dbc6.png HTTP/1.1
Host: ???.heroku.com
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:6.0.1) Gecko/20100101 Firefox/6.0.1
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://???.heroku.com/
Cookie: ???
If-Modified-Since: Tue, 08 Nov 2011 07:36:04 GMT
If-None-Match: "5bda917d22f8a144c293f3f19723dbc6"
Cache-Control: max-age=0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这行:
告诉浏览器不要长时间请求更新,并将文件存储在可公开访问的缓存中(这意味着对本地计算机公开 - 不是公众)。因此,您的浏览器不应该真正重新检查这些文件。您是否尝试过其他浏览器来验证其他地方是否存在此行为?
尽管如此,考虑到您的文件来自 varnish 缓存而不是您的 dyno,并且以 HTTP 304 的形式返回,20 个文件需要 300 毫秒,这听起来像是一个非常长的时间。然而,这对于用户来说应该是几乎察觉不到的。
This line:
is telling the browser to not ask for updates for a long time, and store the files in a publicly accessible cache (which hear means public to the local machine - not the general public). Your browser should therefore not really be re-checking those files. Have you tried another browser to verify this behaviour exists elsewhere?
Saying all of this though, considering that your files are coming from the varnish cache and not your dyno, and are being returned as HTTP 304, 300ms for 20 files sounds like a very long time. However, This should be barely perceptible to the user.