Monday, January 3, 2011

Static Resources and Cache-Busting on Google App Engine Python

Hello and happy new year!

In a couple of previous posts I discussed using Google Closure JavaScript Compiler in your build process to minimise the size of your JavaScript resources. Arguably a bigger win for the end user experience is to ensure the caching of all of your website static resources (JavaScript, stylesheets and images) is properly optimised. In this post I want to share how I think I have achieved this Google App Engine (Python).

Ideally, we would like the end user's browser to use its cache for every static resource, on every request, until that resource changes because we have deployed a new application version. Adding the (relatively new) default_expiration and expiration directives to your app.yaml file will achieve the first part of this requirement:

- url: /images
  static_dir: images
  expiration: "99d"

Behind the scenes, these directives add additional headers to the HTTP response, letting the browser know that they can safely serve these files from cache on subsequent requests.

But how do we tell the end user browser when our static resources have changed?

The solution is called cache-busting, and there are a number of similiar approaches. These involve changing the request URI for the static resources. This could mean appending a version identifier to the individual JS, CSS and image filenames. When the browser sees the request for a nominally different file it has no reason to pull the cached copy, so it requests the file again, with the specified caching headers in place again to prevent it being requested again:

First Request
URI:\images\banner_v1.jpg (requested)

Second Request
URI:\images\banner_v1.jpg (not requested - cached)

Third Request
URI:\images\banner_v2.jpg (updated file - re-requested)

Fourth Request
URI:\images\banner_v2.jpg (not requested - cached)

This technique works, but how you consistently achieve it without introducing errors, confusing source control with trivial changes or simply absorbing too much development time is worth a discussion. Making manual changes to resource file names, the app.yaml static resource directives and the HTML for every application deployment is not very practical.

My approach to removing the development pain associated with cache busting works as follows: Instead of mangling the actual filename of the resource, I change the URI path. This means filenames themselves do not need to change. I also setup static file directives in the app.yaml such that the intermediate path variance (due to changing version) is ignored:

- url: /images/.*/(.*)
  static_files: static/img/\1
  upload: static/img/(.*)

Using a directives such as this, the position or naming of the actual static resource in the source code never changes. For example, the following URI's all serve the same static file: » /static/img/banner.jpg » /static/img/banner.jpg

Within the HTML (or any other resource specifying a URI) the source code contains a known token:

img src="/images/xx-replace-with-version-id-zz/banner.jpg"

My build script, written in Apache Ant, replaces the known token in the prepared build files with the version identifier. The original source files (whose known token works fine in development) are not updated:

<copy overwrite="true" todir="${}">
    <fileset dir="${path.assets.src}">
      <include name="**/*.html" />
      <include name="**/*.css" />
    <filterset begintoken="xx-" endtoken="-zz">
      <filter token="replace-with-version-id" value="${}" />

The build script goes on to deploy the application either to its online test or production versions and then uses the same version identifier to tag newly deployed source code in my source control.

I know some developers prefer using a dynamically generated version identifier (using os.environ['CURRENT_VERSION_ID']) within their HTML or other URI carrying source. Since I already use a build script for JS compilation and Python linting, my approach of replacing a known token makes more sense to me - It removes complexity from my code and allows a full correspondence with the version identifier between the deployed version and the source control records.

I'd be interested in hearing if someone has an idea for improving this process or thinks another approach has distinct advantages I have not considered. If you have questions, leave them here as well.


  1. That it was shocking at that moment a chrome steel activity sit back and watch that marketed with the exact same charge in the form of look at via several other labels. That parallel amongst the van and watch can be comical, plus the numbers are generally almost exactly the same every. The fact is that, your amazing interpretation of any observe that had been blisteringly swiftly, not to mention mega gorgeous, achieved the country collapse. The actual designer watches for sure would not! It absolutely was hence attractive and additionally chic. He did this a wristwatch destined to get the iconic reflection for the make replica omega. Whereas absolutely everyone idea a watch seemed to be beautiful, exclusively a small number of families truly appreciated the benefits in the nation style, additionally, the extraordinary company at the rear of the software. Fast in advance to 2015. typically the pieces have fashion inside your prior to when and ceramic certainly is the hippest materials close to The industry is usually full of wrist watches constructed from naff, aluminum or inflated material. All the running watches for a long time offers tried out a variety of items together with prefer a great many others lives in ceramic for a few purposes despite the fact that entirely ceramic pieces continue unheard of. Considering that the rewards of the very very first follow, they need put together in the unique structure making a lot of designs and additionally closes which will spreads out most of the access throughout the market place when staying genuine to their primary vision- an extravagance outdoor activity watch for an incredibly scary consumer. The principle look at was additional in order to include a tourbillion, some chronograph, your perpetual work schedule, an important awesome side effect, and even more!. That may find a watch wholly blanketed with stones any time which may be ones own matter. Gemstone to your preference or perhaps not necessarily, a enjoy is invariably ready look at an item fresh, bold plus daring. Speaking of extremes, I prefer the follow that located 48mm huge, yet the fact is that Ariel definitely assessed them replica watches uk, therefore i california not likely. Moreover, I can merely suppose that bracelets supplied a rest through the convention with what was you can get, relating to old anklet bracelets that can be purchased. In addition to, when you have obtained that gold tweaked suitably, it ought to be comfortable at any kind of wrist.

  2. This comment has been removed by the author.

  3. Thanks for sharing.
    Click here for more information.

  4. Quel est notre produit? - La meilleure marque de luxe au monde regarde des kopie horloges de la plus haute qualité! Si vous voulez avoir ces Kopie Horloges IWC de luxe, vous voulez porter ces montres sur votre poignet, mais arrêtez-vous en raison de leur prix élevé. Alors s'il vous plaît arrêtez vos pas, c'est votre endroit le plus correct..

  5. nice and realy good work now it is amazingamiduos crack

  6. replika rolex klockor, som kombinerar elegant stil och spetsteknik, en rad stilar av replika rolex submariner klockor, går pekaren mellan din exklusiva smakstil.

  7. Hi! Im so happy that you shared this stuff. I was really having a hard time understanding static resources.
    Chris | click here

  8. I think this is one of the most significant information for me. And I’m glad reading your article. But should remark on some general things, The web site style is perfect, the articles is really great: D. Good job, cheers. tree trimmers hypoluxo

  9. In this post I think I have achieved the thing I wanna do and I might want to share this to you too vacant home staging las vegas

  10. Very nice bro, thanks for sharing this with us. Keep up the good work and Thank you for sharing information
    pc repair west palm beach

  11. You have a good point here!I totally agree with what you have said!! Thanks for sharing your views. hope more people will read this article!!! interior painting west palm beach

  12. I think this is one of the most significant information for me. And i’m glad reading your article.air conditioning companies palm beach gardens

  13. I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it..
    air conditioning companies in palm beach county

  14. Hi, This is a nice article you shared great information i have read it thanks for giving such a wonderful blog for the reader. pest control companies royal palm beach florida

  15. I think this is one of the most significant information for me. And i’m glad reading your article. commercial ac unit palm beach county

  16. Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject?
    hvac repair royal palm beach

  17. This comment has been removed by the author.

  18. A superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place. porta potty rental west palm beach

  19. This comment has been removed by the author.

  20. Thanks for the wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. hillsborough county tree removal services

  21. This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest clearing tampa

  22. I'm so grateful to have this information. Keep it up! |

  23. CyberGhost VPN 7.2.4294 Crack + Activation Code 2020 Most recent

    CyberGhost Cracked is undoubtedly an fundamental tool to go to blocked online websites with liberty. Today, about the internet marketing business the right VPN product may be the current variation of CyberGhost VPN 7.0.5 Premium. It’ll only preserve your IP address lined from hackers, it’ll spot an alternative resolution IP relating to the internet, so you scan the web cautiously.

  24. Download one of the best and wonderful recovery software from

  25. 4K Video Downloader Crack to download free 4K or 4K videos from YouTube and other 4K video sites. Build a collection of the best 4K video downloaders with our PC app and watch them on your 4K TV with no buffering or ads. With over 40 million downloads, Videoder is one of YouTube’s most popular downloaders for Android. Simply great. Furthermore, 4K Video Downloader has everything you could want and there is no problem. Just download a video for free. Well, it started out as a YouTube uploader but now supports downloading from 50+ websites. With many functions and modern design, the app will conquer your heart. The important thing is that Videoder is not just a 4K video download key and YouTube converter, but much more.

  26. is the latest professional multimedia slideshow program. It enables us to create photo and video slide shows with amazing styles. Also; compared to previous versions, it contains many new functions