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: yourapp.blogspot.com\images\banner_v1.jpg (requested)

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

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

Fourth Request
URI: yourapp.appspot.com\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:

yourapp.appspot.com/images/v1/banner.jpg » /static/img/banner.jpg
yourapp.appspot.com/images/v2/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="${path.build.src}">
    <fileset dir="${path.assets.src}">
      <include name="**/*.html" />
      <include name="**/*.css" />
    </fileset>
 
    <filterset begintoken="xx-" endtoken="-zz">
      <filter token="replace-with-version-id" value="${generated.id.timestamp}" />
    </filterset>
</copy>


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.

60 comments:

  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 http://www.attrinity.com. 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.

    ReplyDelete
  2. This comment has been removed by the author.

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

    ReplyDelete
  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..

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

    ReplyDelete
  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.

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  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

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  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

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

    ReplyDelete
  22. I'm so grateful to have this information. Keep it up! | g.page/cincinnati-roofing?share

    ReplyDelete
  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.

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

    ReplyDelete
  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.

    https://crackcook.com/4k-video-downloader-crack/

    ReplyDelete
  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

    ReplyDelete
  27. Нey fantastic blog! Does running a bloɡ such as
    this take a lot of oof work? I’ve very little understanding of programming but I
    was hoping to start my оwn blog in the near future. Аnyhow, if you һave any recommendations or techniques for new blog owners ρlesе share.
    Ι know tһis is off topic but I јust wanted to ask.
    ccleaner pro crack
    recover my files crack
    duplicate files fixer crack
    system mechanic pro crack activation key

    ReplyDelete
  28. iMyFone TunesMate 2.9.5.1 Crack: This is a new design for your iPhone Manager and iTunes.
    This software handles all the limitations and limits of iTunes. iTunes collects information about errors and unexpected crashes.
    Download it free From Here:
    https://softserialskey.com/imyfone-tunesmate-iphone-transfer-crack/

    ReplyDelete
  29. We would like to know more about this, please send us a message here: https://www.louisvilleexcavators.com/contact.html

    ReplyDelete
  30. Avast Password Crack is software that falls into the category of security software that protects your entire computer from unexpected dangers and malicious attacks.crackforavastpassword

    ReplyDelete
  31. Adobe Illustrator CC Crack Free Full Version Adobe Illustrator CC Full Pre-Cracked Version is available for download. adobeillustratorcckekygen

    ReplyDelete
  32. Ableton Live Crack Full Version for Windows and Mac is advanced and highly featured software with the latest MIDI sequencers and other DJ mixing tools for advanced music track production.abletonlivetorrent

    ReplyDelete
  33. PlayerUnknown’s Battleground PUBG is a player-versus-player battleground shooter video game. One hundred players can fight on a battlefield.playerunknownsbattlegrounds

    ReplyDelete
  34. Windscribe VPN Key Crack is a desktop browser and expansion that is an application. This software will ensure your privacy, non-blocked sites with geo-restrictions, and cleanliness and paths.freedownloadwindscribe

    ReplyDelete
  35. MiniTool Partition Wizard 12.1 Crack It is a product name MiniTool Partition Wizard Serial Key that permits you to compartmentalize and deal with your Computer’s Hard plate memory.minitoolpartitionwizard

    ReplyDelete
  36. The Latest Key PhoneRescue Crack is a very powerful and outstanding application that allows the user to easily restore all lost or deleted data.downloadphonerescuekey

    ReplyDelete
  37. R-Studio 8.14 Crack is a data recovery software that comes with Windows operating systems. Provides powerful data recovery, erasure protection, disk duplication, data security, and PC privacy.rstudiokey

    ReplyDelete
  38. Wavepad Crack is audio editing software is a complete professional audio and music editor for Windows and Mac.wavepadsoundeditorwithkeygen

    ReplyDelete
  39. Did the iPhone shut down after system update failure or water damage? Does the iPhone have a white Apple logo or a blue screen? Does the iPhone keep restarting? No matter the situation your iPhone is facing, FoneLab iOS System Recovery allows you to quickly and conveniently restore your iPhone / iPad / iPod’s iOS system.crackforaiseesoftfonelab

    ReplyDelete
  40. You can get the following with d16 Group Silverline Collection Crack Download.d16groupsilverlinecollectionfreedownload

    ReplyDelete
  41. Capture One Pro Free Download is an advanced digital photo editing software that provides an all-in-one solution for capturing, editing, adjusting, and organizing photos.captureone20pro

    ReplyDelete
  42. 4K YouTube to MP3 Crack Download stores audio tracks, playlists, podcasts, audiobooks from YouTube, VEVO, SoundCloud, Facebook, Instagram, and more. Original quality, various audio formats, fast download.download4kyoutubetomp3

    ReplyDelete
  43. GlissEQ Crack Free Download is an AAX parameter equalizer, AudioUnit, and VST plug-in for professional audio and music production applications.voxengoglisseqcrack

    ReplyDelete
  44. WavePad Sound Editor 11.08 Crack is a component pressed incredible sound editor. It is created by NCH Software for Windows and Mac.wavepadsoundeditoractivationkey

    ReplyDelete
  45. Cinch Audio Recorder 4.0.2 Crack: Need to record MP3 from streaming music sites like Spotify? You can depend on the famous Spotify music recorder, Cinch Audio Recorder.crackforcinchaudiorecorder

    ReplyDelete
  46. This is a morale booster for the people and the workers. This helps in achieving a perfect place for working.
    retail cleaning service Saskatoon

    ReplyDelete
  47. I know this site provides good content and more information,
    Are there any websites that offer these features and quality?
    driver talent pro keygen

    ReplyDelete
  48. Hey what a great post I came across and believe me
    I was looking for similar posts last week and hardly ever
    found it. Thank you very much and we will be waiting for more messages from you.
    adobe prelude cc crack
    movavi video suite
    phonerescue crack
    acdsee photo studio ultimate crack

    ReplyDelete
  49. Hope everything is okay. Missing your posts. This is one of my favorite websites
    sound forge 13 crack
    microsoft office-crack
    malwarebytes crack

    ReplyDelete
  50. I really love your blog.. Great colors & theme.
    Did you develop this website yourself? Please reply
    back as I’m hoping to create my own site and would love to know
    where you got this from or just what the theme is named.
    Thank you!
    audials one crack
    wondershare filmora crack
    avg ultimate
    folder lock crack
    radmin vpn crack
    dvdfab player crack

    ReplyDelete
  51. Stellar Converter Crack is a one-stop answer for all of your audio and video conversion needs. The audio-video ratio converter converts films. audio documents into many codecs and also offers utilities for video editing. creating GIF’s inserting metadata and much more for audio-video patch overlay overlays for videos is well known among users of YouTube, Instant Chat, and Instagram.
    https://protocrack.com/stellar-converter-crack/
    Adobe Premiere Pro Crack is the innovative video editing software for film, television, and the Internet. With creative tools, integration with other apps and services, and the power of Adobe Sensei, you can turn your video into high-quality movies and videos. With Premiere Rush, you can create and edit new projects from any device. The automated tools supported by Adobe Sensei save time so you can focus on your story.
    https://secrack.com/adobe-premiere-pro-crack/
    Miracle Box Crack is a practical problem-solving tool for Android. It is easy to work on Windows, MAC, and Android devices for support. Miracle Box Pro is a great software for designing jobs on an intuitive platform. It’s good for cellphones. At the same time, you can get fantastic ash performance for the phones. You can also unlock the process. It is a continuous tool that repairs only several devices.
    https://thiscrack.com/miracle-box-full-crack/

    ReplyDelete
  52. Hope everything is okay. Missing your posts. This is one of my favorite websites

    best blogs on this
    Fortnite Download For Pc Free
    Corel DRAW X6 Crack
    cyberlink powerdvd crack
    Adobe Premiere Pro Crack is the innovative video editing software for film, television, and the Internet. With creative tools, integration with other apps and services, and the power of Adobe Sensei, you can turn your video into high-quality movies and videos. With Premiere Rush, you can create and edit new projects from any device. The automated tools supported by Adobe Sensei save time so you can focus on your story.

    ReplyDelete