Click here to Skip to main content
Licence CPOL
First Posted 28 Aug 2008
Views 82,923
Bookmarked 168 times

HTTP Handler to Combine Multiple Files, Cache and Deliver Compressed Output for Faster Page Load

By Omar Al Zabir | 28 Aug 2008
An HTTP handler that combines multiple CSS, JavaScript or URL into one response for faster page load. It can combine, compress and cache response which results in faster page load and better scalability of web application
Prize winner in Competition "Best ASP.NET article of August 2008"
13 votes, 23.6%
1

2

3
3 votes, 5.5%
4
39 votes, 70.9%
5
4.96/5 - 55 votes
13 removed
μ 4.17, σa 2.97 [?]

Introduction

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, when a browser requests those JavaScript and CSS files, it makes one HTTP request per file. Every HTTP request results in a network roundtrip from 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 the 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. The browser cannot show the page properly until CSS and JavaScripts are fully loaded. So, the more latency you have, the slower the page loads.

How Bad is Latency

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

image_16.png

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> tags, 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_18.png

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="http://www.msmvps.com/Content/JScript/jquery.js">
</script>
<script type="text/javascript" src="http://www.msmvps.com/Content/JScript/jDate.js">
</script>
<script type="text/javascript"
    src="http://www.msmvps.com/Content/JScript/jQuery.Core.js">
</script>
<script type="text/javascript"
    src="http://www.msmvps.com/Content/JScript/jQuery.Delegate.js">
</script>
<script type="text/javascript"
    src="http://www.msmvps.com/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 them as one response. It delivers the response as gzip compressed to save bandwidth. Moreover, it generates a proper cache header to cache the response in the browser cache, so that, the browser does not request it again on future visits.

In the query string, you specify the file set name in the "s" parameter, then the content type in the "t" parameter and a version number in "v" parameter. As the response is cached, if you make changes to any of the files in the set, you will have to increase the "v" parameter value to make browsers download the response again.

Using this HttpHandler, you can deliver CSS as well:

<link type="text/css" rel="stylesheet"
 href="HttpCombiner.ashx?s=CommonCss&t=text/css&v=1" ></link>

Here's how you define the sets in web.config:

<appSettings>
    <add key="jQueryScripts" 
        value="~/Content/JScript/jquery.js,
            ~/Content/JScript/jDate.js,
            ~/Content/JScript/jQuery.Core.js,
            ~/Content/JScript/jQuery.Delegate.js,
            ~/Content/JScript/jQuery.Validation.js"    
    />
    <add key="CommonCss"
        value="~/App_Themes/Default/Theme.css,
            ~/Css/Common.css,
            ~/Controls/Grid/grid.css"
    />
</appSettings>

Example Website that Uses HttpCombiner

I have made a simple test website to show you the use of HttpCombiner. The test website has two CSS and two JS files. The default.aspx requests both of them using only one <link> and <script> tag via the HttpCombiner.ashx.

image_4.png

Here's the content of the Default.aspx:

image_12.png

Here you see, there's one <link> tag sending a request to HttpCombiner.ashx to deliver the set named Set_Css and a <script> tag asking for a set Set_Javascript.

Files that belong to these two sets are defined in the web.config file:

image_30.png

Here's how the handler works:

  • First it reads the file set name passed in the "s" parameter
  • Then it gets the files defined in the web.config for the set
  • It reads individual files and stores in a buffer
  • The buffer is then gzip compressed
  • The compressed buffer is sent to the browser
  • The compressed buffer is stored in ASP.NET cache so that subsequent requests for the same set can be directly served from cache without reading the individual files from file system or external URL

Benefits of the handler:

  • It saves network roundtrip. The more files you can put in one set, the more you save in network latency. This improves performance.
  • It caches the total combined response as compressed and thus saves reading file from file system and compressing it again and again. This improves scalability.

How the HttpHandler Works

First the handler reads the set, type and version to use from the query string:

image_20.png

If the set has already been cached, the it's written directly from cache. Otherwise the files in the set are loaded one by one and stored in a MemoryStream. The MemoryStream is compressed using GZipStream if browser supports compressed output.

image_22.png

After combining all the files and compressing it, the combined bytes are cached so that subsequent requests can be directly served from cache.

image_24.png

The GetFileBytes function reads a file or URL and returns the bytes. So, you can use Virtual Path to files within your website or you can use URL to external Javascript/CSS files hosted on another domain.

image_26.png

The WriteBytes function has much wisdom in it. It generates a proper header based on whether the bytes are in compressed form or not. Then it generates proper browser cache header to make browser cache the response.

image_28.png

How to use this handler::

  • Include the HttpCombiner.ashx in your project
  • Define the file sets in the <appSettings> section of your web.config
  • Change <link> and <script> tags throughout your website to point to HttpCombiner.ashx in this format:
    HttpCombiner.ashx?s=<setName>&t=<contentType>&v=<versionNo>

Conclusion

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

You should also read my earlier articles about deferring and combining script loading for faster perceived speed and how to make best use of browser cache.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Omar Al Zabir

Architect
BT, UK (ex British Telecom)
United Kingdom United Kingdom

Member
I am: Chief Architect, SaaS Platform, BT (ex British Telecom). Visual C# MVP '05-'07, ASP.NET/IIS MVP '08-'12
I was: Co-founder & CTO, Pageflakes(www.pageflakes.com)
My Book: Building a Web 2.0 portal using ASP.NET 3.5. Also on Amazon
My Blog: http://omaralzabir.com
My Specialization: Web 2.0 Rich AJAX Applications, Level 4 SaaS, Performance and Scalability of Web Apps.
My Email: OmarALZabir at gmail dot com
My Interest: Travel, Performance and Scalability Challenges.
Follow Me: twitter.com/omaralzabir
 
My Projects:
Open Source Web 2.0 AJAX Portal
PlantUML Editor - Super fast UML editor
Smart UML - Freehand UML Designer
RSS Aggregator both Outlook and Standalone
Store Front in JSP but ASP.NET style
 
My Articles:
Top 10 caching mistakes
99.99% Available Production Architecture
Build GoogleIG like Ajax Start Page in 7 days
10 ASP.NET Performance and Scalability Secrets
ASP.NET AJAX under the hood secrets
UFrame: UpdatePanel and IFRAME combined
Fast ASP.NET web page loading
Fast Streaming AJAX Proxy
Using COM safely inside "using" block without requiring interop assembly
Implementing Word Like Automation Model
Distributed Command Pattern

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionIts really nice idea .... but PinmemberMuhammad Shoaib Raja22:14 29 Jun '11  
AnswerRe: Its really nice idea .... but PinmvpOmar Al Zabir8:31 4 Jul '11  
Generalfyi ... PinmemberMichael Stilson5:47 7 Apr '11  
GeneralMy vote of 5 PinmemberJennOpp12:52 8 Feb '11  
QuestionHow can i achieve this in sharepoint site? PinmemberJim Mathew6:31 27 Sep '10  
GeneralThere is Error in returns by the this handler [modified] PinmemberRavenet4:11 31 Jul '10  
GeneralParser Error Pinmemberrashadkk21:34 12 Jul '10  
GeneralA better way to serve dynamically created css/js Pinmembertaliesins4:34 19 Apr '10  
GeneralRe: A better way to serve dynamically created css/js PinmvpOmar Al Zabir8:28 19 Apr '10  
GeneralRe: A better way to serve dynamically created css/js Pinmembervardars22:44 12 Aug '10  
GeneralNot working with Url Rewriting PinmemberRavenet1:09 25 Dec '09  
GeneralRe: Not working with Url Rewriting PinmvpOmar Al Zabir1:13 25 Dec '09  
GeneralRe: Not working with Url Rewriting PinmemberRavenet1:28 25 Dec '09  
GeneralRe: Not working with Url Rewriting PinmemberRavenet1:43 25 Dec '09  
GeneralMinify and obfuscate with YUI Compressor PinmemberKellros2:59 5 Oct '09  
QuestionCannot update file after caching PinmemberMember 5311605:17 15 Sep '09  
AnswerRe: Cannot update file after caching PinmvpOmar Al Zabir6:42 15 Sep '09  
GeneralRe: Cannot update file after caching PinmemberMember 5311607:17 15 Sep '09  
GeneralMedium Trust PinmemberZach Floyd7:21 4 Sep '09  
QuestionWhat is the difference between Scripts.ashx & HttpCombiner.ashx? PinmemberMember 19851651:00 6 Jun '09  
Hi,
 
I'm planning to combine my js files as well as the script files downloaded by ScriptManager. I would like to know the difference between Scripts.ashx and HttpCombiner.ashx. Both of them are used to combine scripts. Do I need to use both Scripts.ashx for asp.net ajax script files and HttpCombiner for my own custom scripts or I can simply use HttpCombiner to combine both asp.net ajax script and my custom scripts.
 
Thanks
-Sunil
Generalfirefox 3 issues with combining CSS files Pinmemberswyche6:51 6 Feb '09  
GeneralRe: firefox 3 issues with combining CSS files Pinmemberkomemi10:17 4 Jan '10  
GeneralDynamic list of files Pinmemberkitkatrobins23:35 18 Jan '09  
GeneralUnable to download the code Pinmembermaxcom23:22 5 Dec '08  
QuestionHow to implement your code if using friendly adapters and ajax net framework? PinmemberMember 325727513:58 11 Sep '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 28 Aug 2008
Article Copyright 2008 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid