English 中文(简体)
Why does firefox round-trip to the server to determine whether my files are modifed?
原标题:

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".

最佳回答

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.

问题回答

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.





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签