Update: There's a new version of this module that also handles ScriptResource.axd here

A lot have been written about the compression of WebResource.axd over the years, but as far as I know, nobody has come up with a simple solution to it. Miron Abramson does have a very good library for compressing a lot of different file types, but I’ve never been much in favour of third-party libraries.

There are several reasons why you want to compress the WebResource.axd. First of all you want to reduce the size of your website. On this blog ASP.NET inserts two WebResource.axd script with a combined file size of 43.3KB. If I use compression and minify the scripts, the total file size will be 8.1KB. That is a significant gain.

The WebResource.axd is cached in the browser until midnight the same day, so the file size might not be that big a deal. Well, if we can make it expire on a much later date it would be much better since it never changes. So, if the WebResource.axd is minified, compressed and set to live longer in the browsers it really adds up.

Implementation

Download the CompresWebResource.cs file below and put it in your App_Code folder. Then add the following lines to your web.config:

<httpModules>
  <add type="CompressWebResource" name="CompressWebResource" />
</httpModules>
<httpHandlers>
  <add verb="*" path="*js.axd" type="JavaScriptHandler" />
</httpHandlers>

You can use the HttpHandler for all your other .js files as well. Just change the path from /script.js to js.axd?path=/script.js.

Download

CompressWebResource.zip (2,81 kb)

Comments


Comments are closed