简体   繁体   English

在Django上的js / css文件上使用Gzip

[英]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. 我知道最好使用像AWS这样的静态文件,但我正在开发阶段,我更喜欢在localhost上使用javascript / css文件。

It would be great if I could get gzip working on my javascript files for testing. 如果我能让gzip处理我的javascript文件进行测试,那就太好了。 I am using the default gzip middleware but it's just compressing the view request. 我正在使用默认的gzip中间件,但它只是压缩视图请求。

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. 对于基于django的服务器,应该存在类似于Nginx的文件类型列表。 How can I add application/javascript, text/javascript, etc for gzip compression? 如何为gzip压缩添加应用程序/ javascript,text / javascript等?

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/". 您应该阅读GZipMiddleware文档,其中解释了当“Content-Type标头包含javascript或以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. 为了阐明文档所说的Content-Type ,如果Content-Type标头值包含javascript或者不以text/开头,则不会压缩响应。 That means both text/javascript and application/javascript will be invalid responses, since they match javascript . 这意味着text/javascriptapplication/javascript都是无效的响应,因为它们匹配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. 这些限制是由中间件本身故意强加的,但您仍然可以通过使用gzip_page()装饰器包装静态文件视图处理程序并手动将其添加到URL配置来规避这些限制。

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 在开发过程中,您使用的是Django内置的Web服务器,这个服务器非常简单,除了您可以看到的任何其他选项./manage.py help runserver

You options are either to setup a real webserver or use the staticfiles app with a custom StaticFilesStorage 您可以选择设置真实的Web服务器,也可以将staticfiles应用程序与自定义的StaticFilesStorage

But honestly, this is overkill, why would want to test gzip compression? 但老实说,这是矫枉过正,为什么要测试gzip压缩?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM