Omar AL Zabir blog on ASP.NET Ajax and .NET 3.5

Working hard to enrich millions of peoples' lives

Sponsors

News

I am
Co-Founder and CTO of Pageflakes, acquired by LiveUniverse - founded by MySpace founder.

My Public Page
www.pageflakes.com/omar

View Omar AL Zabir's profile on LinkedIn

Read my blog on:

Omar AL Zabir

www.oazabir.com



Views:

Articles

Open source projects

HTTP handler to combine multiple files, cache and deliver compressed output for faster page load

It's a good practice to use many small Javascript and CSS files instead of one large Javascript/CSS file for better code maintainability, but bad in terms of website performance. Although you should write your Javascript code in small files and break large CSS files into small chunks but when browser requests those javascript and css files, it makes one Http request per file. Every Http Request results in a network roundtrip form your browser to the server and the delay in reaching the server and coming back to the browser is called latency. So, if you have four javascripts and three css files loaded by a page, you are wasting time in seven network roundtrips. Within USA, latency is average 70ms. So, you waste 7x70 = 490ms, about half a second of delay. Outside USA, average latency is around 200ms. So, that means 1400ms of waiting. Browser cannot show the page properly until Css and Javascripts are fully loaded. So, the more latency you have, the slower page loads.

Here's a graph that shows how each request latency adds up and introduces significant delay in page loading:

image

You can reduce the wait time by using a CDN. Read my previous blog post about using CDN. However, a better solution is to deliver multiple files over one request using an HttpHandler that combines several files and delivers as one output. So, instead of putting many <script> or <link> tag, you just put one <script> and one <link> tag, and point them to the HttpHandler. You tell the handler which files to combine and it delivers those files in one response. This saves browser from making many requests and eliminates the latency.

image

Here you can see how much improvement you get if you can combine multiple javascripts and css into one.

In a typical web page, you will see many javascripts referenced:

<script type="text/javascript" src="/Content/JScript/jquery.js">
</script>
<script type="text/javascript" src="/Content/JScript/jDate.js">
</script>
<script type="text/javascript" src="/Content/JScript/jQuery.Core.js">
</script>
<script type="text/javascript" src="/Content/JScript/jQuery.Delegate.js">
</script>
<script type="text/javascript" src="/Content/JScript/jQuery.Validation.js">
</script>

Instead of these individual <script> tags, you can use only one <script> tag to serve the whole set of scripts using an Http Handler:

<script type="text/javascript" 
src="HttpCombiner.ashx?s=jQueryScripts&t=text/javascript&v=1" >
</script>

The Http Handler reads the file names defined in a configuration and combines all those files and delivers as one response. It delivers the response as gzip compressed to save bandwidth. Moreover, it generates proper cache header to cache the response in browser cache, so that, browser does not request it again on future visits.

You can find details about the HttpHandler from this CodeProject article:

http://www.codeproject.com/KB/aspnet/HttpCombine.aspx

You can also get the latest code from this code site:

http://code.msdn.microsoft.com/HttpCombiner

That's it! Make your website faster to load, get more users and earn more revenue.

Share this post :
kick it on DotNetKicks.com

Comments

Dave said:

OK I'm going to state this for the record. IE and gzip do not play nice. Do a search on IE and Web Page Hangs. You may not realize it but your clients will. Micrsoft has a kb article and a hotfix for IE6. I have not seen them publicly acknowledge the problem in IE7 but its still there.

# August 28, 2008 6:48 PM

Christopher Steen said:

Link Listing - August 28, 2008

# August 29, 2008 12:09 AM

Romi said:

No working for me. The handler is ok, but after load compressed js the script are't executed. css inside js compressed are not loaded and Google map api with key not works.!?(IE7,FF)

# September 4, 2008 4:53 PM

Andrew de Klerk said:

Ok - I have implemented this and it all works fine except for:

www.mydomain.com/api'1234'.  Now obviously the second "?" is causing grief. Is there a way to get around this? I imagine anyone using gooogle maps will have the same issue as they implement a similar key querystring. It just so happens that the js associated with this url is the one I really need to compress as it is large. I appreciate any assistance with this

Andrew

# September 16, 2008 4:53 AM

Santhipala said:

if we change anything in java script it will not working wt can i do

# September 30, 2008 2:32 AM

Santhipala said:

The handler is ok, but after load compressed js if i make any changes in java script it will not updated in the compressed script.

# September 30, 2008 2:36 AM

krishan said:

i have js scripts working under different <div> to run with the styling on the page, still how can i combine them all in a single <script>.

# December 26, 2008 6:47 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)