Fix the Firefox cache problem

Apr 11, 2006

For some strange reason, Firefox caches ASP.NET pages even if you tell it not to. This could create problems with the back-button. Normally you can tell the browser how to cache your web page in two ways. You can use meta tags or set some HTTP headers. To be absolutely sure that all browsers understand you cache policy, you could combine the two.

However, when you don’t want the browser to cache your page, Firefox caches it anyway. It does so because it cannot se any difference between how the page looked before and after you pressed the back-button. This issue drove me absolutely nuts today, and it took me a while to come up with a solution.

To make the back-button work with the cache in Firefox, add the ETag header to your page. Its value is a random number, and this will tell Firefox that all pages are different, even when you use the back-button to view a previously viewed page. Here’s an example that tells Firefox not to cache your page. Just add these lines to the Page_Load event:

Random rd = new Random();
Response.AddHeader("ETag", rd.Next(1111111, 9999999).ToString());
Response.AddHeader("Pragma", "no-cache");
Response.CacheControl = "no-cache";
Response.Cache.SetNoStore();
Response.Expires = -1;

I don’t know why the ETag header is necessary in Firefox. I find it rather disturbing that the big browsers cannot agree on network issues like this.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Comments (3) -

Remy
Remy
4/25/2006 12:14:04 AM #

Hey Mads,

Thanks for the info.  I've run into this problem as well and have scoured the web for a workaround.  This seems like a big problem and I can't believe that so few people are talking about it.

It seems to me like ASP.NET is not doing something correctly.  I would think that Firefox's caching policy adheres to rfc2616.  I'm guess IE does things differently and ASP.NET 1.1 wasn't tested with other browsers; big surprise.  Do you know if .NET 2.0 solves this problem?

Anyway, to figure out exactly what's happening, I'll have to sniff request/response headers and try to interpret caching behavior and how the http1.1 spec defines it.

Thanks again.  Good stuff on your blog.  Keep it up.

 Mads Kristensen
Mads Kristensen
4/25/2006 11:34:40 AM #

Hey Remy

Unfortunatly, ASP.NET 2.0 doesn't solve this problem. It behaves exactly the same as ASP.NET 1.1 in that regard. I believe it is Firefox that is the problem, because IE, Opera and Safari gets it right. The important thing is that we found a way that works.

Remy
Remy
4/25/2006 6:09:52 PM #

Mads,

You're ETag solution wouldn't work for me with Firefox 1.5, which uses bfcache (developer.mozilla.org/.../...g_Firefox_1.5_caching).  
This works fine with IE and Firefox:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

Firefox seems to ignore a "no-cache" Cache-Control header, unless you're on https.  "no-store" seems to work all the time.

Pingbacks and trackbacks (1)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.