如何 Gzip 一个 javascript 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4352378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:02:58  来源:igfitidea点击:

How to Gzip a javascript file?

javascriptwebservergzip

提问by siva636

179KB of jQuery is Minified and Gzipped to 26KB. I am trying to do the same compressions to my javascript files and found the Yuicompressor to Minify it. But I am still searching for a way to Gzip it.

179KB 的 jQuery 被压缩并压缩为 26KB。我正在尝试对我的 javascript 文件进行相同的压缩,并找到了 Yuicompressor 来缩小它。但我仍在寻找一种 Gzip 的方法。

How can I Gzip my Javascript files?

如何 Gzip 我的 Javascript 文件?

回答by Ryan Li

If you are using it in a webpage, gzip is a configuration method in your web server. The file is gzipped by the server, sent to the browser. No manual action is needed. For Apache: http://httpd.apache.org/docs/2.0/mod/mod_deflate.html.

如果您在网页中使用它,gzip 是您的 Web 服务器中的一种配置方法。该文件由服务器 gzip,发送到浏览器。无需手动操作。对于 Apache:http: //httpd.apache.org/docs/2.0/mod/mod_deflate.html

If you are delivering the code to developers, you could use the gzipcommand.

如果您将代码交付给开发人员,则可以使用该gzip命令。

回答by Yann Ramin

You would use gzip, the GNU compression utility. Luckily, the gzip algorithm and file structure is implemented by numerous other tools, such as 7zip (for Windows). You can configure your server (via mod_deflateor others) to compress files on the fly, but for static content its a waste of CPU power.

您将使用gzipGNU 压缩实用程序。幸运的是,gzip 算法和文件结构是由许多其他工具实现的,例如 7zip(适用于 Windows)。您可以配置您的服务器(通过mod_deflate或其他方式)以动态压缩文件,但对于静态内容,它会浪费 CPU 能力。

Here is an article which shows how to transparently serve pre-compressed gzip to browsers which support it: http://blog.alien109.com/2009/03/17/gzip-your-javascript/

这是一篇文章,展示了如何将预压缩的 gzip 透明地提供给支持它的浏览器:http: //blog.alien109.com/2009/03/17/gzip-your-javascript/

回答by Tamik Soziev

You could also use zbugs.com- just provide your website url and it will do everything for you.

您也可以使用zbugs.com- 只需提供您的网站网址,它就会为您完成所有工作。