Python WhiteNoise not gzip compressing in Flask application
I found the solution, after a few tries. It seems that the WhiteNoise
documentation is a little bit outdated and doesn't mention everything.
I changed the line from:
app.wsgi_app = WhiteNoise(app.wsgi_app, root='static/')
to:
app.wsgi_app = WhiteNoise(app.wsgi_app, root=os.path.join(os.path.dirname(__file__), 'static'), prefix='static/')
First of all, the prefix
parameter is required (not mentioned in the documentation) and furthermore the Flask
app did not know how to handle the 'static/'
path therefore an absolute path has to be provided.
You should use the command line utility comes with WhiteNoise to do the compression yourself.
Quote
WhiteNoise comes with a command line utility which will generate compressed versions of your files for you.
$ python -m whitenoise.compress --helpusage: compress.py [-h] [-q] [--no-gzip] [--no-brotli] root [extensions [extensions ...]]Search for all files inside <root> *not* matching <extensions> and producecompressed versions with '.gz' and '.br' suffixes (as long as this results ina smaller file)positional arguments: root Path root from which to search for files extensions File extensions to exclude from compression (default: jpg, jpeg, png, gif, webp, zip, gz, tgz, bz2, tbz, swf, flv, woff, woff2)optional arguments: -h, --help show this help message and exit -q, --quiet Don't produce log output --no-gzip Don't produce gzip '.gz' files --no-brotli Don't produce brotli '.br' files
You can either run this during development and commit your compressed files to your repository, or you can run this as part of your build and deploy processes.