开发者

How can I return gzipped css/js

I'm using .NET and the simple question is:

How or can I let the web browser know that

<script type="text/javascript" src="/Scri开发者_JS百科pts/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?


Tag response with header:

Content-Encoding: gzip

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:

  • It tests whether the request's Accept-Encoding header includes gzip.
  • If it does, then checks if there is filename + ".gz" file
  • If it does, send that, tagging it with Content-Encoding: gzip and appropriate Content-Type
  • If not, check if there is filename, send that if it exists
  • If that fails, 404.

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).


#####################################################
# CONFIGURE media caching
#
Header unset ETag
FileETag None
<FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>
#
#####################################################

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜