javascript - 即使使用 `Cache-Control: max-age`,浏览器是否继续发送资源请求

标签 javascript caching

我正在阅读 this great article关于缓存,那里有以下内容:

Validators are very important; if one isn’t present, and there isn’t any freshness information (Expires or Cache-Control) available, caches will not store a representation at all.

The most common validator is the time that the document last changed, as communicated in Last-Modified header. When a cache has a representation stored that includes a Last-Modified header, it can use it to ask the server if the representation has changed since the last time it was seen, with an If-Modified-Since request.

所以,我想知道即使我指定了 Cache-Control: max-age=3600,浏览器是否会继续发送资源请求(例如 HEAD)?如果没有,那么这个标题的意义何在?是否在 max-age 时间过去后使用?

最佳答案

Cache-Control: max-age=3600 header 表示浏览器会将响应缓存最多 3600 秒。在该时间过去之后,它可能不再提供响应,除非首先确认它仍然是新鲜的。

为此,浏览器可以:

  • 使用普通的 GET 请求获取完整资源(再次传输整个响应主体)
  • 或者根据 ETag (If-None-Match) 或 Last-Modified header (If -Modified-Since),即客户端仅在实际发生更改时才获取响应主体。这当然只有在原始响应中存在验证器时才有可能。

简而言之:同时使用 max-age 和缓存验证器的原因是首先将响应缓存一段时间,然后执行节省带宽的重新验证确认资源的新鲜度。

关于javascript - 即使使用 `Cache-Control: max-age`,浏览器是否继续发送资源请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40342069/

相关文章:

javascript - 如何在 Javascript 中验证文本 oninput() 的 00-00-00-0000 格式

javascript - pg-promise:将函数作为参数传递给 func()

android - 清除 picasso 图像缓存

Jquery 缓存变量

javascript - 如何在不使用 Javascript 调用它的情况下将参数传递给引用的函数

javascript - AngularJS - 引用错误 : $ is not defined

javascript - 如何检测 JavaScript 中引导模式对话框的关闭?

php - 为什么 Yii Redis 缓存 set() 方法不创建第一个参数中指定的键。用于追加命令

javascript - 如何将渲染的组件保存到内存中?

wcf - 连接 WCF 客户端缓存?