简体   繁体   English

JavaScript可以检测用户的浏览器是否支持gzip吗?

[英]Can JavaScript detect if the user's browser supports gzip?

Can I use JavaScript to detect if the user's browser supports gzipped content (client side, not node.js or similar)? 我可以使用JavaScript来检测用户的浏览器是否支持gzip压缩内容(客户端,而不是node.js或类似内容)?

I am trying to support the following edge case: 我试图支持以下边缘情况:
There are a lot of possible files that can load on a particular web app and it would be better to load them on demand as necessary as the application runs rather than load them all initially. 有很多可能的文件可以加载到特定的Web应用程序上,最好在应用程序运行时根据需要加载它们,而不是最初加载它们。 I want to serve these files off of S3 with a far-future cache expiration date. 我希望使用远期缓存过期日期从S3提供这些文件。 Since S3 does not support gzipping files to clients that support it , I want to host two versions of each file -- one normal, and one gzipped with content-type set to application/gzip . 由于S3不支持向支持它的客户端gzipping文件 ,我想要托管每个文件的两个版本 - 一个正常,一个gzip, content-type设置为application/gzip The browser of course needs to know which files to request. 浏览器当然需要知道要请求的文件。 If JavaScript is able to detect if the browser supports gzipped content, then the browser will be able to request the correct files. 如果JavaScript能够检测到浏览器是否支持gzip压缩内容,那么浏览器将能够请求正确的文件。

Is this possible? 这可能吗?

Javascript can't, but you can use Javascript to detect wether or not the browser supports gzipped content. Javascript不能,但您可以使用Javascript来检测浏览器是否支持gzip压缩内容。

I commented above and would just like to reiterrate, you should use CloudFront anyway, which does gzip content. 我在上面评论过并且想要重新启动,你应该使用CloudFront,它可以使用gzip内容。 If you are using S3, then there is zero reason why you would not want to use CloudFront, however, for the purposes of answering your question... 如果您使用的是S3,那么为什么您不想使用CloudFront,以回答您的问题...

This blog post perfectly addresses how you would detect if the browser supports Gzip. 这篇博文完美地说明了如何检测浏览器是否支持Gzip。

http://blog.kenweiner.com/2009/08/serving-gzipped-javascript-files-from.html http://blog.kenweiner.com/2009/08/serving-gzipped-javascript-files-from.html

Here is a quick summary: 这是一个快速摘要:

1) Create a small gzipped file, gzipcheck.js.jgz, and make it available in CloudFront. 1)创建一个小的gzip压缩文件gzipcheck.js.jgz,并使其在CloudFront中可用。 This file should contain one line of code: 该文件应包含一行代码:

 gzipEnabled = true; 

2) Use the following code to attempt to load and run this file. 2)使用以下代码尝试加载和运行此文件。 You'll probably want to put it in the HTML HEAD section before any other Javascript code. 您可能希望在任何其他Javascript代码之前将其放在HTML HEAD部分中。

 <script type="text/javascript" src="gzipcheck.js.jgz"> </script> 

If the file loads, it sets a flag, gzipEnabled, that indicates whether or not the browser supports gzip. 如果文件加载,它会设置一个标志gzipEnabled,指示浏览器是否支持gzip。

Well cloudfront does not gzip content automatically. 好吧,cloudfront不会自动gzip内容。 Till the time Amazon decides to do automatic gzip compression in S3 and Cloudfront one has to use the below workaround. 直到亚马逊决定在S3和Cloudfront中进行自动gzip压缩时,必须使用以下解决方法。

  • In addition to the normal version, create a gzipped version of the file and upload on S3. 除了普通版本之外,还要创建该文件的gzip压缩版本并在S3上传。 If the file name is style.css the gzipped version should be named style.css.gz. 如果文件名是style.css,则gzip压缩版本应命名为style.css.gz。
  • Add a header to the file with key=Content-Encoding & value=gzip to the file. 使用key = Content-Encoding&value = gzip将文件添加到文件中。 This is needed so that browsers understand that the content is encoded using gzip. 这是必需的,以便浏览器了解内容是使用gzip编码的。 The header can be added using S3 api or the popular S3 file manager tools like Cloudberry, Bucket Explorer, etc 可以使用S3 api或流行的S3文件管理器工具(如Cloudberry,Bucket Explorer等)添加标头
  • Also add the correct Content-Type header for the file. 还要为文件添加正确的Content-Type标头。 eg for style.css it should be Content-Type: text/css. 例如,对于style.css,它应该是Content-Type:text / css。
  • In the webpage include the file normally 在网页中正常包含文件
  • Use the above mentioned javascript to detect if the browser supports gzip encoding. 使用上面提到的javascript来检测浏览器是否支持gzip编码。 If found true replace the file name eg style.css with style.css.gz 如果找到true,则用style.css.gz替换文件名,例如style.css

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

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