#1 By: kermit666, December 12th, 2013 18:45
I am getting reports using Chrome PageSpeed that my application is not using compression for static files (css, js). I enabled it in the Django middleware, but that only works for HTML files. I have everything set up "by the book" with dj-static, good settings.py variable values, default Procfile etc. This issue was raised on StackOverflow multiple times (1, 2), but without a real answer.
Does anyone have some suggestions on how to enable compression on static files?
#2 By: cc548, December 13th, 2013 13:21
At the risk of being an annoying answer - the reason this isn't well documented is simply because Django is not made for serving static assets. Using something like s3 is highly recommended (and there's great community support for it, along with CloudFront). There are all sorts of reasons why serving from Django might make sense in your specific case (which is why I'm fully conscious this is not the best answer), but at least that may shed some light on why the answer has proven elusive.
I don't see anything in the docs that would indicate the django middleware only compresses html content, but I guess it's possible that the staticfiles app does an end-around the middleware entirely. You could probably hack something up by providing your own view that wraps the staticfiles app and use the @gzip_page() decorator.
#3 By: kermit666, December 17th, 2013 06:45
Thanks for replying, @cc548. I also think Django shouldn't be serving static files. That's why I'm asking this on the Heroku forum (and not a Django forum), because I am looking for a way to get Heroku to serve the static files compressed on its own (if that is possible). I guess the settings described in the official tutorial are set so that Django wouldn't serve static files by itself. I also found this post by @kenneth that explains how this
dj-static module actually serves the files properly.
So, I guess what I'm asking is how do I get
dj-static to enable gzip compression for the stuff it's serving?
#4 By: Kenneth Reitz, January 7th, 2014 15:42
Honestly, if you want to optimize things to this level, you should be using a CDN or crafting every line of your static pipeline.
#5 By: kermit666, January 7th, 2014 17:32
I know, but after using grunt to massage all my static files into nice and light concatenated-minified-custom-modernizer little daisies, the PageSpeed red "high priority" warning about my web server not gzipping jquery and bootstrap, thus wasting 200 kB just stings my eyes
Maybe I'll try to dig into that static3 code and see how hard it would be to add something like this if I find the time.
#6 By: hadyndickson, September 18th, 2014 23:49
I had the exact same issue. I was using grunt but found there was a lot of extra config etc etc etc then I found django-compressor which is AWESOME!
But I deploy to Heroku which doesn't gzip anything and relys on the app to do it.
I couldnt find any wsgi wrapper that served pre-compressed gzip files, so I extended dj-static to do it for me. Check out my fork of dj-static which includes the additions. Basically it will check for a corresponding static filename that ends in .gz and if it finds it, it will serve it instead. I use this for my Heroku projects.
To test on your dev server if you run the django built in web server be sure to run it with the --nostatic command line switch. ie python manage.py runserver 0.0.0.0:80 --nostatic