gpt4 book ai didi

http - 缓存控制 header 和条件请求

转载 作者:可可西里 更新时间:2023-11-01 17:09:42 34 4
gpt4 key购买 nike

带有Cache-Control header 的响应是否应该在未来某个时间点发送条件请求,以便在下次需要资源时发送条件请求?

例如,我已将服务器设置为返回一个 Cache-Control header 以在 10 天后过期,并且确实来自服务器的资源的第一个响应是 Http Status: 200Cache-Control max-age=864000 header 。但是,第二次需要资源时,会发出另一个请求,这次返回 Http Status: 304。两次 Last-Modified header 都是过去的某个日期。

我得到 304 状态的事实并不令人惊讶,因为所有资源都没有改变,然后使用了缓存版本。然而,显然是有条件的请求是否应该首先提出?我的印象是,如果没有超过缓存控制 header 的最大期限,那么浏览器甚至不应该发出请求,而应该只使用资源的缓存版本。

我首先认为这是一个 IE9 浏览器问题,因为这是我测试它的浏览器,经过一些谷歌搜索后,我在 caching improvements in IE9 上找到了以下文章.这篇文章证实了我的想法,即不应对未过时的缓存资源发出请求:

If the browser later needs a resource which is in the local cache, that resource’s headers are checked to determine if the cached copy is still fresh. If the cached copy is fresh, then no network request is made and the client simply reuses the resource from the cache.

If a cached response is stale (older than its max-age or past the Expires date), then the client will make a conditional request to the server to determine whether the previously cached response is still valid and should be reused. The conditional request contains an If-Modified-Since and/or If-None-Match header that indicates to the server what version of the content the browser cache already contains. The server can indicate that the client’s version is still fresh by returning HTTP/304 Not Modified headers with no body, or it can indicate that the client’s version is obsolete by returning a HTTP/200 OK response with the new version of the content.

我还检查了 IE 的 Internet 选项,并仔细检查了 Check for newer versions of stored pages 选项设置为 Automatically 这应该让浏览器决定何时进行对资源的请求。

为了更好的衡量,我还在 Firefox 和 Chrome 中对此进行了测试,发现这不是 IE9 的问题,因为这两种浏览器的响应方式与 IE9 相同,并发送条件响应。

如果为未过时的缓存项发送条件请求,这是正常行为吗?

最佳答案

你是对的。通常情况下,如果缓存仍然有效,则不应发出条件请求。这意味着还有其他事情正在使浏览器去检查缓存。继续阅读:

There are a number of reasons why IE might make a conditional request for an item that is already in the cache:

  • The cached item is no longer fresh according to Cache-Control or Expires
  • The cached item was delivered with a VARY header
  • The containing page was navigated to via META REFRESH
  • JavaScript in the page called reload on the location object, passing TRUE for bReloadSource
  • The request was for a cross-host HTTPS resource on browser startup
  • The user refreshed the page

因此,您必须深入挖掘才能了解发生了什么。您可以在以下链接中获取更多信息:http://blogs.msdn.com/b/ieinternals/archive/2010/07/08/technical-information-about-conditional-http-requests-and-the-refresh-button.aspx

关于http - 缓存控制 header 和条件请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23737221/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com