Monday, 22 June 2009

JavaScript Compression in IIS 7

In IIS 7.0, the HTTP compression is configured in the ApplicationHost.config file (c:\windows\system32\inetsrv\config\applicationhost.config.
<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>
Anything that starts with the MIME type of text or message will be compressed, but it maps only a single javascript mime type to be compressed, application/javascript.

However, the default mappings in the ApplicationHost.config are set to:

<mimeMap fileExtension=".jpg" mimeType="image/jpeg" />
<mimeMap fileExtension=".js" mimeType="application/x-javascript" />
<mimeMap fileExtension=".jsx" mimeType="text/jscript" />


As you can see, 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. To solve this, you need the add the MIME type application/javascript, to your Web.config.
<system.webServer>
...
<staticContent>
<remove fileExtension=".js" />
<mimeMap fileExtension=".js" mimeType="application/x-javascript" />
</staticContent>
</system.webServer>

No comments: