简体   繁体   English

为什么没有JS和CSS的gzip压缩html文件显示?

[英]Why does gzipped html file display without JS and CSS?

I have an html file saved in gzip format. 我有一个以gzip格式保存的html文件。 The browser displays the html file but without the javascript and CSS. 浏览器显示html文件,但没有javascript和CSS。 Non-zipped html files in the same directory do display correctly. 同一目录中的非压缩html文件可以正确显示。 In addition, I saved the source from the compressed html file and it reopened correctly, with JS and CSS applied. 此外,我从压缩的html文件中保存了源代码,并在应用了JS和CSS的情况下正确地重新打开了源代码。

What is different about displaying the zipped html that would not allow it to pick up the JS and CSS? 显示压缩的html不允许其拾取JS和CSS有什么不同?

The basic problem is you can't just serve a gzip file where the browser expects CSS. 基本问题是您不能仅在浏览器需要CSS的地方提供gzip文件。 By itself, that does not work any more than if you return a JPEG or a ham sandwich. 就其本身而言,它仅比返回JPEG或火腿三明治有效。

When content gets zipped on the fly, the response is somewhat different -- the response says "I am text/css, but, happen to be encoded with gzip for transfer". 当内容快速压缩时,响应会有所不同-响应显示“我是text / css,但是碰巧用gzip编码以进行传输”。 The browser can figure that one out. 浏览器可以找出答案。

Some web servers like Apache will do that sort of thing for you if you supply gzipped files locally, too. 如果您也本地提供gzip压缩文件,则某些Web服务器(如Apache)将为您执行此类操作。 But I imagine your server isn't. 但我想您的服务器不是。

Why does it work for HTML? 为什么它适用于HTML? Hmm, I don't know, maybe your browser actually manages to figure it out in that particular case? 嗯,我不知道,也许您的浏览器确实能够在这种情况下找到答案?

What you ultimately want to do is serve the response with Content-Type: text/css and Content-Encoding: gzip to have it recognized correctly. 您最终要做的是使用Content-Type:text / css和Content-Encoding:gzip提供响应,以使其正确识别。

it you're working on Localhost on your own server (like XAMPP) 如果您正在自己的服务器上使用Localhost(例如XAMPP)
then you need to configure the .htaccess file to send the right 那么您需要配置.htaccess文件以发送正确的权限
headers that said that files might be gziped. 标头表示文件可能已压缩。

try adding this to you main .htaccess file : 尝试将其添加到主.htaccess文件中:

AddEncoding x-gzip .gz
AddType text/html .gz

and make sure your gziped compressed 并确保您的gziped压缩
files are endind with the .gz extention. 文件以.gz扩展名结尾。

also, always run this in a server.. :) 此外,请始终在服务器上运行它。

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

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