|
I like this approach to static assets, where each file's name is changed to include the hash of the file's contents on deployment. But I've never implemented it myself, because I can't figure out a good way to refer to those static assets in my templates. simonw, if you're listening, how do you solve this problem? How do you go from, e.g., <link rel="stylesheet" href="/style.css"> to <link rel="stylesheet" href="/style.{current-hash}.css"> ? |
In development, the above tag would output "/static/css/example.css?0.234234" - the random number at the end cache busts so e.g. IE will always load the latest version of the file.
In production, the tag looks up the transformed filename in a dictionary, which looks something like this:
The deployment script includes a bit of code that goes through every file in the static directory, figures out the hash, renames it and then writes out that dictionary in a generated static_assets.py file ready to be deployed to the servers. There's a separate management script that pushes the renamed files to S3 - I run that before doing a deploy.The only really fiddly bit is that the script needs to rewrite all of the CSS files to include the updated filename of any referenced images. I'm using a dumb regex to do this:
Since we control the coding standards for our own CSS, there's no need to do anything more robust than that.