为什么 Firefox 会往返服务器来确定我的文件是否被修改?

发布于 2024-08-12 01:11:05 字数 2169 浏览 4 评论 0原文

我的网站上有一些静态内容,我已为其设置了缓存(使用 Asp.NET MVC)。根据 Firebug,我第一次打开页面时,Firefox 会发送以下请求:

GET /CoreContent/Core.css?asm=0.7.3614.34951
Host: 127.0.0.1:3916
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
Accept: text/css,*/*;q=0.1
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://127.0.0.1:3916/Edit/1/101
Cookie: .ASPXAUTH=52312E5A802C1A079E2BA29AA2BFBC5A38058977B84452D62ED52855D4164659B4307661EC73A307BFFB2ED3871C67CB3A9AAFDB3A75A99AC0A21C63A6AADE9A11A7138C672E75125D9FF3EFFBD9BF62
Pragma: no-cache
Cache-Control: no-cache

我的服务器回复如下:

Server: ASP.NET Development Server/9.0.0.0
Date: Mon, 23 Nov 2009 18:44:41 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 1.0
Cache-Control: public, max-age=31535671
Expires: Tue, 23 Nov 2010 18:39:12 GMT
Last-Modified: Mon, 23 Nov 2009 18:39:12 GMT
Vary: *
Content-Type: text/css
Content-Length: 15006
Connection: Close

到目前为止,一切顺利。但是,如果我刷新 Firefox(不是缓存清除刷新,只是普通刷新),在该刷新周期中,Firefox 将再次向服务器发送此请求:

GET /CoreContent/Core.css?asm=0.7.3614.34951
Host: 127.0.0.1:3916
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
Accept: text/css,*/*;q=0.1
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://127.0.0.1:3916/Edit/1/101
Cookie: .ASPXAUTH=52312E5A802C1A079E2BA29AA2BFBC5A38058977B84452D62ED52855D4164659B4307661EC73A307BFFB2ED3871C67CB3A9AAFDB3A75A99AC0A21C63A6AADE9A11A7138C672E75125D9FF3EFFBD9BF62
If-Modified-Since: Mon, 23 Nov 2009 18:39:20 GMT
Cache-Control: max-age=0

我的服务器响应304 Not Modified

为什么 Firefox 会发出第二个请求?在第一个响应中,我说缓存一年内不会过期(我打算在情况发生变化时使用查询参数)。我是否必须添加另一个响应标头来防止这种额外的往返?

编辑:无论我是否按刷新,或者是否再次转到该页面(或引用相同外部文件的不同 URL),都无关紧要。 Firefox 又做了同样的事情。另外,我并不声称这是 FF 中的错误,我只是想知道是否有另一个我可以设置的标头,这意味着“此文档永远不会改变,不要再打扰我”。

I have some static content on my web site that I have set up caching for (using Asp.NET MVC). According to Firebug, the first time I open the page, Firefox sends this request:

GET /CoreContent/Core.css?asm=0.7.3614.34951
Host: 127.0.0.1:3916
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
Accept: text/css,*/*;q=0.1
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://127.0.0.1:3916/Edit/1/101
Cookie: .ASPXAUTH=52312E5A802C1A079E2BA29AA2BFBC5A38058977B84452D62ED52855D4164659B4307661EC73A307BFFB2ED3871C67CB3A9AAFDB3A75A99AC0A21C63A6AADE9A11A7138C672E75125D9FF3EFFBD9BF62
Pragma: no-cache
Cache-Control: no-cache

Which my server replies to with this:

Server: ASP.NET Development Server/9.0.0.0
Date: Mon, 23 Nov 2009 18:44:41 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 1.0
Cache-Control: public, max-age=31535671
Expires: Tue, 23 Nov 2010 18:39:12 GMT
Last-Modified: Mon, 23 Nov 2009 18:39:12 GMT
Vary: *
Content-Type: text/css
Content-Length: 15006
Connection: Close

So far, so good. However, if I refresh Firefox (not a cache-clearing refresh, just a normal one), during that refresh cycle Firefox will once again go to the server with this request:

GET /CoreContent/Core.css?asm=0.7.3614.34951
Host: 127.0.0.1:3916
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
Accept: text/css,*/*;q=0.1
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://127.0.0.1:3916/Edit/1/101
Cookie: .ASPXAUTH=52312E5A802C1A079E2BA29AA2BFBC5A38058977B84452D62ED52855D4164659B4307661EC73A307BFFB2ED3871C67CB3A9AAFDB3A75A99AC0A21C63A6AADE9A11A7138C672E75125D9FF3EFFBD9BF62
If-Modified-Since: Mon, 23 Nov 2009 18:39:20 GMT
Cache-Control: max-age=0

to which my server responds 304 Not Modified.

Why does Firefox issue this second request? In the first response, I said that the cache does not expire for a year (I intend to use query parameters whenever things change). Do I have to add another response header to prevent this extra roundtrip?

Edit: It does not matter whether I press refresh, or whether I go to the page again (or a different URL, which references the same external files). Firefox does the same again. Also, I don't claim this to be a bug in FF, I just wonder if there is another header I can set which means "This document will never change, don't bother me again".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

游魂 2024-08-19 01:11:06

显然,该问题是由于将 Vary: * 标头添加到响应中造成的。要在 Asp.Net 中删除此选项,请将其添加到 部分内的 web.config 中:

<caching>
    <outputCache omitVaryStar="true"/>
</caching>

对我来说,此开关似乎是“是的,我想要一个工作行为而不是一个非工作行为” -正在工作”,但一旦找到它,翻转它就很简单了。

Apparently, the issue was due to a Vary: * header being added to the response. To remove this in Asp.Net, add this to web.config inside the <system.web> section:

<caching>
    <outputCache omitVaryStar="true"/>
</caching>

To me it seems this switch is "yes, I want a working behaviour rather than a non-working one", but once you find it, flipping it is trivial.

不醒的梦 2024-08-19 01:11:05

对于所有现代浏览器来说,这种行为似乎是“设计使然”。在 IE 中您可以调查相同的情况。按 F5 总是会导致浏览器检查内容是否被修改。在缓存清除请求期间,浏览器不传递 Last-Modified 标头,服务器必须返回 HTTP 200 (而不是 HTTP 304),因此 304 在您的情况下并没有那么糟糕。

This behavior seems to be "by design" for all modern browsers. In IE you can investigate the same situation. Hitting F5 always causes browser to check whether content was modified. During cache-clearing request browser do not pass Last-Modified header and server must return HTTP 200 (not HTTP 304), so 304 in your situation is not so bad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文