Chrome 在 Glassfish 服务器上缓存资源时遇到问题。尽管存在 Last-Modified header ,但不会发送过期和无缓存 header ,并且 Chrome 会缓存资源(大约 4 MB SWF 文件)。
有时 Chrome 会得到 304 代码,有时它只是得到 200(来自缓存)。我了解 304——Chrome 可能会检查最近的上次修改日期和缓存版本来决定。但其他时候它会执行 200(来自缓存),它不会返回任何 header 信息,并且 Chrome 似乎只是假设文件尚未被修改而不是进行检查。
HTTP/S supports local caching of static resources by the browser. Some of the newest browsers (e.g. IE 7, Chrome) use a heuristic to decide how long to cache all resources that don't have explicit caching headers.
但这并没有提供明确的答案。这个启发式是否在任何地方发布?我意识到可能没有固定的答案(例如 30 天),但一些一般准则会很有用。此外,如果设置了 Last-Modified,我不明白为什么 Chrome 不先检查一下。
最佳答案
浏览器认为缓存响应新鲜的时间通常是相对于它上次修改的时间而言的:
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)... If the response has a Last-Modified header field (Section 2.2 of [RFC7232]), caches are encouraged to use a heuristic expiration value that is no more than some fraction of the interval since that time. A typical setting of this fraction might be 10%. [https://www.rfc-editor.org/rfc/rfc7234#section-4.2.2]
Chrome(和其他浏览器)如何计算该值的详细信息可以在源代码 ( An example from Chrome v49 ) 中找到。 Chrome 似乎还会计算相对于 Last-Modified header 的值。
关于google-chrome - 如果未设置过期和/或无缓存 header ,Google Chrome 浏览器会缓存资源多长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9740822/