09 Apr 2008

Getting IIS 7 to Compress JavaScript

13 Comments Uncategorized

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.

  1. text/javascript
  2. application/x-javascript
  3. application/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: , , , ,
written by
Nick Berardi
subscribe
If you found this post valuable and would like to see more like it you can follow me.

13 Responses to “Getting IIS 7 to Compress JavaScript”

  1. Reply Keith Pepin says:

    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

  2. Reply Nick Berardi says:

    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.

  3. Reply Doug Mayer says:

    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). :)

  4. Reply Nick Berardi says:

    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.

  5. Reply april says:

    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?

  6. Reply Shash says:

    I cannot get IIS7 to compress even css files, I’m using Windows web server. None of the files are getting compressed. Although I could get compression working on IIS6.

  7. Reply Nick Berardi says:

    For anybody interested the trick to getting compression working is a requirement berried deep in TechNet. You must hit the same page on IIS 7 with two different IP addresses from two different machines. If you keep trying to hit the page with the same machine it will never compress the page. Weird, but true.

  8. Reply Vilsad says:

    I tried all these but still not working js and css. can you advice on this ?

  9. Reply Sumit Kaldate says:

    Hi Nick,

    Thanks a lot for the solution…It worked for me..

Leave a Reply