Compress WebResource.axd in ASP.NET

by Mads Kristensen 13. June 2008 04:30

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)

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags: , , , ,

ASP.NET

Comments

6/13/2008 5:19:54 AM #

Josh Stodola

For one with a mentality of disregarding 3rd party libraries, I would think you would forget about WebResource.axd all together and write your own Javascript.  Then it wouldn't be bloated with needless crap like WebResource.axd is.  That's the way I see it.

By the way, I think you already posted something like this...
blog.madskristensen.dk/.../...pages-in-ASPNET.aspx

Oh, and I hate the dates on your blog because looking at that post, you can't tell what year it was written.  That sucks.

Best Regards...

Josh Stodola United States |

6/13/2008 5:29:38 AM #

Mads Kristensen

Hey Josh.
I don't disregard 3rd party libraries, but I do think they take away a lot of the fun. I like simple and small plugable classes I can manipulate easily as I please.

The page you are refering to didn't handle the compression in a simple matter. It was way to complicated - much more than was needed.

Regarding the dates. I know they suck. It started as an experiment but it failed horribly. I just always forget to remove them.

Mads Kristensen Denmark |

6/13/2008 6:24:37 AM #

Carlito

Hey Mads, you must be reading my mind, I was just using YSlow last night and trying to figure out how to do this - so thanks.  

You'd think that MS would've at least put some small effort into minimizing the size of the axd code - but nope!

Carlito United States |

6/13/2008 11:46:00 AM #

Josh Stodola

Hey, I'm actually with you.  I don't like to use 3rd party libraries either, and I agree that they take out some of the fun.  You lose that sense of total control as well.  But I take it a step further; I don't rely on WebResource.axd for the simple fact that it is bloated.  I handroll my Javascript functions.

Josh Stodola United States |

6/13/2008 3:12:55 PM #

pingback

Pingback from blog.cwa.me.uk

Reflective Perspective - Chris Alcock  » The Morning Brew #114

blog.cwa.me.uk |

6/13/2008 8:51:38 PM #

Miron

Hey, you guys don't like 3rd party stuff, but sometimes it just doing the job much better, and if it is open source (http://www.codeplex.com/MbCompression) you have the control over it.
Why you guys are using Blogengine.NET and not building your own blog engine? what is the different? it is 3rd stuff too.
We are using it because it is doing the job good, it is saving us time, and the best part - it is open source. The same you can say about every other 3rt party stuff. If it's doing the job better than what we have, it save us time, and it is open source, why not using it?

Miron Israel |

6/13/2008 9:04:14 PM #

Mads Kristensen

@Miron,

I use third-party stuff all the time and loving it - especially if it's open source like your library. When I code for fun then I don't like to use them, because they take away some of the fun of discovering and building things - reinvent the wheel so to speak. That's fun to me.

Mads Kristensen United States |

6/13/2008 9:10:52 PM #

Matt

Thanks a lot Mads, this module will really help me out for nearly every website I'm working on.

Matt United States |

6/14/2008 2:31:09 AM #

Chris Pietschmann

Thanks, nice and simple.

I did find that it doesn't work when the website isn't running in the root path. I modified the first line if the JavaScriptHandler.ProcessRequest method to the following to fix this issue:

string path = context.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~" + context.Request.QueryString["path"]);

Chris Pietschmann United States |

6/23/2008 10:53:44 PM #

Domokos Tar

In case of IE 6 is the compression disabled?

Line 134,135:
if (context.Request.UserAgent != null && context.Request.UserAgent.Contains("MSIE 6"))
return;

Why is it so?


Domokos Tar Hungary |

6/23/2008 11:05:36 PM #

Mads Kristensen

@Domokos Tar,

It's just to play it safe. People have reported various places on the web that IE6 can have trouble with compressed javascript files. Maybe it's safe to remove.

Mads Kristensen United States |

6/26/2008 5:52:17 PM #

henry

Is this intended to replace the compression routine you wrote in blog.madskristensen.dk/.../...pages-in-ASPNET.aspx

henry United States |

7/3/2008 7:03:30 PM #

Blogging Developer

Compressing on the way is better but there are online tools that compress javascript files such as http://www.compressjavascript.com

Blogging Developer Turkey |

7/8/2008 2:08:28 PM #

Ravi Thapliyal

Thanks a lot Mads!! Your code is so perfect. excellent work.
Thank you for sharing this script, it helped me a lot with my site performance.

I'm using YSLOW to test my website and i noticed that all the ScriptResource.axd files are not minified and not combined to one .js file.

how can i set that all generated ScriptResource.axd's will also be compressed and minified?

Best Regards,
Ravi.

Ravi Thapliyal India |

10/27/2008 2:44:05 PM #

gowrishankar

hi mads

how to compress the javascript files
iam getting error.my js files are not working

gowrishankar India |

5/12/2009 4:51:57 AM #

pingback

Pingback from alectang.com

Compress WebResource.axd in Client side HTML - Alec Tang

alectang.com |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008