Monday, December 1, 2008

Google Chrome caches 301 redirects

Ever since the release of Chrome I've alternating between it and Firefox for casual browsing. At work, I mainly use Chrome for information seeking because of its sheer speed, but nothing beats Firefox + Firebug for web development and javascript debugging. Sometimes, however, I mix my browsers and run some non-javascript stuff on Chrome (of course I test javascript-heavy stuff on all main browsers).

So a couple of days ago I was checking a seemingly innocent piece of code on Chrome, something like:

Response.StatusCode = 301;
Response.RedirectLocation = newUrl;

and I messed up a querystring parameter on newUrl. I fixed it, ran it again, and it still pointed to the wrong URL. Hmm, was I using the right port (sometimes I run multiple branches of different ports)? Yes. So I set a breakpoint on Response.RedirectLocation, and it didn't fire. WTF?! After some more hair-pulling and cursing (spanish is a really wonderful language for cursing, you know, there's even a whole mathematical theory behind it(1)) I ran it on Firefox and it worked as expected. And then it hit me: it is a permanent redirection after all, isn't it? So why not cache it? I ran it on every other browser I had (IE, Safari, Opera), they all behaved like Firefox, the only one caching the redirect was Chrome.

I couldn't believe it, so I wrote a simple "proof": an app that gives you a link, when you click the link it creates a cookie and redirects (301) to another page where it shows the content of the cookie and deletes it. Source code is here (nothing interesting) and you can run it here. Point your browser to http://localhost:12345/. Click the link. You'll see "Original=/first" as the response. Ok, now go back to root. Click the link again. Now, if your browser cached the redirect, you'll see "Original=", since the /first URL wasn't requested again. Go back to root, hit F5 and it clears the cache.

I don't mean to say this is wrong behavior, it's just that it's the first browser I see that implements 301 redirect caching, it's something to be aware of while developing and testing.

(1) For the non-Spanish-speaking readers: it's just a joke ;-)

15 comments:

Niels said...

Aaaargh! I just spent an hour trying to figure out what was wrong with my htaccess redirect, now I realize it's just stupid Chrome. And I don't know how to clear the redirect cache either. At least I know it's not just me.

mausch said...

Clear browsing data -> Empty the cache seems to do the trick

Asylum-ET said...
This comment has been removed by the author.
Asylum-ET said...

This one got me for about an hour as well before I figured out that it must be caching the redircts. A quick google search confirmed that when I found your website. Leave it to google to throw a wrench in the gears. I mean really the way I see chrome is for dev use "only" I cannot imagine any of my clients using it so then with it known that it is mostly dev users why cache 301's as that is a common mistake of a dev and google is now costing these devs much wasted time. On that same token caching the 301 makes perfect sense for a "users" browser.

ben said...

For the record, I'm pretty sure Firefox also caches 301 redirects at this point (I'm using version 3.5.5).

Anonymous said...

I'm having this issue as well, chrome has cached a redirect from a .htaccess file, and i can't clear it no matter what i try. IE and FF both work but they didnt get the bad redirect cached. lookin for help!

Steev said...

Just mentioning this here, too. It makes sense, a 301 means the resource has been moved permanently. Caching just makes that process more efficient.

coimccracken said...

I wonder, is this issue still occurring? Firefox and IE did not follow chrome's 301 redirect caching did they?

Tito said...

I feel lame that everyone else only spent one hour on this. I was screaming away for at least 2-3 before I caught on. Thanks very much.

Maniquí said...

Tito, don't feel bad.
I spent several hours too (like 3 or 4, or even more).
I wasn't really suspecting of Chrome, but of GoDaddy, as there is some track record that GoDaddy caches your .htaccess and that you have to wait some minutes until your changes in .htaccess apply successfully.

It was driving me crazy, really. To be fair, I don't like this Chrome "feature". It hurts more than it helps, all for the sake of speed...

Andrei Z said...

I was tricked by the caching too. Lost one hour with that.

cp1252 said...

im seeing IE (version 9) cache these as well. can be pretty frustrating, i had it still caching a whole day later.

asad_ddos said...

same issue here

Jason said...

Damn you Chrome, I spent a morning debugging my apache config trying to understand why I still see that preposterous redirect.

phenotype said...

Has anyone had this issue on Chrome even AFTER the user has cleared their cache?
Are 301 redirects stored somewhere special?

As ever this never happens on my machines or anyone else's I can find, so I figure this was a throwback to an old bug