[Django]-Django seamless upgrades with CDN

3👍

http://www.allbuttonspressed.com/projects/django-mediagenerator provides asset versioning. It provides a function and a template tag to give you the versioned filename from the actual filename. It also has some nice extras like js/css minification and concatenation.

1👍

Have a look at html5boilerplate, has ant build scripts which as well as doing a lot of other things rename your static js/CSS references to a random number and changes any references in your template files to these new random numbered js/CSS. I don’t think it does the same for image assets but I suppose the ant files could be changed to perform the same.

Django port of html5boilerplate is here: https://bitbucket.org/samkuehn/django-html5-boilerplate

0👍

Seems to me the last part (change all templates and python code to use the new names) is easy. Store the current style “version number” in your settings. Make sure this version number is passed to your templates using the template context. Then, use that when rendering the templates to select the real URL to static files.

The steps involved in publishing a new set of static files is:

  1. publish the new files to your CDN
  2. edit your settings file
  3. re-load your application settings

You can achieve the same type of effect using a similar scheme by storing the version number in the database rather than in the settings. This avoids the reload and changes are immediate, but will add a DB query at each view render.

How you integrate each of these steps into your development process is up to you. It might be possible to (conditionally) automate publishing to the CDN, editing the settings file and restarting the application. To “edit” the settings file easily, you can store the version number in a separate file and have the settings file read that on start-up.

0👍

I’d append a random string to the end of all the static assets you update often.

Example: style.css will become style.css?ASFDSF34343SDSFDSF

In debug mode, I ensure that the random string changes on each request, so my changes take effect right away. (this prevents browser caching)

In production, the random string is ONLY generated once on start-up and reused thereafter.

You can have it so the random string sticks around after a restart of your web server too.

This has been working very well for me, both during the development as well as in production.

Mind you that I keep my assets on S3 and have not enabled the CloudFront yet.

Here is the actual example:

http://static.outsourcefactor.com/css/main.css?ASDFASFASF434434

Leave a comment