10

In my GAE app I have the following handler in app.yaml:

- url: /lang/strings.js
  script: js_lang.py

So a call to /lang/strings.js will actually map to the js_lang.py request handler which populates the response as application/javascript. I want this response to be cached in the browser so that the request handler only gets called once in a while (for example when I "invalidate" the cache by importing /lang/strings.js?v=xxxx when I deploy a new version of the app.

For normal static content, there is the default_expiration element, which is very handy. And results in http response headers like this:

Expires: Fri, 01 Apr 2011 09:54:56 GMT
Cache-Control: public, max-age=600

Ok, the question: is there an easy way for me to return headers such as this, without having to explicitly set them? Alternatively, is there a code snippet out there that accepts a few basic parameters such as "days" and produces the expected http-headers?

Edit 12 April 2011

I solved this very by simply setting the two headers Expires and Cache-Control like this:

import datetime
thirty_days_in_seconds = 4320000
expires_date = datetime.datetime.now() + datetime.timedelta(days=30)
HTTP_HEADER_FORMAT = "%a, %d %b %Y %H:%M:00 GMT"        

self.response.headers["Expires"] = expires_date.strftime(HTTP_HEADER_FORMAT)
self.response.headers["Cache-Control"] = "public, max-age=%s" % thirty_days_in_seconds
Jon Nylander
  • 8,743
  • 5
  • 34
  • 45
  • 1
    related: http://stackoverflow.com/questions/2426103/asking-browsers-to-cache-as-aggressively-as-possible – balpha Apr 01 '11 at 11:32
  • Thanks balpha - I will probably end up setting agressive caching headers explicitly. But posting this to see if someone has already solved this, or if there some yaml request handler setting I have overlooked. – Jon Nylander Apr 01 '11 at 11:41

1 Answers1

6

Have a look at Static serving blog post by Nick.

There's everything you need to know about Conditional request and how to properly get and set the correct HTTP headers:

  • Http Request header handling (If-Modified-Since,If-None-Match)
  • Http Response headers handling (Last-Modified, ETag)
Community
  • 1
  • 1
systempuntoout
  • 71,966
  • 47
  • 171
  • 241