javascript GZIP 是浏览器自动解压的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32172704/
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
Is GZIP Automatically Decompressed by Browser?
提问by user5075511
I have enabled gzip compression in IIS 8.0 by following the url Enabling Gzip in IIS on Windows 8I am calling external rest services from my application via jquery ajax call and C# code, currently my external web service is not gzip compressed. If i ask my service partner to gzip their response, do i need to write any decompression logic in my code on jquery side and c# side or browser automatically decompress the response for me ?
我通过在 Windows 8 上的 IIS 中启用 Gzip的 URL启用了 IIS 8.0 中的 gzip 压缩 我正在通过 jquery ajax 调用和 C# 代码从我的应用程序调用外部休息服务,目前我的外部 Web 服务不是 gzip 压缩的。如果我要求我的服务合作伙伴 gzip 他们的响应,我是否需要在 jquery 端和 c# 端或浏览器自动为我解压响应的代码中编写任何解压缩逻辑?
回答by Marc Baumbach
All modern browsers can handle a gzip encoded response. In fact, if you look at their requests, they'll have a header that says something along the lines of Accept-Encoding: gzip
which is their way of saying to the server that they can handle gzipped responses.
所有现代浏览器都可以处理 gzip 编码的响应。事实上,如果您查看他们的请求,他们将有一个标头,Accept-Encoding: gzip
其中说明了他们对服务器说他们可以处理 gzip 响应的方式。
The important part is that your server can return both gzip and uncompressed responses depending on the existence and value of that header. If a client doesn't send the Accept-Encoding
header, you shouldn't compress it. If the client does send it, you can optionally encode the response using gzip. Not all content needs to be compressed as it may already be compressed and you're wasting CPU cycles. JPEG images are usually a good example of this. Most likely, IIS is making an intelligent decision here as well and only compressing what is necessary, when necessary.
重要的部分是您的服务器可以返回 gzip 和未压缩的响应,具体取决于该标头的存在和值。如果客户端不发送Accept-Encoding
标头,则不应对其进行压缩。如果客户端确实发送了它,您可以选择使用 gzip 对响应进行编码。并非所有内容都需要压缩,因为它可能已经被压缩并且您正在浪费 CPU 周期。JPEG 图像通常就是一个很好的例子。最有可能的是,IIS 也在此处做出明智的决定,并且仅在必要时压缩必要的内容。
You can verify IIS is doing what it should be by looking at the response headers coming back from your server and looking for the Content-Encoding: gzip
header. That tells the client, or browser, that the content is encoded using gzip compression and it should decompress it appropriately.
您可以通过查看从您的服务器返回的响应标头并查找Content-Encoding: gzip
标头来验证 IIS 是否在做它应该做的事情。这告诉客户端或浏览器内容是使用 gzip 压缩编码的,它应该适当地解压缩它。
All browser based requests (e.g., XHR/AJAX/jQuery, regular requests) will automatically be decompressed without additional effort from you. The browser is the client responsible for determining if it can handle gzip and will add the Accept-Encoding
header if it does. Your JavaScript code will receive the uncompressed version of it in your response handler.
所有基于浏览器的请求(例如,XHR/AJAX/jQuery,常规请求)将自动解压缩,无需您额外的努力。浏览器是负责确定它是否可以处理 gzip 的客户端,如果可以,将添加Accept-Encoding
标头。您的 JavaScript 代码将在您的响应处理程序中接收它的未压缩版本。
TL;DR: Turning it on is usually a good idea and you shouldn't need to do additional work.
TL;DR:打开它通常是个好主意,您不需要做额外的工作。
回答by eribeiro
If the gzip compression is enabled on the web server, that is, not in the application logic, then the browser will uncompress automatically.
如果在web 服务器上启用了 gzip 压缩,即不在应用程序逻辑中,那么浏览器将自动解压缩。
In fact, if the browser doesn't support compression the web server will send the data uncompressed (this info is in the request/response http headers exchanged between browser and web server). Just be aware that compression is ineffective with JPEG and other already compressed formats.
事实上,如果浏览器不支持压缩,Web 服务器将发送未压缩的数据(此信息位于浏览器和 Web 服务器之间交换的请求/响应 http 标头中)。请注意,压缩对 JPEG 和其他已经压缩的格式无效。