Can I gzip JavaScript and CSS files in Django?

I tried profiling my web application, and one of the bottlenecks was the lack of gzip compression. I started installing the gzip middleware in Django and got some momentum, but a new report shows that it is only gzipping HTML files, that is, any content processed by Django. Is there a way I could kludge / hack / force / make gzip middleware my CSS and my JS?

Can anyone answer my questions below. I lost it a bit.

  • Maybe I was wrong, but people do gzip CSS and JS, right?
  • Does Django not compress JS and CSS for some browsers compatibility issues?
  • Compresses and minimize the same thing?

Thanks.

+4
source share
4 answers

Thanks to everyone.

It seems that GzipMiddleware in Django DOES compresses CSS and JS.

I used the Google Page Speed ​​plugin for Firebug to profile my page, and it looks like it was generating reports based on old copies (without gzipped versions) of CSS and JS in my local cache. These copies were from the time I turned on the Gzip middleware. I dropped the cache, and it seems that the reports showed different results.

+1
source

Your CSS and JS should not go through Django on your production system. You need to configure Apache (or Nginx or something else) to serve them, and when you do, you can configure gzip compression there, not Django.

And no, compression and minification are not the same thing. GZip compression is performed dynamically by the server when it serves your request, and the browser transparently decompresses the file when it receives it. Minimization is the process of removing comments and spaces from files, and sometimes combining several files into one (i.e. One css and one javascript, not a lot). This is done when you deploy your files on the server β€” using django-compress, as Ashok suggests, or using something external, such as a YUI compressor , and the browser does not try to restore the original file β€” this would be impossible and not necessary.

+14
source

You should consider placing a django application behind a reverse HTTP proxy.

You can configure apache as a reverse proxy for your django application, although some people prefer to use nginx or lighttpd for this scenario.

The HTTP reverse proxy is basically a proxy created just before your web application. Browsers make requests from the reverse proxy, and the reverse proxy redirects requests to the web application. The reverse proxy can also perform a number of interesting things, such as handle ssl, a gzip descriptor for compressing all responses and processing static files.

+4
source

Follow Daniel Rosman's suggestion: "Your CSS and JS should not go through Django on your production system"

If you want to serve through Django, then you can compress css, js files using django-compressor , django-compress

+2
source

Source: https://habr.com/ru/post/889096/


All Articles