Thursday, October 11, 2012

IIS7, JSON Compression, and You

How do you know if your requests are being compressed?

Fiddler. Select resource > Response Section > Transformer tab. Also, the Headers tab should have a Transport section listing Content-Encoding: gzip.

How do I enable it in IIS 7?

First, just get Dynamic and Static Compression running by going to the IIS level or the site level, selecting the Compression feature, and checking the boxes. You may need to install it if you don’t see these options.

How come my JSON requests aren’t being compressed?

The JSON MIME type isn’t compressed by default. That MIME type is application/json.

Great. So how do I add it?

You can use the appcmd.exe or you can edit the file directly. C:\Windows\System32\inetsrv\Config\applicationHost.config is locked by the system fairly tightly, but I did the following. Make a backup. From the Start menu, right click Notepad and start as administrator. Use File > Open to go to the config file. Go to the <httpCompression> node. In both <dynamicTypes> and <staticTypes>, add both <add mimeType="application/json" enabled="true" /> and <add mimeType="application/json; charset=utf-8" enabled="true" />. Note the charset in the second item. That has to match what is sent from the server, exactly. Again, I pulled that from Fiddler by inspecting one of the responses' headers and checking the Content-Type value. I restarted at the IIS level to get the changes recognized.

No comments: