Code, Code, Revolution!
Enabling gzip compression is an easy way to make your site load faster, but how do you go about doing so for your EPiServer site and why should you?
Gzip compression will reduce the size of a text file (such as html markup, css, js and files) up to some 90%. If your user is on a slow connection, reducing the size of data does make a difference. The number of users on “slow” connections are actually growing by every sold smart phone and mobile broadband connection. Here’s a real world example:
| Content | Uncompressed | Compressed |
|---|---|---|
| Markup | 40kb | 40kb |
| CSS | 104kb | 18kb |
| JS | 368kb | 119kb |
| Toal | 512 kb | 177 kb |
Gzip will typically compress a text file around 65-90%. In this example this example we reach an average compression of about 70% for the static content. A web page usually contains more CSS and JavaScript content than the size of the specific pages’ markup. Although, this is only true for the initial page the user visit, for subsequent page views the visitors’ browser has already cached the static content making page markup the biggest part of text content.
If you’re on a broadband connection, meaning 2mbit+ and not including wireless broadband, downloading the page (ignoring images) uncompressed will take less than 2 seconds. If you’re on a slower connection however, say, 0.2-0.5 mbit, it will take about 7-10 seconds. With compression enabled a mobile user with not optimal coverage (which seems more common than good coverage with Telia) you can save some 5 seconds which is significant if you’re that user. There’s another aspect to compression aswell. Say you run a large site with 100.000 unique visitors per day, by reducing at least the first page view by 330kb that reduce your daily traffic with a whopping 3,9Gb of data!
Compressing the markup will reduce the size for each request but because the markup is generally not very big the download time improvement will not be as significant. Compressing dynamic content will also use up more server resources for each page view. IIS will store/cache compressed static files on disk at first request so the impact of compressing static files is minimal.
Here’s an old but still very informative blog post by Yahoo on front end optimization.
First of all, compression need to be activated in IIS. There are a few guides for this already on the net but most of them tell you to manually edit the metabase xml file, while that is all well and dandy you could potentially break your IIS. The metabase file is like the registry for IIS, all IIS settings you find in IIS Manager are stored there. If you do edit manually, backup the metabase file first. If you want to play it safe (and I think you should), download the IIS resource kit tools which includes a metabase editor (IIS Metabase Explorer).
![]() |
![]() |
![]() |
You have now configured compression of static files in IIS!
Yes, normally the steps above would have enbled compression for you. In previous versions of EPiServer this was enough. In CMS 5 however the EPiServer.Web.StaticFileHandler is registered in web.config to handle both .css and .js files. Because code is used to deliver the files and we configured static file compression only the files won’t be compressed. Removing the HttpHandlers will break EPiServer so that is not an option. The solution is to add locations to your web.config and remove the HttpHandlers for those directories. That way IIS will deliver the static files in those folders and EPiServer will still function. Another option would be to enable compression of dynamic files but even if you do I would still advice towards using static compression of your static files because of the cache function. Here’s an example of how to configure your javascript and css folder in web.config to enable static file compression.
<location path="javascript"> <system.web> <httpHandlers> <remove path="*.js" verb="GET,HEAD" /> </httpHandlers> </system.web> </location> <location path="styles"> <system.web> <httpHandlers> <remove path="*.css" verb="GET,HEAD" /> </httpHandlers> </system.web> </location>
That’s it! Your static content will now be gzip’ed!
With this blog I try to provide useful tips and solutions for programming .NET, Objective-C and more. My name is Björn Sållarp, and I love writing code.
It's now available on AppStore. It's free and open source. Read more about the app here: Swedish / English
Rune Jensen
February 5th, 2010 at 12:22 pm
We have a intranet EPIServer and this compression imediately gave us a performance improvement of 25%. But when we open the site in Edit mode or Admin mode it look corrupted (no styling etc…..) – the web.config part “admUI” looks like this:
Can you help?
Björn Sållarp
February 5th, 2010 at 7:40 pm
@Rune,
Your web.config settings are not included in your comment, I think wordpress remove that stuff because of the XML format. But it sounds like you have removed episervers’ http handlers for .css and .js files or you made some mistake when clearing those handlers in your location tag.
If you need assistance, email the config and I can have a look. My email is: bjorn.sallarp@gmail.com.
Emil Birgersson
March 1st, 2010 at 10:50 am
Thanks for a great article, One comment regarding compression of dynamic files: Do you really need to check “compress application files” on the service tab? If I am not entirely misstaken this checkbox corresponds to enabling LM/W3SVC/Filters/Compression/Parameters/HcDoDynamicCompression in Metabase explorer? If we only need static file compression (css, js), we shouldn’t need to enable dynamic compression right? I would prefer leaving this setting off to make sure the server wont try to gzip dynamic content, eg .aspx-files.
Björn Sållarp
March 2nd, 2010 at 7:28 am
@Emil, You are correct! My mistake, you don’t have to enable dynamic compression, however, it won’t make any difference for you doing so because IIS 6 by default doesn’t include aspx, asmx etc. in the dynamic compression extension list.
I’ve updated the post and removed the dynamic compression setting.
// Björn
Carlos
May 5th, 2011 at 9:13 am
Great article. Just one question. Without Epi Static Headers how does the browser knows that the file should be cached? I followed your instructions and it works now but then my response headers do not include Cache-Control: private
In other words, I am saving bandwidth since my file is around 50% smaller but then it will be downloaded more often since the headers do not tell the browser to cache the file? Am I correct? If so how can I achive both.
Many thanks
Björn Sållarp
May 13th, 2011 at 3:14 pm
@Carlos
You can set the cache headers in IIS for these files