Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Django] Ease disabling gzip support #207

Closed
hartwork opened this issue Nov 16, 2018 · 3 comments
Closed

[Django] Ease disabling gzip support #207

hartwork opened this issue Nov 16, 2018 · 3 comments

Comments

@hartwork
Copy link
Contributor

Hi!

I'm very happy I ran into WhiteNoise, happy user, thanks for sharing it as software libre.
I started using brotli (with brotli_static on;) at some point and noticed that compressing all the files with both gzip and brotli takes a few seconds. So I played with getting rid of gzip compression like this:

# Copyright (c) 2018 Sebastian Pipping
# Copyright (c) 2013 David Evans
# License under the MIT license

from django.conf import settings
from whitenoise.compress import Compressor
from whitenoise.storage import CompressedManifestStaticFilesStorage


class WhiteNoiseWithoutGzipStorage(CompressedManifestStaticFilesStorage):

    def create_compressor(self, extensions):
        return Compressor(extensions=extensions, quiet=True, use_gzip=False)

    def compress_files(self, names):
        extensions = getattr(settings,
                             'WHITENOISE_SKIP_COMPRESS_EXTENSIONS', None)
        compressor = self.create_compressor(extensions)
        for name in names:
            if compressor.should_compress(name):
                path = self.path(name)
                prefix_len = len(path) - len(name)
                for compressed_path in compressor.compress(path):
                    compressed_name = compressed_path[prefix_len:]
                    yield name, compressed_name

(and then STATICFILES_STORAGE = 'foo.bar.storage.WhiteNoiseWithoutGzipStorage' in settings to activate, for completeness)

As you can see, to make that work, I currently need to duplicate 100% of CompressedManifestStaticFilesStorage.compress_files, which is not ideal.

So I wonder about how to best ease that case. One way would be to extract a new public-API method create_compressor similar to the one above, except without use_gzip=False. Would you except a pull request in that direction? Would you want to do it yourself? Other ideas?

Thanks and best, Sebastian

@edmorley
Copy link
Contributor

Hi :-)

In #148 I found that Brotli took 95%+ of the total time to compress, so disabling gzip probably wouldn't help reduce the duration by much. Instead concurrency is likely the best way forwards - I keep meaning to find time to dust off the WIP I have locally.

@hartwork hartwork changed the title Ease disabling gzip support [Django] Ease disabling gzip support Nov 16, 2018
@hartwork
Copy link
Contributor Author

Hi! Good analysis at #148. I think we can have both at the same time: Getting wanted things faster (Brotli) and unwanted things (gzip for me) not be done at all.

hartwork added a commit to hartwork/whitenoise that referenced this issue Nov 19, 2018
evansd added a commit that referenced this issue Nov 19, 2018
…r-config-with-django

Extract method .create_compressor x2 (#207)
@hartwork
Copy link
Contributor Author

hartwork commented Nov 19, 2018

With #208 merged and with WhiteNoise 4.1.2 around the above code can now be simplified to:

# Copyright (c) 2018 Sebastian Pipping
# License under the MIT license

from whitenoise.compress import Compressor
from whitenoise.storage import CompressedManifestStaticFilesStorage


class WhiteNoiseWithoutGzipStorage(CompressedManifestStaticFilesStorage):

    def create_compressor(self, **kwargs):
        kwargs['use_gzip'] = False
        return Compressor(**kwargs)

Closing, "bug" fixed in my eyes. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants