Tomcat中的Gzip如何工作 [英] How Gzip in Tomcat works

查看:95
本文介绍了Tomcat中的Gzip如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理有关脚本压缩的问题。以下是我在stackoverflow中刚刚发现的内容:

I am doing issues about script compression. Following is what I just found in stackoverflow:

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

"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命令。

If you are delivering the code to developers, you could use the gzip command."

有人告诉我,如果使用这种脚本压缩策略,它将使您非常容易调试:

Someone told me that if such script compression strategy is used, it will make you very easy to debug:

使用Firebug在运行时调试javascript时,您看到的脚本是未经压缩的原始脚本,可读性强。

When you use Firebug to debug javascript in run time, the script you see is the original, uncompressed one, which is much readable.

但是如果使用在YUI压缩器中,Firebug中显示的脚本将如下所示:

But if you use the YUI compressor, the script shown in Firebug will be something like this:

var is_moz=(typeof document.implementation.createDocument!="undefined");var is_chrome=navigator.userAgent.toLowerCase().indexOf("chrome")>-1;var selectedTreeNodeIdInOper="";var selectedTreeNodePkInOper="";var winDef="width=490,height=190,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyh....

其中

我的问题是,tomcat中的gzip如何将压缩脚本发送给客户端,同时向开发人员显示原始脚本?

My question is that how can gzip in tomcat send the compressed scripts to the clients meanwhile show the developer the original script? Magic?

推荐答案

这里有两个独立的问题:压缩和压缩。

There's two separate issues going on here: compression and minification.

压缩是服务器压缩内容(html,css,js)以发送给客户端(浏览器)的过程。然后,浏览器将内容解压缩为压缩之前的内容。在您查看源代码或查看浏览器中的开发人员工具时,您已经看到了原始脚本。可以将其视为将zip文件发送给某人。

Compression is the process by which the server compresses content (html,css,js) to send to the client (Browser). The browser then de-compresses the content back to exactly what it was before it got compressed. By the time you get to view-source or look at the developer tools in your browser you're seeing the original script. Think of it like sending a zip file to someone. The original file is still there exactly as it was, just wrapped up in a zip.

可以在应用程序体系结构的多个位置启用压缩功能。您可以在Web服务器(链接到Apache的httpd文档),应用服务器(Tomcat支持压缩)或自己的代码(例如搜索 servlet压缩过滤器)中启用它。

Compression can be enabled in several places in your app's architecture. You can enable it in your Webserver (you linked to Apache's httpd docs), your app server (Tomcat supports compression) or in your own code (search for 'servlet compression filter' for examples)

Minification(这是YUI Compressor和其他工具的作用)会永久更改脚本,通常会创建文件的-min.js版本。该文件将缺少换行符,并且可能已重命名了变量。由于此文件是服务器发送的文件,因此您将在浏览器中看到该文件,是的,它很难调试。浏览器制造商已经意识到这一点,并且Chrome,Firefox和IE11 +支持 sourcemaps 可以告诉您浏览器如何将代码的最小化版本映射回原始文件。 YUI Compressor不支持源地图,但是其他工具(例如 uglify 也可以。

Minification (which is what YUI Compressor and other tools do) permanently changes a script, usually creating a -min.js version of the file. This file will be missing newlines and may have variables re-named. Because this altered file is what the server is sending, that's what you'll see in the browser and yes, it's hard to debug. Browser makers have recognized this and Chrome, Firefox and IE11+ support sourcemaps which tell the browser how to map from a minimized version of the code back to the original file. YUI Compressor doesn't support sourcemaps, but other tools like uglify do.

您可以同时使用压缩和压缩,这样做有很多好处。有关更多详细信息,请参见此讨论

You can use minification and compression together and there are benefits to doing so. See this discussion for more detail.

这篇关于Tomcat中的Gzip如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆