nginx 版本:nginx/1.14.0 (Ubuntu)
正在尝试研究如何处理浏览器缓存。
你能向我解释一下为什么在 html 情况下浏览器会向服务器发送请求,而在 css 情况下却不会?
换句话说,为什么在 html 中我们有 304,但在 css 中我们有 200(来自磁盘缓存)?
最佳答案
服务器没有向浏览器提供任何有关缓存其资源多长时间的信息。 (也就是说,它不包含 Cache-Control
或 Expires
header 。)因此,浏览器可以自由地提出自己的启发式新鲜度,如 RFC 7234 中所述:
Since origin servers do not always provide explicit expiration times, a cache MAY assign a heuristic expiration time when an explicit time is not specified, employing algorithms that use other header field values (such as the
Last-Modified
time) to estimate a plausible expiration time.
大概浏览器为静态 CSS 资源分配的新鲜时间比为 HTML 页面分配的新鲜时间更长。这是有道理的。
如果您关心资源的缓存方式,答案很简单 - 使用适当的缓存 header 提供明确的指示。
关于http - 304 和 200 的区别(来自磁盘缓存),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61781170/