当没有明确指示时,浏览器为什么/如何知道缓存内容(html、css、js 等)

发布于 2024-12-25 05:17:41 字数 1021 浏览 2 评论 0原文

我正在寻找 Chirpy 用于 css/js 缩小、压缩等。 我注意到它不支持缓存。它没有任何发送过期标头、etag 等的逻辑。

缺少此功能让我怀疑缓存内容是否不那么重要;慢!评分所以我有点困惑。现在我正在研究缓存,但无法解释为什么从缓存中检索此 css 文件 SuperFish.css

  1. 访问http://www.weirdlover.com(Chirpy 的开发者)

    “初始下载”

  2. 查看初始网络轨迹。请注意,SuperFish.css 没有过期标头。

    “第一次拉”"

  3. 重新访问该页面并再次检查网络跟踪。现在从缓存中检索了 SuperFish.css

    “缓存图像”

为什么重新访问页面时会从缓存中检索SuperFish.css?即使我关闭所有 chrome 实例然后重新访问该页面,也会发生这种情况。

I was looking at Chirpy for css/js minifying,compression, etc.
I noticed it doesn't support caching. It doesn't have any logic for sending expires headers, etags, etc.

The absence of this feature made me question if caching content is not as much of a concern; YSlow! grades this so I'm a little confused. Now I'm researching caching and cannot explain why this css file, SuperFish.css, is being retrieved from cache.

  1. Visit http://www.weirdlover.com (developer of Chirpy)

    Initial Download

  2. Look at initial network track. Notice, there is no expiration header for SuperFish.css.

    First pull

  3. Revisit the page and inspect the network trace again. Now SuperFish.css is retrieved from cache.

    Cached image

Why is the SuperFish.css retrieved from cache upon revisiting the page? This happens even when I close all instances of chrome and then revisit the page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

没︽人懂的悲伤 2025-01-01 05:17:41

这似乎符合 HTTP 规范。

13.4 响应缓存能力

除非受到缓存控制(第 14.9 节)指令的特别约束,否则缓存系统可以始终将成功的响应(请参阅第 13.8 节)存储为缓存条目,如果它是新鲜的,可以在不验证的情况下返回它

13.2.2 启发式过期

由于源服务器并不总是提供明确的过期时间,因此 HTTP 缓存通常会分配启发式过期时间,并采用使用其他标头值(例如上次修改时间)的算法来估计合理的过期时间。

看起来,通过不提供缓存控制标头,并省略过期标头,客户端可以自由地使用启发式方法来生成到期日期,然后根据该日期缓存响应。

etag 的存在对此没有影响,因为 etag 用于重新验证过期的缓存条目,在这种情况下,chrome 认为缓存条目是新鲜的(这同样适用于上次修改),因此它没有'还没有过期。

一般原则是,如果源服务器关心新鲜度,则应明确说明。

This seems to fall with in the HTTP specification.

13.4 Response Cacheability

Unless specifically constrained by a cache-control (section 14.9) directive, a caching system MAY always store a successful response (see section 13.8) as a cache entry, MAY return it without validation if it is fresh

13.2.2 Heuristic Expiration

Since origin servers do not always provide explicit expiration times, HTTP caches typically assign heuristic expiration times, employing algorithms that use other header values (such as the Last-Modified time) to estimate a plausible expiration time.

It would seem by not providing a cache-control header, and leaving out the expires header the client is free to use a heuristic to generate an expiry date and then caches the response based upon that.

The presence of an etag has no effect on this since the etag is used to re-validate an expired cache entry, and in this case chrome considers the cached entry to be fresh (the same applies to last-modified), thus it hasn't yet expired.

The general principle being if the origin server is concerned with freshness it should explicitly state it.

放低过去 2025-01-01 05:17:41

在这种情况下(当服务器不返回 Expires 标头时),浏览器应发出带有 If-Modified-Since 标头的 HTTP 请求,如果服务器返回 HTTP 304 Not Modified,则浏览器从缓存中获取数据。
但是,我发现,现在当数据位于缓存中时,浏览器不会执行任何请求。我认为他们这样做是为了缩短响应时间。

In this case (when server doesn't return Expires header), the browser should make HTTP request with If-Modified-Since header, and if the server returns HTTP 304 Not modified then the browser gets the data from the cache.
But, I see, nowadays browsers don't do any requests when the data is in the cache. I think they behave this way for better response time.

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