Tuesday, January 27, 2009

When is a permanent redirection not permanent?

Let's say I have a flatmate called John. One day, his friend Simon rings:
Simon: "Hello, is John there please?"
Me: "No, he's at his girlfriend's this afternoon. Her number is ..."
Simon: "Ok, thanks."
No problems here. But eventually, John decides to move in with his girlfriend, but forgets to tell Simon the exact date.
Simon: "Hello, is John there please?"
Me: "No, he's moved in with his girlfriend. His new number is ..."
Simon: "Ok, thanks."
So, now I no longer have a flatmate called John. Then, one day:
Simon: "Hello, is John there please?"
Me: "No, he moved out, remember? His new number is ..."
Simon: "Ok, thanks."
Then another day:
Simon: "Hello, is John there please?"
Me: "No, I told you before, his new number is ..."
Simon: "Ok, thanks."
This is obviously wrong. When John was out for the afternoon, it was okay for Simon to ring and ask for John. But when I told Simon that John no longer lives here, Simon should just remember John's new number and start ringing that instead.

This, ladies and gentleman, is the difference between a temporary redirection and a permanent redirection. Those of us in the know call them a 302 and a 301. They behave exactly like I've described above -- if the server sends a 302, the client should keep checking back with the server for the resource in case its URL changes.

If it sends back a 301 though, it's permanent. Never going to change. Never. So, the client may as well remember that, and indeed, this is what the spec says:
This response is cacheable unless indicated otherwise.
(Note that the spec never really says "Cache it, or else" -- it only gets worked up over things that aren't cacheable.)

So, any decent browser worth its salt will be caching this response, right? Wrong. Although there have been posts asking about this, it turns out that the neither Firefox nor Internet Explorer cache any kind of 3xx redirection. The only one I've come across that does is Google Chrome (as also noted by Mauricio Scheffer).

This is really annoying, and I spent a morning tearing out my hair trying to understand why this wasn't working. Hopefully this post means somebody else loses a bit less time.


So, in summary:

Google Chrome: good
Firefox: bad
Internet Explorer: bad

If you want to test your browser, I recommend the excellent suite of tests written by Mark Nottingham. He has also written an excellent introductory guide to caches, which I suggest you read to understand this area.

2 comments:

Anonymous said...

Who's John?

Stuart said...

A hypothetical flatmate.