Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python WhiteNoise not gzip compressing in Flask application

I used WhiteNoise in my Flask/Python3 application with gunicorn being the web server as follows:

from whitenoise import WhiteNoise

app = Flask(__name__, static_folder='static')
app.wsgi_app = WhiteNoise(app.wsgi_app, root='static/')
app.wsgi_app.add_files(app.static_folder)

I even tried to create static_folder in Flask object and adding it later on to additional WhiteNoise files but that did not work either. It compiles fine, but when I do:

curl -i -H "Accept-Encoding: gzip" https://my-homepage/static/css/my.css

I receive the following response:

HTTP/1.1 200 OK
Connection: keep-alive
Server: gunicorn/19.8.1
Date: Wed, 23 May 2018 09:53:38 GMT
Content-Length: 50162
Content-Type: text/css; charset=utf-8
Last-Modified: Wed, 23 May 2018 09:51:21 GMT
Cache-Control: public, max-age=43200
Expires: Wed, 23 May 2018 21:53:38 GMT
Etag: "1527069081.0-50162-130551313"
Accept-Ranges: bytes
Strict-Transport-Security: max-age=31536000
Via: 1.1 vegur

You can see the Content-Encoding: gzip not being present. What am I missing?

like image 475
12dollar Avatar asked Mar 08 '26 12:03

12dollar


2 Answers

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 prefixparameter 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.

like image 74
12dollar Avatar answered Mar 11 '26 02:03

12dollar


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 --help
usage: compress.py [-h] [-q] [--no-gzip] [--no-brotli]
                   root [extensions [extensions ...]]

Search for all files inside <root> *not* matching <extensions> and produce
compressed versions with '.gz' and '.br' suffixes (as long as this results in
a 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.

like image 36
laike9m Avatar answered Mar 11 '26 01:03

laike9m