0

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?

frank
  • 477
  • 4
  • 14

2 Answers2

2

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.

Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114
  • @frank I've updated the answer with some clarification of what the docs say and how you can circumvent the response compression restriction with the suggested solution form the docs. – Filip Dupanović May 05 '12 at 12:13
  • Thank you. I followed these instructions: http://stackoverflow.com/questions/7576449/django-how-can-i-gzip-staticfiles-served-in-dev-mode – frank May 05 '12 at 12:43
0

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?

Claude Vedovini
  • 2,421
  • 21
  • 19
  • I need to download large files in a few seconds. – frank May 05 '12 at 12:47
  • may be what you can do then is bypass any webserver altogether and tell the browser to access your static files directly by setting the STATIC_URL to point to your static folder through the filesystem, this is the faster you can get. – Claude Vedovini May 06 '12 at 16:37