zerosleeps

Since 2010

Open a random post

I though this suggestion to add an “open a random post” button sounded fun, so I’ve just added that feature to zerosleeps.com. You’ll find it in the navigation bar. A simple way for someone to get a feel for the nonsense that I’ve posted here over the years.

I didn’t want to generate a link to a random post every time the navigation bar is rendered. Instead, I wanted the random thingy to have it’s own URL, which in turn redirects the requestor to the absolute URL for a random post.

On top of that I didn’t want to do this using HTTP 302. Django’s RedirectView makes it easy to serve 302 responses, but the semantics of that status code didn’t feel quite right - it’s supposed to indicate “that the target resource resides temporarily under a different URI”.

HTTP 303 felt like a better fit to me: “the server is redirecting the user agent to a different resource … which is intended to provide an indirect response to the original request”. A subclass of HttpResponseRedirectBase made light work of that requirement.

After deployment I discovered that Safari (and only Safari as far as I can tell) was caching the 303 response: you could visit /random_post/ as many times as you wanted, but you’d always get the same page back within the same 180 second period. Fair enough I suppose: I’ve told my web server to add a Cache-Control header to anything generated by Django, so it’s not unreasonable for Safari to cache the response to the initial redirect in addition to caching the final page. It’s interesting that other browsers don’t do that though.