Compress WebResource.axd in ASP.NET

Jun 12, 2008

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

Comments (14) -

Josh Stodola
Josh Stodola United States
6/12/2008 8:19:54 PM #

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

Mads Kristensen
Mads Kristensen Denmark
6/12/2008 8:29:38 PM #

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.

Carlito
Carlito United States
6/12/2008 9:24:37 PM #

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!

Josh Stodola
Josh Stodola United States
6/13/2008 2:46:00 AM #

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.

Miron
Miron Israel
6/13/2008 11:51:38 AM #

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?

Mads Kristensen
Mads Kristensen United States
6/13/2008 12:04:14 PM #

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

Matt
Matt United States
6/13/2008 12:10:52 PM #

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

Chris Pietschmann
Chris Pietschmann United States
6/13/2008 5:31:09 PM #

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"]);

Domokos Tar
Domokos Tar Hungary
6/23/2008 1:53:44 PM #

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?


Mads Kristensen
Mads Kristensen United States
6/23/2008 2:05:36 PM #

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

henry
henry United States
6/26/2008 8:52:17 AM #

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

Blogging Developer
Blogging Developer Turkey
7/3/2008 10:03:30 AM #

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

Ravi Thapliyal
Ravi Thapliyal India
7/8/2008 5:08:28 AM #

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.

gowrishankar
gowrishankar India
10/27/2008 5:44:05 AM #

hi mads

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

Pingbacks and trackbacks (3)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.