Search code examples
azuresvghttp-compression

Enable GZip compression for SVG in Azure Web Sites?


I'm trying to enable GZip compress for SVG in an Azure Web Site using web.config transforms without success. Here is what my transform looks like:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.webServer>
    <httpCompression>
      <staticTypes>
        <add mimeType="image/svg+xml" enabled="true" xdt:Transform="Insert" />
      </staticTypes>
    </httpCompression>
    <staticContent xdt:Transform="Insert">
      <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
    </staticContent>
  </system.webServer>
</configuration>

This should both add the mime type for SVG, which Azure doesn't seem to have, and then enable compression. I've verified the mime type addition works fine, but upon publishing I get an error for the compression elements:

No element in the source document matches '/configuration/system.webServer/httpCompression/staticTypes'

Removing the compression from the transform and adding it directly to my web.config file removes the error, but I still don't see the compression in the HTTP headers. Here are the response headers:

Accept-Ranges:bytes
Content-Length:23265
Content-Type:image/svg+xml
Date:Mon, 10 Jun 2013 17:19:37 GMT
ETag:"c4e9ec93d765ce1:0"
Last-Modified:Mon, 10 Jun 2013 12:39:41 GMT
Server:Microsoft-IIS/8.0
X-Powered-By:ASP.NET
X-Powered-By:ARR/2.5
X-Powered-By:ASP.NET

Solution

  • Here is how you can enable it in your web.config:

    <configuration>
       <system.webServer>
          <staticContent>
             <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
          </staticContent>
          <httpCompression>
             <staticTypes>
               <remove mimeType="*/*" />
               <add mimeType="image/svg+xml" enabled="true" />
               <add mimeType="*/*" enabled="false" />
             </staticTypes>
          </httpCompression>
       </system.webServer>
    </configuration>
    

    The key line is the removal of the catch-all (and later re-add). If you don't have that, then the svg line basically gets ignored since the catch-all is inherited from applicationhost.config, and catches all before it reaches svg line.