nonplus .me

Aggressively caching CSS media with Sass and Tornado

Tornado comes with a nice static url feature for including static files in your templates. It appends hashes to those urls and turns on really aggressive file caching, since the hash bearing url of the requested static file will change whenever the file itself changes, thus busting any browser caching. Unfortunately there’s no inherent means of dealing with the urls in the css (or scss as the case may be) files (e.g. png icons).

To solve this there are a few (probably more) options that I thought of: regex replacements before deployment, making the css files into a tornado template and processing them, adding a hook to sass, or adding the same static_url method to Sass’s arsenal using custom functions. I opted for the last method and added the following to config.rb in order to extend Sass and add a new static_url function for .scss files.