File compression in Divio Django applications

Default behaviour in our projects

By default, we apply gzip compression to:

  • Django-served content, such as HTML files
  • static files, when DEBUG is False

This compression can be disabled by setting the DISABLE_GZIP environment variable to True.

For Django-served content, we apply Django’s GZipMiddleware.

For static files, we use our own STATICFILES_STORAGE classes to gzip static files after they are collected. See GZippedStaticFilesMixin.post_process(), in Aldryn Django’s storage.py.

Using DISABLE_GZIP

When gzip is disabled, gzipped versions of static files are not used (though they are still collected). A default Django storage class is used instead.

Using DEBUG = True

When DEBUG = True, the non-gzipped versions of the files are loaded by templates, whether or not DISABLE_GZIP has been applied.

Using Django Compressor

Django Compressor is a popular tool for additional compression functionality (for example, consolidation of multiple files into one).

When using Django Compressor, note that:

  • offline compression must be used
  • static files must be compressed before collection (python manage.py compress must come before collectstatic in the Dockerfile)

To verify correct and expected operation of Django Compressor, use the local server in live configuration, taking note to disable volume mapping that would affect the collected compressed files.