web-applicationsziphttpresponse

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="/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?


Solution

  • 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:

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