Getting IIS 7 to Compress JavaScript
One of the many recommendations that Yahoo makes on optimizing your web site for high amounts of traffic, and to make the response time speedier to your user is GZip encoding all your static content. I usually do this as a standard for setting up any of my Web Servers, in addition to setting expiration headers on my static content, to ensure that I am serving as little content as possible.
IIS 7 has improved and simplified the compression and serving of static files by making it easier to setup and configure than previously in IIS 6.0. The IIS 7.0 compression works perfectly for CSS, HTML, and Text files, however JavaScript is another story. The JavaScript files on my IIS 7.0 server were not being compressed and served with GZip encoding, which is a major problem for any Web 2.0 site where 75% of your content severed per request is JavaScript. (I just made that number up, but it sounds right!)
I found Rick Strahl’s post on this very subject that he wrote up about a 9 months ago. It was helpful in diagnosing my problem, however it didn’t solve it. The HTTP compression is configured in IIS 7.0’s ApplicationHost.config file (c:\windows\system32\inetsrv\config\applicationhost.config), see below for the default settings:
<httpCompression directory="%SystemDrive%\websites\_compressed" minFileSizeForComp="0">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
As you can see anything that starts with the MIME type of text or message is GZip encoded just fine. However there is also application/javascript as a compressible MIME type, there is nothing wrong with that, because there are 3 accepted ways to set a JavaScript MIME type.
text/javascriptapplication/x-javascriptapplication/javascript
However the problem comes in when you look at the default MIME type mappings setup, in the same ApplicationHost.config file, a little further down.
...
<mimeMap fileExtension=".jpg" mimeType="image/jpeg" />
<mimeMap fileExtension=".js" mimeType="application/x-javascript" />
<mimeMap fileExtension=".jsx" mimeType="text/jscript" /
...
As you may notice the MIME type for JavaScript files is set to application/x-javascript, which is not the same as the default in the compression section above. So I added the following MIME type, application/javascript, to my Web.config thinking I had the problem licked, and all that I had to do was change the default MIME type for JavaScript files.
<system.webServer>
<staticContent>
<remove fileExtension=".js" />
<mimeMap fileExtension=".js" mimeType="application/javascript" />
</staticContent>
</system.webServer>
However that didn’t work either, and it should have because the MIME type now matched my compression MIME type. I even verified the MIME type in fiddler. So I then tried my last option to change the MIME type to text/javascript, which is the defacto standard on the internet for JavaScript MIME types.
<system.webServer>
<staticContent>
<remove fileExtension=".js" />
<mimeMap fileExtension=".js" mimeType="text/javascript" />
</staticContent>
</system.webServer>
Finally, this was the key to getting the JavaScript GZip Compression working IIS 7.0. And this didn’t require me to modify the ApplicationHost.config file get it done. Which is something I love about the new IIS 7.0, I can do my whole server configuration through FTP and my Web.config file.
Tags: Compression, GZip, IIS, IIS 7.0, JavaScript








April 15th, 2008 at 2:51 pm
Nick:
I tried to make the same modifications you mentioend for the web.config file so that I could get JS compression working on my defautl website, but I get a 500.19 error. I’m not that familiar with making changes to the web.config file… do you have any advice? I’m pasting the full error message below:
***
Server Error in Application “Default Web Site”
HTTP Error 500.19 – Internal Server Error
Description: The requested page cannot be accessed because the related configuration data for the page is invalid.
Error Code: 0×00000000
Notification: SendResponse
Module: CustomErrorModule
Requested URL: http://localhost/docs/EktronJS/default.htm
Physical Path: C:\source\Engineering\Internal_Documentation\EktronJS\default.htm
Logon User: Anonymous
Logon Method: Anonymous
Handler: StaticFile
Config Error: This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault=”Deny”), or set explicitly by a location tag with overrideMode=”Deny” or the legacy allowOverride=”false”.
Config File: \\?\C:\inetpub\wwwroot\web.config
Config Source:
40:
41:
42:
Thanks for any advice you can offer.
Keith
April 15th, 2008 at 3:12 pm
Keith,
Honestly what I found is that IIS 7.0 seems to be slow at putting what is in the web.config in to effect on the site. Unlike IIS 6.0 where everything was instantaneous, IIS 7.0 seems to take its sweet time with things. Eventually I just gave up and came back later and the site started compressing JavaScript.
May 12th, 2008 at 10:38 pm
The lines with “application/x-javascript” is part of the dynamicContent node. I’m not exactly sure why IIS7 feels javascript is dynamic content, but I got this working when I enabled compression of dynamic content through the IIS Manager without messing with any of the MIME types (primarily because I’m not sure if that will break MS AJAX or the like).
May 13th, 2008 at 2:41 pm
I guess that makes total sense, Doug, since Microsoft in all their wisdom to support their ASP.NET AJAX put JavaScript in in the dynamicContent section. Because their AJAX is generated on the fly. Sort of sucks for the 99% of all other JavaScript files that are not automatically generated.
June 11th, 2008 at 7:38 pm
IIS 如何启用 GZip 压缩…
微软 IIS 上如何启用 Gzip 压缩机制? 或许看过 YSlow 优化规则并且正在使用 IIS 的朋友比较关心这个问题。 基本步骤可以参考微软官方指导,直接一点的方式通过命令行执行如下命令启用对动……
July 28th, 2009 at 12:53 pm
[DW-798] Set expiration of js and swf files on IIS…
Highlight the folder level and adjust the HTTP Headers tab on the properties. The AOClient and JS folders should be set to expire far into the future. Under the Flash directory the embed.js file should also be set.
There is an ISAPI filter tool for….
October 13th, 2009 at 10:28 am
OK, I have this exact problem and would like to make the corrections however; I know nothing about how to do this and can’t seem to find any directions. Can you help?