简体   繁体   English

浏览器是否没有正确遵循 HTTP 规范的缓存控制?

[英]Do browsers not follow the HTTP spec's Cache-Control correctly?

I am somewhat new to web development and have noticed an issue, Browsers seem to not respect the Cache-Control header, I have it set to no-cache, no-store, must-revalidate but yet many of my clients have a cache to begin with (which no-store should prevent according to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#no-store ) and the cache is used rather than revalidating with the server leading to broken pages when I change a JS script referenced in a page, only after I tell them to refresh without cache does the browser then fetch the new file but for the browsers to be compliant with the HTTP protocol and spec, don't they need to respect the no-store policy or are none of the major browsers properly compliant with the HTTP protocol/spec and why haven't they been fixed so we don't need workaround solutions like query strings appended to files or using the file's hash or last modification date?我是 web 开发的新手,注意到一个问题,浏览器似乎不尊重 Cache-Control header,我将其设置为no-cache, no-store, must-revalidate ,但我的许多客户都有缓存开始(根据https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#no-store应该阻止 no-store )并且使用缓存而不是重新验证当我更改页面中引用的 JS 脚本时,服务器导致页面损坏,只有在我告诉他们在没有缓存的情况下刷新后,浏览器才会获取新文件,但浏览器要符合 HTTP 协议和规范,不要他们是否需要尊重no-store政策,或者没有一个主要浏览器正确地符合 HTTP 协议/规范,为什么他们没有被修复,所以我们不需要变通解决方案,如附加到文件的查询字符串或使用文件的 hash 或最后修改日期?

You initially served the resource without cache headers.您最初提供没有缓存标头的资源。 In that case, the specification allows the client to choose the cache time itself:在这种情况下,规范允许客户端自己选择缓存时间:

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.由于源服务器并不总是提供明确的过期时间,缓存可以在未指定明确时间时分配启发式过期时间,采用使用其他 header 字段值(例如Last-Modified时间)的算法来估计合理的过期时间.

Different browsers will use different algorithms, but in any case it probably won't be very long.不同的浏览器会使用不同的算法,但无论如何它可能不会很长。 Your problem might have already resolved itself.您的问题可能已经自行解决。

As for query strings, I think your confusion comes from conflating at least three distinct issues.至于查询字符串,我认为您的困惑来自于将至少三个不同的问题混为一谈。 One is the HTTP protocol mechanism for communicating cache policies.一种是用于通信缓存策略的 HTTP协议机制 That is covered in RFC 7234 and mainly involves the proper use of the Cache-Control response header.这在RFC 7234中有所介绍,主要涉及正确使用Cache-Control响应 header。

A separate issue is what cache strategy to use.一个单独的问题是使用什么缓存策略 That is, which resources should be cached and for how long?也就是说,应该缓存哪些资源以及缓存多长时间? There are different ways to approach this, my suggestion would be to follow the best practices discussed here .有多种方法可以解决这个问题,我的建议是遵循此处讨论的最佳实践。

Finally, there's how to fix your mistake if you communicated the wrong cache policy and now need an already-cached resource to be ignored or invalidated.最后,如果您传达了错误的缓存策略并且现在需要忽略或使已缓存的资源无效,则如何解决您的错误 In that case, if possible, you could just use a different resource (ie change the name).在这种情况下,如果可能,您可以使用不同的资源(即更改名称)。 Adding query strings is sometimes suggested here, but it's not a great solution since the standard does not forbid clients from caching resources with query strings.这里有时会建议添加查询字符串,但这不是一个很好的解决方案,因为标准不禁止客户端使用查询字符串缓存资源。

Getting back to your question, you can temporarily fix your mistake (missing Cache-Control headers) by changing the name of the linked resource, or just by waiting a short time for the heuristic expiration time to pass.回到您的问题,您可以通过更改链接资源的名称,或者只是等待很短的时间让启发式过期时间过去来临时修复您的错误(缺少Cache-Control标头)。 Longer term, you should decide how you want your different resources to be cached, and then use Cache-Control to communicate that intent to the browser.从长远来看,您应该决定如何缓存不同的资源,然后使用Cache-Control将该意图传达给浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM