http - 304 和 200 的区别(来自磁盘缓存)

标签 http browser-cache

nginx 版本:nginx/1.14.0 (Ubuntu)

正在尝试研究如何处理浏览器缓存。

你能向我解释一下为什么在 html 情况下浏览器会向服务器发送请求,而在 css 情况下却不会?

换句话说,为什么在 html 中我们有 304,但在 css 中我们有 200(来自磁盘缓存)?

enter image description here

enter image description here

最佳答案

服务器没有向浏览器提供任何有关缓存其资源多长时间的信息。 (也就是说,它不包含 Cache-ControlExpires 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/

相关文章:

php - Guzzle 中的异步请求正文

security - 一次性csrf token的优点

internet-explorer - Internet Explorer 与 Chrome 缓存

javascript - 为什么从 Vue.js 中挂载的钩子(Hook)发送的 AJAX GET 请求总是忽略浏览器缓存?

javascript - 在不同页面中加载 javascript 的最佳(快速)方法是什么?

javascript - 使用请求在 node.js 中传输图像与发送回调正文

javascript - Firefox 4.0.1 忽略过期的 HTTP header

java - 我怎么能改变 "http.connection-manager.timeout"?

javascript - jQuery html 响应

javascript - 强制浏览器下载具有相同 src 的多个脚本标签