When Tutorials Lie

Wed 13 January 2010 | -- (permalink)

The Google App Engine documentation tells you that you can set the "Expires" header on an image like this:

~~~~ {lang="python"} class StatusImageHandler(webapp.RequestHandler): def get(self): img_data = get_status_image_for_current_user() self.response.headers["Content-Type"] = "image/png" self.response.headers.add_header("Expires", "Sun, 14 Mar 2010 00:48:10 GMT") self.response.out.write(img_data) ~~~~

But in Firefox at least, that doesn't work. If you check your Firefox's cache by putting about:cache in your address bar you'll see the Expires date on your image set to something like Expires: 1969-12-31 16:00:00.

A little detective work with the Live HTTP Headers extension for Firefox showed me that the Cache-Control header was set to 'no-cache', which means that the Expires header is ignored. The fix is to set Cache-Control to public:

~~~~ {lang="python"} class StatusImageHandler(webapp.RequestHandler): def get(self): img_data = get_status_image_for_current_user() self.response.headers["Content-Type"] = "image/png" self.response.headers["Cache-Control"] = "public" self.response.headers.add_header("Expires", "Sun, 14 Mar 2010 00:48:10 GMT") self.response.out.write(img_data) ~~~~