保持浏览器中资源的新鲜度并取消缓存的新鲜度检查请求[适用于heroku上的rails 3.1应用程序]

发布于 2024-12-14 23:03:25 字数 1282 浏览 7 评论 0 原文

我有很多小图像(大小约为 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

I have lot of small images (of sizes ~3kb or so) and lot of css and js files. After the first request tey are getting cached on the browser, but when I reload the page the browser is trying to check the freshness of the cached content (by setting the If-Modified-Since etc) and gets the response 304 not modified. Each of this validation request seriously increase the page load time (say 20 time 300ms).

How can I cancel this cache freshness check with the server from the browser? How can instruct the browser to use local cached files/images for certain time (say 1 hour) without re-validating or checking the freshness of the local cache with the remote server for every reload with that time period?

sample small image fetch header details below [using rails 3.1, on heroku]:

Response Headers

    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"

Request Headers

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪花飘飘的天空 2024-12-21 23:03:25

这行:

Cache-Control: public, max-age=31536000

告诉浏览器不要长时间请求更新,并将文件存储在可公开访问的缓存中(这意味着对本地计算机公开 - 不是公众)。因此,您的浏览器不应该真正重新检查这些文件。您是否尝试过其他浏览器来验证其他地方是否存在此行为?

尽管如此,考虑到您的文件来自 varnish 缓存而不是您的 dyno,并且以 HTTP 304 的形式返回,20 个文件需要 300 毫秒,这听起来像是一个非常长的时间。然而,这对于用户来说应该是几乎察觉不到的。

This line:

Cache-Control: public, max-age=31536000

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文