Simple aggressive caching with Google App Engine

Feb 6, 2013

For the HTML5 version of my puzzle game Persuasion, I wanted to make sure the static resources (images, stylesheets, javascript etc) get cached for as long as possible but still ensure that I can push out new versions whenever they change.

One way of doing this is to embed a version number in the URL for each resource file. You can then give the resources an expiry time far in the future and, when you need to update them, change the version number and reference them at the new URL.

This is pretty straightforward with Google App Engine since you automatically get a versioned URL for your application at versionstring.yourapplication.appspot.com. So you can keep your app at yourapplication.appspot.com (or a custom domain) and reference your resources at the versioned URL. As a bonus this also means you avoid the overhead of cookie data being sent with every resource request.

For example, in my case I just use base and relative URLs to reference the resources:

index.html:

<html>

<head>
...
<!-- "3-owap" is the name of the current version of my app -->
<base href='//3-owap.persuasiongame.appspot.com/persuasion/' />
...
</head>

<body>
...
<!-- Use relative URL to reference resource file -->
<script src="res/js/main.js" type="text/javascript"></script>
...
</body>

</html>

Then set a long expiration for your resources, e.g. (for a Java app):

appengine-web.xml:
...
<static-files>
<include path="/persuasion/res/**" expiration="365d" />
...
</static-files>
...