Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I gzip JavaScript and CSS files in Django?

I tried profiling my web application and one of the bottlenecks reported was the lack of gzip compression. I proceeded to install the gzip middleware in Django and got a bit of a boost but a new report shows that it is only gzipping the HTML files i.e. any content processed by Django. Is there a way I could kludge/hack/force/make the middleware gzip my CSS and my JS as well?

Could someone please answer my questions below. I've gotten a little lost with this.

  • I might have gotten it wrong but people do gzip the CSS and the JS, don't they?
  • Does Django not compress JS and CSS for some browser compatibility issues?
  • Is compressing and minifying the same thing?

Thanks.

like image 778
Mridang Agarwalla Avatar asked Nov 29 '22 11:11

Mridang Agarwalla


1 Answers

Your CSS and JS should not be going through Django on your production system. You need to configure Apache (or Nginx, or whatever) to serve these, and when you do so you'll be able to set up gzip compression there, rather than in Django.

And no, compressing and minifying are not the same thing. GZip compression is done dynamically by the server when it serves your request, and the browser transparently unzips the file when it receives it. Minification is the process of removing comments and white-space from the files, and sometimes concatenating multiple files into one (ie one css and one javascript, instead of lots of each). This is done when you deploy your files to the server - by django-compress, as Ashok suggests, or by something external like the YUI compressor, and the browser doesn't attempt to reconstruct the original file - that would be impossible, and unnecessary.

like image 58
Daniel Roseman Avatar answered Dec 05 '22 10:12

Daniel Roseman