4

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

Teddy Ward
  • 450
  • 3
  • 16
  • It looks like you are seeing this behaviour in development, not production. Have you seen http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development? – Alasdair Nov 30 '17 at 17:39
  • Hey Alasdair, sorry for neglecting to say, yes! I saw that and added 'whitenoise.runserver_nostatic', to my INSTALLED_APPS, as suggested, as the --nostatic flag itself doesn't work for me: `manage.py runserver: error: unrecognized arguments: --nostatic` – Teddy Ward Nov 30 '17 at 18:14

2 Answers2

3

In my case this was caused by my Django setting
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
because the collectstatic process failed due to a non-existing font file referenced in a .css file.

This error stopped the whole process and thus no compressed files were produced.

Solution
Switch to STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

Note: the problem with non-existing files causing errors really lies with Django's ManifestStaticFilesStorage, not with WhiteNoise.

veuncent
  • 1,599
  • 1
  • 20
  • 17
  • You can add manually the file from http://www.maennergesundheitsbericht.de/fileadmin/styles/mgb/css/ui-lightness/images/ and automatically will solve the error – bmalbusca Mar 06 '21 at 00:39
1

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.

D. Evans
  • 3,242
  • 22
  • 23
  • Hey, thanks for the response! It's like talking to a celebrity! I still don't get gzip encoding in my response after running `python manage.py collectstatic` and then `python manage.py runserver localhost:8080`, even though I have DEBUG = False, and the whitenoise.runserver_nostatic app installed. I only get gzip encoding if I manually run the `gzip` command before running the server. I tested deploying to Heroku, and the static files are also not compressed there, unless I do it manually. – Teddy Ward Nov 30 '17 at 22:14
  • Oh, I think you pointed me in the right direction, though! I found compressed versions of the files in a different directory after running `collectstatic`. I must have staticfiles configured incorrectly. I can debug from here. Thank you!! – Teddy Ward Nov 30 '17 at 22:21