The CombineAndMinify package discussed here automatically speeds up the loading of JavaScript files, CSS files and or images (loaded from image tags or from CSS files). The result can be a dramatic improvement in ASP.NET web site performance.
The CombineAndMinify package discussed here automatically speeds up the loading of JavaScript files, CSS files and or images (loaded from image tags or from CSS files). The result can be a dramatic improvement in ASP.NET web site performance.
Installation is easy - just add a dll, update your web.config and add a small file. If you use IIS 6, you'll also need to update the configuration of IIS (no such update needed for IIS 7). Step by step installation instructions are in the installation section.
Download the package (free) from CodePlex, at http://combineandminify.codeplex.com/.
The features below can all be switched on and off individually via the web.config file (full description). If you just install the package and not do any further configuration, it only minifies and combines JavaScript and CSS files, and than only when the site is in Release mode.
Combines JavaScript files and CSS files. Loading a single large file is often much quicker than loading a series of small files, because that saves the overhead involved in all the request and response messages.
If a CSS file contains image urls that are relative to the folder containing the CSS file itself, those urls are fixed up by the package. That way, they continue to work even if CSS files from several different folders are combined.
This package is just one way of improving the performance of your web site. My recently released book ASP.NET Performance Secrets shows how to pinpoint the biggest performance bottlenecks in your web site, using various tools and performance counters built into Windows, IIS and SQL Server. It then shows how to fix those bottlenecks. It covers all environments used by a web site - the web server, the database server, and the browser. The book is extremely hands on - the aim is to improve web site performance today, without wading through a lot of theory first.
If you are interested in web site performance, you may be interested in this short digression into server compression.
IIS 6 and 7, and Apache as well, provide the option to gzip compress all text files (html, JavaScript, CSS, etc.) sent to the browser. All modern browsers know how to decompress those files. Compression can save you a lot of bandwidth and download time. It is not uncommon to reduce file sizes by way over 50%.
In IIS, compression is switched off by default for dynamic files, such as .aspx files. This is because it increases the load on the CPU. However, with the overabundence of CPU cycles on modern server hardware, switching on compression for dynamic files on your server is almost always a great idea. Also, IIS 6 and 7 allow you to set the compression level, so you can choose a level that you're comfortable with. Finally, IIS 7 can automatically switch off compression when CPU usage goes over a predetermined level (set by you), and switch it back on after CPU usage has dropped below a second level (also set by you). It even lets you cache compressed dynamic files, which makes compression extremely attractive.
Switching on basic compression on IIS 7 is easy, but getting the most out of it is a bit tricky. Switching on compression in IIS 6 is just tricky. Good places to find out more would behere (for IIS 7) and here (for IIS 6).
Or you could read chapter 10 of my book ASP.NET Performance Secrets where it is all spelt out in one place (believe me, this will save you a lot of time).
<configuration>
...
<configSections>
...
<section name="combineAndMinify" type="CombineAndMinify.ConfigSection"/>
...
</configSections>
...
</configuration>
Allow the package to process the head sections of your pages. That way, it can replace the tags loading individual JavaScript and CSS files with tags loading combined files:
Make sure that the head tags of your pages have runat="server", like so:
<head runat="server">
When you create a new page in Visual Studio, it creates a head section with runat="server", so you probably don't have to change anything here.
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlHead"
adapterType="CombineAndMinify.HeadAdapter" />
</controlAdapters>
</browser>
</browsers>
This tells ASP.NET to leave processing of all HtmlHead controls (which represent the head tags) to the class CombineAndMinify.HeadAdapter (which is part of the package).Make sure that your site has trust level Full. This allows it to access the combineAndMinify section. If your web.config contains
<trust level=... />
remove it. You'll than get the default trust level, which is Full.
Only do these steps if you use IIS 7 for your live site or your development environment. If you only use IIS 6 or IIS 7 in classic mode, skip to the IIS 6 section.
The package uses an HTTP Handler to combine and minify JavaScript and CSS files on the fly. To reduce CPU overhead, it caches the combined files, so they only get combined and minified after they have been changed on the file system.
To configure the HTTP Handler, add the following to your web.config:
</configuration>
...
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
...
<handlers>
...
<add name="JavaScriptHandler" verb="*" path="*.js" type="CombineAndMinify.HttpHandler, CombineAndMinify" resourceType="Unspecified"/>
<add name="CssHandler" verb="*" path="*.css" type="CombineAndMinify.HttpHandler, CombineAndMinify" resourceType="Unspecified"/>
</handlers>
...
</system.webServer>
...
</configuration>
Configure the HTTP Handler in your web.config:
<configuration>
...
<system.web>
...
<httpHandlers>
...
<add verb="*" path="*.js" type="CombineAndMinify.HttpHandler, CombineAndMinify" />
<add verb="*" path="*.css" type="CombineAndMinify.HttpHandler, CombineAndMinify" />
</httpHandlers>
...
</system.web>
...
</configuration>
Send all requests for JavaScript and CSS files to the ASP.NET handler. That allows the package to combine and minify them.
Tell IIS 6 to let ASP.NET handle all requests for files with extension .js:
By default, the package is only active when your site is in Release mode - that is, if you have
<compilation debug="false">
in your web.config. That makes sense, because the package doesn't affect the functionality of your site - it just makes it faster. If this arrangement works for you, there is no need to do any installation related to your development environment, so you can skip this section.If you do want to activate the package in your development environment, you need to run your debugging sessions under IIS 7, instead of under Cassini (the web server that normally runs when you hit F5 in Visual Studio). This is because the package uses an HTTP Handler to process JavaScript and CSS files, and Cassini in .Net 3.5 doesn't support HTTP Handlers.
Note that IIS 7 is included in the Home Premium, Business, Enterprise and Ultimate versions of Windows Vista and Win 7 (but not the Starter and Home versions). That means you probably already have it on your system.
Here is how to run your debugging sessions under IIS 7:
Now that you have installed IIS 7 on your machine, create a site in IIS 7 that Visual Studio can use for debugging sessions:
If you use a Web Site:
If you use a Web Application:
Now when you hit F5 in Visual Studio to run your site in debug mode, you'll find it runs under IIS 7 instead of under Cassini. Apart from that, your debugging experience is the same - you can still set breakpoints, etc.
Now that you're using IIS 7 to do development, do the IIS 7 related installation if you have not already done that.
Follow the steps listed here.
By default, the package minifies and combines JavaScript and CSS files. To use the other features, or to switch off minification or combining of JavaScript or CSS files, add acombineAndMinify element to your web.config file, like so:
<configuration>
...
<combineAndMinify ... >
</combineAndMinify>
...
</configuration>
The combineAndMinify element supports these attributes and child elements:
Determines when the package is active. When it is not active, it doesn't affect your site at all and none of the other attributes or child elements listed in this section have any effect.
Value | Description |
---|---|
Never | Package is never active, irrespective of debug mode |
Always | Package is always active, irrespective of debug mode |
ReleaseModeOnly (default) | Package is only active in release mode |
DebugModeOnly | Package is only active in debug mode |
<configuration>
...
<combineAndMinify active="Always" >
</combineAndMinify>
...
</configuration>
Whether your site is in debug or release mode depends on the debug attribute of the compilation element in your web.config file. If that attribute is set to false, your site is in release mode (as it should be when live). When it is set true, it is in debug mode (as it should be in development). It looks like this in web.config:
<configuration>
...
<system.web>
<compilation debug="true">
...
</compilation>
...
</system.web>
...
</configuration>
Note that by default, the package is only active in release mode - so you won't see any effect while you're developing. To ensure that the package is active in both release mode and in debug mode as well, set active to Always, as shown in the example above.
Note that the active attribute acts as a master switch for the whole package. It's like the ignition in a car - if you don't turn on the ignition (or at least the battery), pressing any other buttons on the dashboard won't do anything.
Determines whether the package combines CSS files.
Value | Description |
---|---|
None | CSS files are never combined |
PerGroup (default) | CSS files are combined per group. See explanation below. |
All | All CSS files are combined into a single CSS file. |
Example
<configuration>
...
<combineAndMinify combineCSSFiles="All" >
</combineAndMinify>
...
</configuration>
To see what is meant with "per group", have a look at this example:
<link rel="Stylesheet" type="text/css" href="/css/site1.css" />
<link rel="Stylesheet" type="text/css" href="/css/site2.css" />
<script type="text/javascript" src="js/script1.js"></script>
<link rel="Stylesheet" type="text/css" href="/css/site3.css" />
<link rel="Stylesheet" type="text/css" href="/css/site4.css" />
By default, the package tries to ensure that the order of the JavaScript and CSS definitions doesn't change when JavaScript and CSS files are combined. It does this by grouping CSS files whose tags are after each other. In this case, there are 2 groups - site1.css and site2.css, and site3.css and site4.css. This causes the browser to load the CSS and JavaScrip definitions in the exact same order as when the files had not been combined:
You get this behaviour when you set combineCSSFiles to PerGroup (which is the default). The package supports grouping for JavaScript files as well, which is controlled by thecombineJavaScriptFiles attribute.
Combining all CSS files into one file
If you set combineCSSFiles to All, all CSS files get combined into a single file. The link tag to load that combined CSS file gets placed where the link tag of the first CSS file used to be. In our example, that causes this load order:
As you see, the CSS definitions in site3.css and site4.css now get loaded before script1.js, instead of after script1.js. As a result, the number of CSS files that need to be loaded is reduced from two to one (as compared with grouping), but you also change the order in which CSS and JavaScript definitions are loaded. It depends on your site whether that is an issue or not.
Loading CSS files from another site
If you decide to set combineCSSFiles to All, be sure that your site doesn't load CSS files from another web site. This is uncommon, but if yours is one of the rare sites that does this, consider this example:
<link rel="Stylesheet" type="text/css" href="/css/site1.css" />
<link rel="Stylesheet" type="text/css" href="/css/site2.css" />
<link rel="Stylesheet" type="text/css" href="http://anothersite.com/css/other.css" />
<link rel="Stylesheet" type="text/css" href="/css/site3.css" />
<link rel="Stylesheet" type="text/css" href="/css/site4.css" />
The package never combines CSS files from other sites - not with CSS files from your site, not with CSS files from the other site. As a result, if you change combineCSSFiles to All, the package will combine all CSS files loaded from your site (but not those from the other site), causing the following load order:
The definitions in other.css now came after those in site3.css and site4.css, meaning they may take precedence - which could break your CSS.
Determines whether the package combines JavaScript files.
Value | Description |
---|---|
None | JavaScript files are never combined |
PerGroup (default) | JavaScript files are combined per group. See explanation below. |
All | All JavaScript files are combined into a single JavaScript file. |
Example
<configuration>
...
<combineAndMinify combineJavaScriptFiles="All" >
</combineAndMinify>
...
</configuration>
As you saw in the description of combineCSSFiles, the package groups JavaScript files in the same way as CSS files. Similarly, if you set combineJavaScriptFiles to All, all JavaScript files that are loaded from your site get combined into a single JavaScript file.
However, there is one major difference with combining CSS files: When you combine all JavaScript files into a single file (combineJavaScriptFiles is All), the script tag for the combined file winds up at the location where the last original script tag used to be. This in contract to a combined CSS file, which winds up at the location of the first original CSS tag.
This makes life easier if you load JavaScript libraries from extenal sites. If you use a popular package such as jQuery, you can load it from the free Google Content Delivery Network (CDN) and also from the free Microsoft CDN - which saves you bandwidth and download time (details about those CDNs are in chapter 13 of my book).
Take this example:
<script type="text/javascript" src="/js/script1.js" ></script>
<script type="text/javascript" src="/js/script2.js" ></script>
<!-- load jQuery from free Google CDN -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="/js/script3.js" ></script>
<script type="text/javascript" src="/js/script4.js" ></script>
script3.js and script4.js may well be dependent on jquery.min.js. If combineJavaScriptFiles is set to PerGroup, you wind up with this load order:
So all definitions load in the same order as before the files were combined, as you'd expect. Now when you set combineJavaScriptFiles to All, you wind up with this load order:
This would probably still work well, because the definitions in script3.js and script4.js still get loaded after those in jquery.min.js. script1.js and script2.js now get loaded after jquery.min.js, but than jquery.min.js wouldn't have been dependent on those files anyway.
Determines whether the package minifies CSS files.
Value | Description |
---|---|
true (default) | CSS files get minified |
false | CSS files do not get minified |
Example
<configuration>
...
<combineAndMinify minifyCSS="false" >
</combineAndMinify>
...
</configuration>
Minifying a file means removing redundant white space and comments. This not only reduces your bandwidth usage and download times, but also makes it harder for outsiders to reverse engineer your web site. It also encourages developers to add comments to their CSS files (and especially their JavaScript files), now that those comments do not travel over the wire to the browser.
It is normally safe to minify files, so this feature is enabled by default. However, just in case removing white space or comments breaks your file, the option exists to switch it off.
Remember that the package doesn't minify your source files. It reads your source files, and minifies their content before sending them to the browser. To save CPU cycles, it caches the minified versions, using file dependencies to ensure cached versions are removed the moment you change the underlying files.
Determines whether the package minifies JavaScript files.
Value | Description |
---|---|
true (default) | JavaScript files get minified |
false | JavaScript files do not get minified |
Example
<configuration>
...
<combineAndMinify minifyJavaScript="false" >
</combineAndMinify>
...
</configuration>
To have your JavaScript, CSS and image files loaded from one or more cookieless domains, specify those domains using a cookielessDomains child element.
Example
<configuration>
...
<combineAndMinify ... >
<cookielessDomains>
<add domain="http://static1.mydomain.com"/>
<add domain="http://static2.mydomain.com/"/>
</cookielessDomains>
</combineAndMinify>
...
</configuration>
Cookieless domains help you boost web site performance in two ways. They let you cut down on the overhead created by cookies. And they let you get the browser to load more JavaScript, CSS and image files in parallel. Let's look at the cookie overhead first, and then the parallel loading.
If your site sets a cookie on the browser, then each time the browser sends a request to your site, that request contains the cookie. The issue is that the cookie is not only sent when requesting an .aspx file, but also when requesting static files, such JavaScript files, CSS files and images. In most cases, this is a waste of bandwidth (the exception would be if you have a handler that processes for example JavaScript files and that uses the cookie).
However, the browser won't send the cookie to another domain or sub domain. So if your page is at http://www.mydomain.com/page.aspx (using subdomain www), and you put your images and other static files on http://static1.mydomain.com (using subdomain static1), than the browser won't send cookies when requesting static files.
As an aside, if your site uses cookies (or ASP.NET sessions, which uses cookies), you should never allow visitors to access your pages without a subdomain. That is, don't allow them to access http://mydomain.com/page.aspx. Otherwise, if a visitor first accesses http://www.mydomain.com/page.aspx (using subdomain www), sets a cookie, and then comes back viahttp://mydomain.com/page.aspx (no subdomain), the browser won't send the cookie! IIS 7 makes it very easy to redirect requests to http://mydomain.com to http://www.mydomain.com using an entry in web.config. See Microsoft's iis.net, or the image control adapter included in chapter 12 of my book ASP.NET Site Performance Secrets.
When a browser loads a page, it loads the static files (images, JavaScript files, CSS files) in parallel to reduce the visitor's wait time. However, the browser limits the number of files that are loaded in parallel. Modern browsers (IE7 and better, Firefox, Chrome, Safari) have a limit of about 6, while older browsers (such as IE6) have a limit of 2.
However, this limit is per (sub)domain. It isn't per IP address. This means that if you spread your static files over for example 2 cookieless domains, you allow the browser to load up to two times more files in parallel.
If you add a cookielessDomains element with one or more domains to the combineAndMinify element, the package adds those domains to the urls of all static files in your site. This includes images referenced from CSS files.
This means that if you use:
<configuration>
...
<combineAndMinify ... >
<cookielessDomains>
<add domain="http://static1.mydomain.com"/>
</cookielessDomains>
</combineAndMinify>
...
</configuration>
Then for example
<img src="images/ball3.png" height="10" width="10" />
is replaced by
<img src="http://static1.mydomain.com/images/ball3.png" height="10" width="10" />
If you define multiple domains, such as:
<configuration>
...
<combineAndMinify ... >
<cookielessDomains>
<add domain="http://static1.mydomain.com"/>
<add domain="http://static2.mydomain.com"/>
</cookielessDomains>
</combineAndMinify>
...
</configuration>
Then the package attempts to spread the files over the available domains (note that you can add more than 2 domains if you want). This to get the browser to load more files in parallel. So if you have these image tags:
<img src="images/ball3.png" />
<img src="images/woodentoy4.png" />
You would wind up with:
<img src="http://static2.mydomain.com/images/ball3.png" />
<img src="http://static1.mydomain.com/images/woodentoy4.png" />
To create the subdomains, log into your account at your domain registrar and create the static1, static2, etc. subdomains (you can use any subdomain names you like). Make sure they point to the same IP address as your www subdomain. This way, you don't have to physically move your static files. Note that every subdomain that is not your www subdomain acts as a "cookieless" subdomain - it isn't like there are special "cookieless" subdomains as such.
Contrary to what you may think, if you have say 2 cookieless domains, the package won't use one domain for half the static files and the other domain for the other half. This is because it needs to make sure that on every page, a given static file is always given the same domain. If images/ball3.png were turned intohttp://static1.mydomain.com/images/ball3.png on one page, but to http://static2.mydomain.com/images/ball3.png on a second page, than the browser won't find ball3.png in its cache when it hits the second page, even if it stored ball3.png when it accessed the first page. Because of the different domains, it would regard the two urls as different, even though they actually point at the same resource.
Because of this requirement, the package uses the hash code of the file name to work out which domain to use. So if there are two domains, than if the hash is even it uses the first domain, and if it is odd it uses the second domain. Because it is unlikely that 50% of file names have an even hash code, you are unlikely to get a perfect distribution of the static files over the available domains.
Determines whether cookieless domains are used.
Value | Description |
---|---|
Never | Cookieless domains are never used. |
Always (default) | Cookieless domains are always used, provided that 1) the package is active, and 2) you have defined a cookielessDomains element with cookieless domains. |
ReleaseModeOnly | Cookieless domains are only used in release mode. |
DebugModeOnly | Cookieless domains are only used in debug mode. |
Example
<configuration>
...
<combineAndMinify active="Always" enableCookielessDomains="ReleaseModeOnly" >
<cookielessDomains>
<add domain="http://static1.mydomain.com"/>
<add domain="http://static2.mydomain.com"/>
</cookielessDomains>
</combineAndMinify>
...
</configuration>
This option is really only useful if you decide to activate the package in your development environment. In that case, you may decide to only use cookieless domains in release mode, while using all the other features of the package in both release and debug mode.
The reason for this is that if you have new images in your development environment that are not yet on your live site, than they won't show up in your development environment if you use the cookieless domains - which point to your live site.
If you want to take that route, set enableCookielessDomains to ReleaseModeOnly.
The default value for enableCookielessDomains is Always. However, keep in mind that for the package to use cookieless domains, it has to be active. And by default, it is only active in release mode.
Determines whether the package inserts code that preloads all images when the page starts loading.
Value | Description |
---|---|
true | All images are preloaded |
false (default) | No images are preloaded (except for those specified using child element prioritizedImages, described further below) |
Example
<configuration>
...
<combineAndMinify preloadAllImages="true" >
</combineAndMinify>
...
</configuration>
Normally, the browser only starts loading an image after it has encountered its image tag in the HTML or in a CSS file. If the image tag is towards the end of a big page, or if it takes a while to load the CSS file, it can take a while before image loading starts.
To get the browser to start loading all images immediately when the page itself starts loading, set the preloadAllImages attribute to true. The package than generates JavaScript at the start of the page head to load each image into the browser cache, along these lines:
<script type="text/javascript">
var img0=new Image();img0.src='http://www.codeproject.com/images/ball3.png';
var img1=new Image();img1.src='http://www.codeproject.com/css/chemistry.png';
var img2=new Image();img2.src='http://www.codeproject.com/images/toy4.png';
...
</script>
Now when the browser encounters an image tag, the image is already in browser cache, so the browser can show the image right away.
Allows you to prioritize certain images for preloading.
Example
<combineAndMinify ... >
<prioritizedImages>
<add url="images/salesbutton.png"/>
<add url="images/logo.png"/>
</prioritizedImages>
</combineAndMinify>
If you have many images on your pages, you may want to prioritize certain images. For example, if your "order now" button is an image, you want that image in front of your visitors as soon as possible.
You can use prioritizedImages without setting preloadAllImages to true. Here is how these two attributes interact:
prioritizedImages | preloadAllImages | Images preloaded |
---|---|---|
Empty or not present | true | All the images referred to from CSS files and all the images on the page are preloaded. They are loaded in the order in which their tags appear in the CSS or in the HTML. Images referred to from CSS are preloaded before images on the page itself. |
Empty or not present | false | None |
One or more urls | true | All urls listed in prioritizedImages are preloaded first, than all the other images. |
One or more urls | false | Only the urls listed in prioritizedImages are preloaded |
You can list any image urls in prioritizedImages, not just urls of images that are used on the page. If you know which page visitors are likely to go to next, you could use this to preload the images used on that next page.
Determines whether the package makes all image urls lowercase.
Value | Description |
---|---|
true | All images urls are converted to lowercase |
false (default) | Images url casing is left as it is |
Example
<configuration>
...
<combineAndMinify makeImageUrlsLowercase="true" >
</combineAndMinify>
...
</configuration>
You may be using inconsistent casing in your web pages to refer to the same image. For example:
<img src="/images/woodentoy4.png" height="10" width="10" />
...
<img src="/images/WoodenToy4.png" height="10" width="10" />
Assume a browser or proxy loads woodentoy4.png and stores it in its cache. When it then needs to load WoodenToy4.png, it may not recognize it is the same as the woodentoy4.pngthat it already has in cache, and send a superfluous request for WoodenToy4.png.
To prevent this, set makeImageUrlsLowercase to true. This way, all images urls in the HTML and CSS sent to the browser will be lowercase, so there is no inconsistent casing. Note that the package doesn't change your source files. Instead, it changes the HTML that gets generated from your source files and sent to the browser.
Determines whether the package 1) inserts version ids in image file names and 2) allows the browser to cache images for up to a year.
Value | Description |
---|---|
true | Version ids are inserted in image file names. Requires changes to web.config (see below). |
false (default) | No version ids are inserted in image file names |
Example
<configuration>
...
<combineAndMinify insertVersionIdInImageUrls="true" >
</combineAndMinify>
...
</configuration>
When a browser receives an image, it stores it in its browser cache. That way, if it needs to load the image again, it may still be in cache, so there is no need to send a request to the server. The result is less waiting for the visitor and less bandwidth used by your server.
One issue here is how long the browser should keep the image in its cache. Too long, and you may have visitors looking at outdated images. Too short, and the browser sends more requests for the image than necessary.
By setting insertVersionIdInImageUrls to true, you get the best of both worlds:
A few more details about how this feature works:
Now that the package inserts a version id in the image file names used in image tags, the browser will send requests for file names with version ids. Those file names won't match actual image files on your server, because they don't have the version ids. To solve this, you need to get the package to handle incoming requests for images, so it can remove the version id and load the correct file.
Additional Installation for IIS 7
Take these steps if you use IIS 7 for your live site, and also if you use the package in your development environment. The steps to take for IIS 6 are here.
In the installation instructions for IIS 7 for the entire package, you modified your web.config file to get the HTTP Handler to handle .js and .css files.
Now change web.config again, so the HTTP Hander also handles .gif, .png and .jpg files:
</configuration>
...
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
...
<handlers>
...
<!-- required for insertVersionIdInImageUrls attribute -->
<add name="GifHandler" verb="*" path="*.gif" type="CombineAndMinify.HttpHandler, CombineAndMinify" resourceType="Unspecified"/>
<add name="PngHandler" verb="*" path="*.png" type="CombineAndMinify.HttpHandler, CombineAndMinify" resourceType="Unspecified"/>
<add name="JpegHandler" verb="*" path="*.jpg" type="CombineAndMinify.HttpHandler, CombineAndMinify" resourceType="Unspecified"/>
<!-- required for all features -->
<add name="JavaScriptHandler" verb="*" path="*.js" type="CombineAndMinify.HttpHandler, CombineAndMinify" resourceType="Unspecified"/>
<add name="CssHandler" verb="*" path="*.css" type="CombineAndMinify.HttpHandler, CombineAndMinify" resourceType="Unspecified"/>
</handlers>
...
</system.webServer>
...
</configuration>
Additional Installation for IIS 6 or IIS 7 in classic mode
Follow these steps if your live site uses IIS 6. If your live site uses IIS 7 or higher, you can skip this.
In the installation instructions for IIS 6 for the entire package, you modified your web.config file to get the HTTP Handler to handle .js and .css files.
Now change web.config again, so the HTTP Hander also handles .gif, .png and .jpg files:
<configuration>
...
</system.web>
...
<httpHandlers>
...
<!-- required for insertVersionIdInImageUrls attribute -->
<add verb="*" path="*.gif" type="CombineAndMinify.HttpHandler, CombineAndMinify" />
<add verb="*" path="*.png" type="CombineAndMinify.HttpHandler, CombineAndMinify" />
<add verb="*" path="*.jpg" type="CombineAndMinify.HttpHandler, CombineAndMinify" />
<!-- required for all features -->
<add verb="*" path="*.js" type="CombineAndMinify.HttpHandler, CombineAndMinify" />
<add verb="*" path="*.css" type="CombineAndMinify.HttpHandler, CombineAndMinify" />
</httpHandlers>
...
</system.web>
...
<configuration>
For a lot more on browser caching and proxy caching, see www.iis.net or chapter 12 of my my recently released book ASP.NET Performance Secrets.
Determines whether the package throws an exception when an image file is missing.
Value | Description |
---|---|
Never (default) | The package never throws an exception when an image file is missing. |
Always | The package always throws an exception when an image file is missing. |
ReleaseModeOnly | The package only throws an exception if the site is in release mode. |
DebugModeOnly | The package only throws an exception if the site is in debug mode. |
Example
<configuration>
...
<combineAndMinify exceptionOnMissingFile="DebugModeOnly" insertVersionIdInImageUrls="true" >
</combineAndMinify>
...
</configuration>
Assume insertVersionIdInImageUrls is set to true, so the package inserts a version id in all image urls. This means it has to access each image file to find its last updated time. What happens if the image file cannot be found? That is determined by the exceptionOnMissingFile attribute:
If all images should be present in your development environment, than it makes sense to set exceptionOnMissingFile to DebugModeOnly. That way, you quickly find broken images while developing your site, while preventing exceptions in your live site where you probably prefer a broken image over an exception.
What about JavaScript and CSS files? The package accesses these files when combining and / or minifying them:
Keep in mind that if you want exceptions while the site is in debug mode, you have to ensure that the package is actually active in debug mode - set active to Always to make that happen.
Determines whether the package removes superfluous white space and comments from the HTML of the page.
Value | Description |
---|---|
true | Superfluous white space and comments are removed. |
false (default) | No superfluous white space and comments are removed. |
Example
<configuration>
...
<combineAndMinify removeWhitespace="true" >
</combineAndMinify>
...
</configuration>
When you set removeWhitespace to true, the package removes all HTML comments from the page and collapses all runs of white space into a space. However, if a run of white space contains one or more line breaks, it is collapsed into a line break. That way, inline JavaScript will not be broken.
Determines how tags for combined JavaScript files and CSS files are cached.
Value | Description |
---|---|
None (default) | Caching of replacement tags is switched off. |
PerSite | There is a single cache entry for the entire site. |
PerFolder | There is a cache entry per folder. |
PerPage | There is a cache entry per page (ignoring any query string). |
PerUrl | There is a cache entry per url (including any query string). |
Example
<configuration>
...
<combineAndMinify headCaching="PerSite" >
</combineAndMinify>
...
</configuration>
Even though the package caches all minified and combined files, there is still some work involved in replacing tags of individual JavaScript files and CSS files with tags of combined files. Without further caching, this needs to be done for each page request.
To reduce the CPU usage involved in this, the package provides the option to cache the replacement tags. The recommended way to do this depends on the way you load your JavaScript and and CSS files:
Situation | Recommended Value |
---|---|
The additional CPU usage of the package is not an issue. Or the tags to load JavaScript and CSS files are totally ad hoc per page. | None (default) |
All pages load the same JavaScript and CSS files in the same order. For example, all pages uses a single master page, and the master page has all the script and link tags to load the JavaScript and CSS files. | PerSite |
Your pages are split over folders, and the JavaScript and CSS files you load depend on the folder. For example, pages in theadmin folder all load the same JavaScript and CSS files in the same order, but those files are different from the ones loaded by pages in the products folder. | PerFolder |
Each page loads different JavaScript and / or CSS files. However, the query string doesn't influence which files are loaded. So toys.aspx?id=1 and toys.aspx?id=2 load the same files in the same order, but categories.aspx loads different files. | PerPage |
The JavaScript and CSS files used by a page depend on the entire url, including its query string. So toys.aspx?id=1 andtoys.aspx?id=2 load different files. | PerUrl |
The headCaching attribute really comes into its own if you load JavaScript and CSS files from a master page or a shared user control. This is because the package caches entire groups of tags, and the tags to replace those groups with. This process is sensitive to for example white space in between tags. That means that
<script type="text/javascript" src="/js/script1.js" ></script>
<script type="text/javascript" src="/js/script2.js" ></script>
and
<script type="text/javascript" src="/js/script1.js" ></script>
<script type="text/javascript" src="/js/script2.js" ></script>
are not the same, due to the extra white line in the second block.
This package will improve the performance of any web site that loads JavaScript, CSS or image files. Please give it a try with your site. If you find any bugs or have any trouble with the documentation, let me know and I'll try to fix the issue. Feature requests would be welcome too.
Matt has over 6 years .NET and SQL Server development experience. Before getting into .Net, he worked on a number of systems, ranging from the largest ATM network in The Netherlands to embedded software in advanced Wide Area Networks. He has lived and worked in Australia, The Netherlands, Slovakia and Thailand.
He recently wrote a book, ASP.NET Performance Secrets (www.packtpub.com/asp-net-site-performance-secrets/book) in which he shows in clear and practical terms how to quickly find the biggest bottlenecks holding back the performance of your web site, and how to then remove those bottlenecks. The book deals with all environments affecting a web site - the web server, the database server and the browser. Buy a copy today for yourself or for your developers.
Matt currently lives in Sydney, Australia, where he works at ticketek.com.au, a high profile web site where most Australians buy their event tickets.