Skip to content

Instantly share code, notes, and snippets.

@aarongustafson
Created September 20, 2016 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarongustafson/1fc78f0c0f445eee17495739390da73f to your computer and use it in GitHub Desktop.
Save aarongustafson/1fc78f0c0f445eee17495739390da73f to your computer and use it in GitHub Desktop.
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.webServer>
<urlCompression doStaticCompression="true" doDynamicCompression="true" dynamicCompressionBeforeCache="false" />
<!-- add a mime type for br, otherwise we get 404 -->
<staticContent>
<remove fileExtension=".br" />
<mimeMap fileExtension=".br" mimeType="application/brotli" />
</staticContent>
<rewrite>
<rewriteMaps>
<!-- pre-compressed files will be suffixed with br or gz -->
<!-- map of correct mime types to be restored -->
<rewriteMap name="CompressedExtensions" defaultValue="">
<add key="js.gz" value="application/javascript" />
<add key="css.gz" value="text/css" />
<add key="js.br" value="application/javascript" />
<add key="css.br" value="text/css" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="ServerPreCompressedBrotli" stopProcessing="true">
<match url="^(.*/)?(.*?)\.(css|js)([?#].*)?$" />
<conditions>
<!-- client accepts brotli -->
<add input="{HTTP_ACCEPT_ENCODING}" pattern="br" negate="false" />
<!-- pre-compressed brotli file exists -->
<add input="{REQUEST_FILENAME}.br" matchType="IsFile" negate="false" />
</conditions>
<!-- serve pre-compressed file -->
<action type="Rewrite" url="{R:1}{R:2}.{R:3}.br{R:4}" />
</rule>
<rule name="ServerPreCompressedZopfli" stopProcessing="true">
<match url="^(.*/)?(.*?)\.(css|js)([?#].*)?$" />
<conditions>
<!-- client accepts gzip -->
<add input="{HTTP_ACCEPT_ENCODING}" pattern="gzip" negate="false" />
<!-- pre-compressed zopfli file exists -->
<add input="{REQUEST_FILENAME}.gz" matchType="IsFile" negate="false" />
</conditions>
<!-- serve pre-compressed file -->
<action type="Rewrite" url="{R:1}{R:2}.{R:3}.gz{R:4}" />
</rule>
</rules>
<outboundRules>
<!-- restore the correct mime type -->
<rule name="RestoreMime" enabled="true">
<match serverVariable="RESPONSE_Content_Type" pattern=".*" />
<conditions>
<add input="{HTTP_URL}" pattern="\.((?:css|js)\.(gz|br))" />
<add input="{CompressedExtensions:{C:1}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" value="{C:3}" />
</rule>
<rule name="RemoveRangeBrotli" preCondition="PreCompressedBrotli" enabled="false">
<match serverVariable="RESPONSE_Content_Range" pattern=".*" />
<action type="Rewrite" value="" />
</rule>
<rule name="RemoveRangeZopfli" preCondition="PreCompressedZopfli" enabled="false">
<match serverVariable="RESPONSE_Content_Range" pattern=".*" />
<action type="Rewrite" value="" />
</rule>
<!-- indicate response is encoded with brotli -->
<rule name="AddEncodingBrotli" preCondition="PreCompressedBrotli" enabled="true" stopProcessing="true">
<match serverVariable="RESPONSE_Content_Encoding" pattern=".*" />
<action type="Rewrite" value="br" />
</rule>
<!-- indicate response is encoded with gzip -->
<rule name="AddEncodingZopfli" preCondition="PreCompressedZopfli" enabled="true" stopProcessing="true">
<match serverVariable="RESPONSE_Content_Encoding" pattern=".*" />
<action type="Rewrite" value="gzip" />
</rule>
<preConditions>
<preCondition name="PreCompressedZopfli">
<add input="{HTTP_URL}" pattern="\.((?:css|js)\.gz)" />
</preCondition>
<preCondition name="PreCompressedBrotli">
<add input="{HTTP_URL}" pattern="\.((?:css|js)\.br)" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment