1

This is just making me angry. I can't figure out why all the resources in my page are being requested EVERY single time.

E.g. my site.css returns the following headers (using fiddler):

HTTP/1.1 200 OK
Server: ASP.NET Development Server/9.0.0.0
Date: Mon, 29 Nov 2010 17:36:21 GMT
X-AspNet-Version: 2.0.50727
Content-Length: 9093
Cache-Control: public, max-age=2592000
Expires: Wed, 29 Dec 2010 17:36:21 GMT
Last-Modified: Mon, 08 Nov 2010 17:20:16 GMT
Content-Type: text/css
Connection: Close

But every time I hit refresh I see all the resources (css,js,images) getting re-requested. I have control over the headers returned for any and all of these resources, but I haven't figured it out yet.

I have even debugged my ASP.NET app and the HttpModule is definitely being asked for the resources again.

Can someone give me an idea of what to do?

EDIT: Ok, I removed must-revalidate, proxy-revalidate from the headers and that is getting me closer to where I want to be, now when I press back it still requests my css/js files when I press back.

Is there anything more I can do to avoid this?

Jose
  • 10,891
  • 19
  • 67
  • 89
  • 9
    Erm, the *Cache-Control* says *must-revalidate* and *proxy-revalidate*. Furthermore, browsers behave differently when returning to an already visited page versus refreshing a page. – Gumbo Nov 29 '10 at 17:48
  • 2
    @Gumbo +1: "Furthermore, browsers behave differently when returning to an already visited page versus refreshing a page." - I suspect this is the case here. E.g. as far as I remember, Firefox will re-request everything when you hit "refresh". – Andras Vass Nov 29 '10 at 17:53
  • @andras: Firefox doesn't request everything on a regular refresh. However, if you control refresh then it does.. and continues to do so for as long as the browser window is open regardless of the caching headers. – NotMe Nov 29 '10 at 18:07
  • @Chris: Hmmm. This is not the case. I have fired up Firefox (note the pun ;-), and verified it. FF re-request everything from google.com. The difference between **Shift**+Refresh and Refresh is that with **Shift**-Refresh, FF will not send If-Modified-Since, so the server will not even have a chance of answering with a 304 Not Modified. – Andras Vass Nov 29 '10 at 18:14
  • @Chris: so the requests still hit the server with a plain refresh as well. All of them. – Andras Vass Nov 29 '10 at 18:16
  • @Jose: what browser are you using? What if you visit e.g. google in the same window/tab, and come back by retyping the server address? (Not using back/forward/refresh buttons...) – Andras Vass Nov 29 '10 at 18:49
  • @Andras: you might want to check this out: http://stackoverflow.com/questions/385367/what-requests-do-browsers-f5-and-ctrl-f5-refreshes-generate Specifically, the answer by @some whether it happened to work for one particular website is really dependent on the headers that site sent for all of the content to begin with. – NotMe Nov 30 '10 at 18:04
  • @Chris: Well, thanks for the link. If you look at those tables yourself, you will see that FF generally re-requests everything on F5 for all the resources, sending out an 'If-Modified-Since' header as well. Table provided by @Some, F5 row, Firefox 3.x column. Am I missing something crucial when I think that it is contrary to when you say that `Firefox doesn't request everything on a regular refresh`? – Andras Vass Nov 30 '10 at 19:10
  • I have worked on issues in the past where parameters in the URL were causing the browser to not cache correctly. Do all of the URLs for the content you want to cache look like static documents with no parameters? – TheJacobTaylor Nov 30 '10 at 19:18
  • @andras: perhaps we're not really communicating and the fault appears to be mine. The part I appear to have been missing in our conversation is the definition of a request. I was simply saying that it would load from cache vs the server based on which method (f5/ctrl-f5) was chosen. Obviously, there is still at least some type of request regardless. – NotMe Nov 30 '10 at 19:18
  • @Chris: no problem, it may be mine. I assumed that the OP assumes that requests going to the server on F5 can be avoided. Furthermore, I assumed that we all assume that. I hope this clears up everything... ;-) Requests going to the server on a plain F5 are generally expected - the server can just answer with a short 304 for each resource request without sending the resources again. The link you have provided is invaluable anyway, so thx. – Andras Vass Nov 30 '10 at 19:29
  • @Jose, in light of this, what is your browser and what *request* headers do you see when you hit back? – Andras Vass Nov 30 '10 at 19:30

2 Answers2

4

The following links might be of help to you.

Differences in reload behavior between FF and IE

http://blog.httpwatch.com/2008/10/15/two-important-differences-between-firefox-and-ie-caching/

In a nutshell, your caching behavior is going to be determined by the headers and the browser you are using.

What browser are you using for testing? The back button is also handled differently.

Back Button (Browser Behavior)

And, finally, a breakdown of f5/ctrl f5, click, shift click, etc behavior between browsers: What requests do browsers' "F5" and "Ctrl + F5" refreshes generate?

Community
  • 1
  • 1
NotMe
  • 87,343
  • 27
  • 171
  • 245
2

If you are handling the requests in your own module - which seems to be the case - and the request contains an If-Modified-Since header, you can use that to determine whether to respond with a 200 and sending the whole resource again, or just send a 304 and skip sending the js/css/etc contents.

Other than that, expect browsers to re-query resources on hitting F5 / Refresh. It is just that you may skip sending the whole js/css/etc and return a 304 if everything is OK. Other than that, @Chris's answer covers pretty much everything else.

What are the request headers you see when you hit back?

Andras Vass
  • 11,478
  • 1
  • 37
  • 49