为什么Firefox为什么忽略缓存头并在刷新时重新验证? [英] Why does Firefox ignore cache headers and revalidate on refresh?

查看:182
本文介绍了为什么Firefox为什么忽略缓存头并在刷新时重新验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些不可变的图像资源,可以永久缓存。 Chrome似乎尊重我的响应标头,并且不重新验证资源:





以下是Chrome中这些资源之一的示例。如您所见,我包括缓存控件:public,max-age expires etag 最后修改,然后从内存缓存提供资源:








Firefox,但是,不遵守这些标头并重新在每次加载时验证资源!每当页面加载时,我的服务器都会收到针对每个个人资料图片的请求,并返回304:





以下是此类请求的示例,结果为304:



的标准。我确保在devtools中启用了缓存。我也尝试过关闭devtools,并且在服务器日志中始终看到304。



我发现这仅在刷新页面时发生。普通刷新不是shift或shift命令,而是普通刷新。

解决方案

简单地:Firefox在浏览器刷新时重新验证缓存的内容。



过去,所有浏览器都这样做。一次假设用户正在积极刷新其页面可能是合理的,这是因为出现了问题,需要从头开始。现在,随着显示实时内容的网站的出现,刷新的使用可能大不相同。



Chrome和Firefox似乎采取了不同的处理方式有了这个。 Chrome浏览器的方法是使其刷新行为更智能,更复杂,而Firefox选择使用显式依赖开发人员,以指示非陈旧资源永远不需要使用 Cache-Control:不可变 响应标头。 (有关此区别的更多信息,请参见此答案)。



如果此刷新行为对于您的应用程序而言是重要的用例(而不是仅用于调试目的),则添加 Cache-Control:不可变应该可以解决此问题Firefox。


I have some image resources that are immutable and can be cached forever. Chrome seems to respect my response headers, and does not re-validate the resources:

Here's an example of one of these resources in Chrome. As you can see, I include cache-control: public, max-age, expires, etag and last-modified and the resource is served from "memory cache":


Firefox, however, does not respect these headers and re-validates the resources on every load! My server is hit with a request for each profile pic every time the page loads, and returns a 304:

Here is an example of on such request that results in a 304:

I can't figure why Firefox ignores the cache headers and keeps going to the server for the 304. I've experimented with various cache-related headers and read the standard on what's "cacheable". I've ensured that caching is enabled in devtools. I've tried with devtools closed too, and I keep seeing the 304s in my server logs.

I've discovered that this only happens on a page refresh. A plain refresh, though, not shift- or shift-command-, but just a plain refresh. That's not the behavior I was expecting.

解决方案

Put simply: Firefox revalidates cached content on browser refresh.

That used to be what all browsers did. It was probably reasonable at one time to assume that if a user was actively refreshing their page it was because something went wrong and they needed to start from scratch. Now, with the advent of sites showing real-time content, the use of "refresh" may be quite different.

Chrome and Firefox seem to be taking different paths to deal with this. Chrome's approach is to make its refresh behavior smarter and more sophisticated, while Firefox has chosen to rely on the developer explicitly indicating that a non-stale resource never needs revalidation by using the Cache-Control: immutable response header. (See this answer for more on this distinction).

If this refresh behavior is an important use case for your application (as opposed to just something you're using for debugging purposes) adding Cache-Control: immutable should solve this problem in Firefox.

这篇关于为什么Firefox为什么忽略缓存头并在刷新时重新验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆