Skip to content

Latest commit

 

History

History
91 lines (60 loc) · 4.1 KB

README.md

File metadata and controls

91 lines (60 loc) · 4.1 KB

Django-static-compress

License Travis Status PyPi No Maintenance Intended

Precompress your static files automatically with Brotli and Zopfli

Installation

Install this from pip:

$ pip install django-static-compress

(you may want to write this in your requirements.txt)

Then update your settings.py:

STATICFILES_STORAGE = 'static_compress.CompressedStaticFilesStorage'

When you run python manage.py collectstatic it will have an additional post-processing pass to compress your static files.

Make sure that your web server is configured to serve precompressed static files:

  • If using nginx:
  • Caddy will serve .gz and .br without additional configuration.

Also, as Brotli is not supported by all browsers you should make sure that your reverse proxy/CDN honor the Vary header, and your web server set it to Vary: Accept-Encoding.

Available storages

  • static_compress.CompressedStaticFilesStorage: Generate .br and .gz from your static files
  • static_compress.CompressedManifestStaticFilesStorage: Like ManifestStaticFilesStorage, but also generate compressed files for the hashed files

You can also add support to your own backend by applying static_compress.CompressMixin to your class.

By default it will only compress files ending with .js, .css and .svg. This is controlled by the settings below.

Settings

django-static-compress settings and their default values:

STATIC_COMPRESS_FILE_EXTS = ['js', 'css', 'svg']
STATIC_COMPRESS_METHODS = ['gz', 'br']
STATIC_COMPRESS_KEEP_ORIGINAL = True
STATIC_COMPRESS_MIN_SIZE_KB = 30

After compressing the static files, django-static-compress still leaves the original files in STATIC_ROOT folder. If you want to delete (to save disk space), change STATIC_COMPRESS_KEEP_ORIGINAL to False.

If the file is too small, it isn't worth compressing. You can change the minimum size in KiB at which file should be compressed, by changing STATIC_COMPRESS_MIN_SIZE_KB.

By default, django-static-compress use Zopfli to compress to gzip. Zopfli compress better than gzip, but will take more time to compress. If you want to create gzip file with built-in zlib compressor, replace 'gz' with 'gz+zlib' in STATIC_COMPRESS_METHODS.

File size reduction

Here's some statistics from TipMe's jQuery and React bundle. Both bundle have related plugins built in with webpack (eg. Bootstrap for jQuery bundle, and classnames for React bundle), and is already minified.

101K jquery.9aa33728c6b5.js
 33K jquery.9aa33728c6b5.js.gz (33%)
 31K jquery.9aa33728c6b5.js.br (31%)
174K react.5c4899aeda53.js
 51K react.5c4899aeda53.js.gz (29%)
 44K react.5c4899aeda53.js.br (25%)

(.gz is Zopfli compressed, and .br is Brotli compressed)

Developing

This project is unmaintained. You may use it, but issues and pull requests might be ignored.

  1. Run python setup.py develop
  2. Run pip install -r requirements-dev.txt && pre-commit install
  3. Start hacking
  4. Run test: python setup.py test
  5. Run integration test: cd integration_test; python manage.py test
  6. Commit. Pre-commit will warn if you have any changes.

License

Licensed under the MIT License