evansd / whitenoise

Radically simplified static file serving for Python web apps
https://whitenoise.readthedocs.io
MIT License
2.51k stars 148 forks source link

Slow compression with many files - add skip patterns settings #279

Open PetrDlouhy opened 3 years ago

PetrDlouhy commented 3 years ago

I have many static files and the compression is very slow (~5 minutes). There are many files collected, that would be probably never used by website users.

If there was some settings similar to WHITENOISE_SKIP_COMPRESS_EXTENSIONS which would allow to add file patterns to skip compression, that would solve my problem.

LucidDan commented 2 years ago

This is a good idea IMO... There is a should_compress() function on the Compressor class, which can let you have complete control over the compression decision.

Rather than just setting a file pattern, a really flexible way to handle this in Whitenoise could be to have a setting that allows configuring the Compressor class in settings, and then you provide your own (where you override the should_compress() function). That would let you select based on filename pattern but also on other things like a lookup or file size etc.

settings.py:

WHITENOISE_COMPRESSOR_CLASS="myapp.apps.CustomCompressor"

myapp/apps.py:

from whitenoise.compress import Compressor
class CustomCompressor(Compressor):
    def should_compress(self, filename: str) -> bool:
        if filename.startswith("big_assets/"):
            return False
        return True

For WSGI apps, this could also work with an extra command line option to the whitenoise.compress CLI, that instantiates your custom class instead of the normal one:

python -m whitenoise.compress --compressor-class mywsgi.compress.CustomCompress