Skip to content

gassan/django-static-compress

 
 

Repository files navigation

Django-static-compress

License Travis Status PyPi

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
  • static_compress.CompressedCachedStaticFilesStorage: Like CachedStaticFilesStorage, 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)

Contributing

To contribute to this project:

  1. Fork and clone this repository
  2. Run python setup.py develop
  3. Run pip install -r requirements-dev.txt && pre-commit install
  4. Start hacking
  5. Run test: python setup.py test
  6. Run integration test: cd integration_test; python manage.py test
  7. Commit. Pre-commit will warn if you have any changes.
  8. Submit pull request on GitHub
  9. Make sure Travis tests pass.

License

Licensed under the MIT License

About

Precompress your Django static files

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 58.3%
  • CSS 32.6%
  • JavaScript 9.1%