frank
frank

Reputation: 477

Gzip on js/css files on Django

I know that it's better to use something like AWS for static files but I am on developing stage and I prefer to have javascript/css files on localhost.

It would be great if I could get gzip working on my javascript files for testing. I am using the default gzip middleware but it's just compressing the view request.

My template looks like:

<script src='file.js' type='application/javascript'></script>

There should be a type-of-file list similar to Nginx for the django-based-server. How can I add application/javascript, text/javascript, etc for gzip compression?

Upvotes: 0

Views: 1806

Answers (2)

Filip Dupanović
Filip Dupanović

Reputation: 33690

You should read the GZipMiddleware documentation, where it's explained that the middleware will not compress responses when the "Content-Type header contains javascript or starts with anything other than text/".

EDIT:

To clarify what the documentation says, if the Content-Type header value contains javascript or doesn't begin with text/, then the response won't be compressed. That means both text/javascript and application/javascript will be invalid responses, since they match javascript.

Those restrictions are intentionally imposed by the middleware itself, but you can still circumvent that by wrapping the static files view handler with the gzip_page() decorator and adding it to your URL configuration manually.

Upvotes: 2

Claude Vedovini
Claude Vedovini

Reputation: 2481

During development you are using the Django built-in webserver, this server is really simple and does not have any other options than what you can see with ./manage.py help runserver

You options are either to setup a real webserver or use the staticfiles app with a custom StaticFilesStorage

But honestly, this is overkill, why would want to test gzip compression?

Upvotes: 0

Related Questions