如何返回 gzipped css/js
我正在使用 .NET,简单的问题是:
如何或如何让 Web 浏览器知道它
<script type="text/javascript" src="/Scripts/myjsfile.js"/>
已被 gzip 压缩?
我可以将 .gz
附加到源吗?
那么我如何告诉网络浏览器它是压缩内容,或者它已经知道了?
I'm using .NET and the simple question is:
How or can I let the web browser know that
<script type="text/javascript" src="/Scripts/myjsfile.js"/>
is gzipped?
can I just append .gz
to the source?
This article, which is pretty cool, shows how to to compress my dynamic html by plugging into the Response.Filter stream object to return gzipped content. But my css/js isn't dynamic so I don't see the point in wasting cpu cycles to zip for every single request.
So how do I tell the web browser that it's zipped content, or does it already know?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标记带有标题的响应:
更新:我刚刚意识到我回答了您的字面问题 - 客户端如何知道 - 但不是您需要在服务器上执行什么操作才能使其发送此类信息。不幸的是,我不使用 IIS,所以我无法给你一个明智的答案。
如果没有人回答你,你可以自己做动态的事情,而且速度也慢不了多少。 Apache 基本上所做的事情是这样的:
filename + ".gz"
文件Content-Encoding: gzip
和适当的对其进行标记Content-Type
filename
,如果存在则发送404
。您还可以告诉它即时 gzip 文件;这在空间上更经济,因为您不在磁盘上保留文件的两个版本,但速度较慢(因为您需要根据每个请求进行压缩)。
Tag response with header:
UPDATE: I just realised I answered your literal question - how the client will know - but not what you need to do on the server to make it send such. Unfortunately, I don't work with IIS, so I can't give you a smart answer.
If no-one answers you, you can do dynamic thing yourself, and it's not much slower. The thing that basically Apache is doing is this:
Accept-Encoding
header includesgzip
.filename + ".gz"
fileContent-Encoding: gzip
and appropriateContent-Type
filename
, send that if it exists404
.You can also tell it to gzip files on the fly; that's more economical in space, as you don't keep two versions of the file on the disk, but slower (because you need to compress at each request).
将“FilesMatch”更改为您想要压缩的任何内容。服务器会自动将未压缩的文件压缩为 gzip,将其发送到浏览器,浏览器会将文件解压回原始形式。
Change the "FilesMatch" to whatever you want gzipped. The server will automatically compress your uncompressed files to a gzip, send it to the browser and the browser will unzip the file back to it's original form.