Update (2015-08-21): Do not use this. You will hate me for saying this, as this page is one of my most popular on the site, but after using my code on this page with Rails for quite a while, I came back on my own decision to use it. I knew compressing pages on every request would add some overhead, but I didn’t think it would be that significant – and it is, a lot.
While this would work with preprocessors like Middleman, which compresses all the files only once, doing so with an app generating pages on every request will slow down the application. You may install performance monitoring software like New Relic then toggle the compression to see the difference.
When optimising Web sites or apps for speed, any little bit helps. One of the easiest things to do is to “minify” all source files, removing all unncessary blank spaces, line breaks, and comments, and then compress them on the fly using gzip.
Also available as a gist on GitHub.
- The code above uses environment variables by default, which I often use to enable or disable features instead of going by the environment name.
- Despite the point above, no matter how hard you try, Rails will only allow Sass to compress the output on production.
As indicated in the code comments, enabling
comments: :copyrightor keep all comments with
There seems to be no way for Sass to strip out copyright notices, or any such comments usually beginning with