|
|||||||||||||||||||||||||
|
|||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIt'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 LatencyHere's a graph that shows how each request latency adds up and introduces significant delay in page loading: 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 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 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 <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 <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 HttpCombinerI have made a simple test website to show you the use of HttpCombiner. The test website has two CSS and two JS files. The Here's the content of the Default.aspx: Here you see, there's one Files that belong to these two sets are defined in the Here's how the handler works:
Benefits of the handler:
How the HttpHandler WorksFirst the handler reads the set, type and version to use from the query string: 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 After combining all the files and compressing it, the combined bytes are cached so that subsequent requests can be directly served from cache. The The How to use this handler::
ConclusionThat'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.
|
||||||||||||||||||||||||