如何链接到 html 文档中的 gzipped javascript?
我见过很多关于压缩 JavaScript 以节省下载时间的参考。 但是我也看到一些警告,指出某些浏览器不支持此功能。
我有两种不同的方法可供使用:
- 使用
mod_deflate
让 Apache 通过 htaccess 压缩给定目录中的 JS/CSS 文件 - 使用
ob_start('gzhandler')
压缩文件并将其返回给浏览器并带有正确的标头。
方法 1 的问题是并非所有浏览器都支持 mod_deflate,而且我不知道如何编写 .htaccess
文件以足够智能地对此进行调整。
方法 2 的问题在于,对于如何判断浏览器是否支持压缩脚本或样式表,以及如果支持,必须在标头中指定什么 mime-type 作为内容类型,没有明确的答案。
我需要一些建议。 首先,哪种方法更能被浏览器普遍接受? 其次,如何使用任一方法进行衰减以提供未压缩的备份脚本? 第三, 本身可以工作吗? (它显然不会衰减。)
根据记录,我使用的是带有 mod_deflate 和完整 gzip 创建功能的 PHP5,并且我的文档类型是严格的 xhtml。 另外,javascript 本身是用 YUI 压缩的。 编辑:我刚刚回去看了看,但我只有 Apache 1.3; 我以为我有 2 个,很抱歉在我可能没有的情况下提到 mod_deflate。
I've seen a number of references to gzipping a javascript to save download time. But I also see a number of warnings that certain browsers do not support this.
I have two different methods at my disposal:
- use
mod_deflate
to make Apache compress JS/CSS files in a given directory through htaccess - use
ob_start('gzhandler')
to compress a file and return it to the browser with the correct headers.
The problems with method 1 are that not all browsers support mod_deflate, and I have no clue how to write the .htaccess
file to be smart enough to adjust for this.
The problem with method 2 is that there is no definitive answer about how to tell if a browser supports a gzipped script, or stylesheet, and that if it does what mime-type must be given as the content type in the header.
I need some advice. First, which method is more universally accepted by browsers? Second, how do I decay using either method to provide the uncompressed backup script? Third, would <script src="js/lib.js.gz" type="text/javascript"></script>
work by itself? (It obviously wouldn't decay.)
For the record, I'm using PHP5 with mod_deflate and full gzip creation capabilities, and my doctype is xhtml strict. Also, the javascript itself is compressed with YUI. Edit: I just went back and looked, but I have only Apache 1.3; I thought I had 2, so sorry for mentioning mod_deflate when I probably don't have it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
mod_deflate 和 php 的 gzhandler 都基于 zlib,因此从这个意义上说,浏览器如何压缩内容几乎没有什么区别。
为了回应你的第一个问题,你可以像这样设置模块特定的 .htaccess 信息:
为了回应你的第二个问题,你可以检测 PHP 中的浏览器支持:
这里有一些未经测试的 .htaccess 应该能够处理压缩与未压缩的协商.js 文件:(源)
mod_deflate and php's gzhandler both are based on zlib, so in that sense there is little difference to a browser how the content is being compressed.
in response to your first concern, you can set module specific .htaccess info like this:
in response to your second concern, you can detect for browser support in PHP:
here's some untested .htaccess that should be able to handle negotiation of compressed vs uncompressed .js files: (source)
如果浏览器正确设置了其
Accept-Encoding
标头,您应该能够判断浏览器是否支持 gzip:If the browser is properly setting it's
Accept-Encoding
header, you should be able to tell if the browser can support gzip:实现此目标的最佳方法是让您的服务器支持内联 gzip。 您可以从脚本标记的 src 属性中删除“.gz”,并将文件以未压缩的方式存储在服务器上。 如果客户端支持,服务器会自动将脚本作为 gzip 编码结果发送。 这确实会花费您的服务器一些额外的 CPU,但是对于支持它的客户端,文件会被压缩,而较旧的客户端仍然可以获得扩展版本。
查看 apache2 的 mod_deflate。 mod_gzip 相当于 Apache 1.x。
The best way to achieve this is for your server to support inline gzip. You would take the ".gz" off both the script tag's src attribute and store the file on the server uncompressed. If the client supported it, the server would automatically send the script as a gzip encoded result. This does cost your server some extra CPU, but the file gets compressed for those clients that support it, while older clients still get the expanded version.
Check out mod_deflate for apache2. mod_gzip is the equivalent for Apache 1.x.