Uploading static assets (CSS/JS) to S3 for CloudFront CDN
For a new Backbone.js + Flask project I’m using grunt + grunt-contrib, RequireJS’s r.js, Flask-Assets / webassets for static file (LESS/CSS, JS) compilation. But I needed a good way to get my nicely optimized static files onto a CDN and serving proper HTTP headers.
Using the excellent s3cmd tool, here’s what I came up with.
This example will break for browsers/proxies that don’t support gzip, but this is fine for my needs. Any other solution would either require a custom origin web server or writing different filenames in HTML depending on the request coming in. But since I want to use S3 as my origin this is the easiest/simplest solution.
Since all assets are “built” with a md5 version number hash in the file name, I want far futures headers to cache permanently.