Search code examples
djangoherokugzipstatic-fileswhitenoise

Should Whitenoise auto-compress static files? How?


I recently enabled Whitenoise for my Django project that will run on Heroku. I want Whitenoise to compress my static files automatically, as would seem to be possible from this part of the docs: http://whitenoise.evans.io/en/stable/django.html#add-compression-and-caching-support

However, after adding the following to my settings:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

I find that my files are not compressed!

curl -H "Accept-Encoding: gzip" -I http://localhost:8080/static/app/js/auth.min.js

HTTP/1.0 200 OK
Date: Thu, 30 Nov 2017 17:14:27 GMT
Server: WSGIServer/0.2 CPython/3.5.2
Last-Modified: Thu, 30 Nov 2017 01:45:33 GMT
Content-Length: 103648
Content-Type: application/javascript; charset="utf-8"
Cache-Control: max-age=0, public
Access-Control-Allow-Origin: *

However, if I manually gzip one of my files, everything works just peachy

$ gzip ../app/static/app/js/auth.min.js
$ curl -H "Accept-Encoding: gzip" -I http://localhost:8080/static/app/js/auth.min.js
HTTP/1.0 200 OK
Date: Thu, 30 Nov 2017 17:21:47 GMT
Server: WSGIServer/0.2 CPython/3.5.2
Last-Modified: Thu, 30 Nov 2017 17:14:53 GMT
Content-Type: application/javascript; charset="utf-8"
Cache-Control: max-age=0, public
Access-Control-Allow-Origin: *
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 21870

Do I just have to add some script to my build process to gzip everything, or does Whitenoise include this? If it does, does anyone have any idea what I might be missing or doing wrong? I would really like the ability (as advertised in the docs above) to keep everything cached forever


Solution

  • The compression is done automatically when the collectstatic management command is run. That command gets run by Heroku as part of the build process so you shouldn't need to do anything else to get compression support.

    If you want to test it locally you'll need to run collectstatic yourself and then run your app with DEBUG = False to get the same behaviour as you would in production.