Search code examples
angulariisgzipbrotli

Deploying Angular 8 Project after brotli compression


I have a project in Angular 8. I have referred to https://medium.com/@subodhkumarjc/angular-build-optimization-part-2-compression-b866dd0593c3 to implement bortli compression in my project since my main.js files were coming pretty big in size after normal build. After runing ng build --prod. I get the following set of dist files. File explorer view

Now, generally to deploy this in IIS I just used to do a simple copy paste from my current local dist folder to my web folder pointed by IIS in the server. But When I am trying to do the same thing, My browser is still loading the main.js file instead of the compressed main.js.br file.

Can anyone help me with what I am missing here while deploying.

Note: As of now, I have not done any changes with my IIS


Solution

  • try to add the below code in your web.conifg file:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <remove name="X-Powered-By" />
                </customHeaders>
            </httpProtocol>
            <rewrite>
                <rules>
                    <clear />
                    
                    <rule name="br_rewrite" enabled="true" stopProcessing="true">
                        <match url="(.*).(js$|svg|css)" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{HTTP_ACCEPT_ENCODING}" pattern="br" />
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" />
                        </conditions>
                        <action type="Rewrite" url="{R:1}.{R:2}.br" logRewrittenUrl="true" />
                    </rule>
                    <rule name="gzip_rewrite" enabled="true" stopProcessing="true">
                        <match url="(.*).(js$|svg|css)" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{HTTP_ACCEPT_ENCODING}" pattern="gzip" />
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" />
                        </conditions>
                        <action type="Rewrite" url="{R:1}.{R:2}.gz" logRewrittenUrl="true" />
                    </rule>
                </rules>
                <outboundRules rewriteBeforeCache="true">
                    <rule name="Remove Server header" enabled="true">
                        <match serverVariable="RESPONSE_Server" pattern=".+" />
                        <action type="Rewrite" value="" />
                    </rule>
                    <rule name="Rewrite content-encoding header gzip" preCondition="IsGZ" enabled="true" stopProcessing="false">
                        <match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
                        <action type="Rewrite" value="gzip" />
                    </rule>
                    <rule name="Rewrite content-encoding header br" preCondition="IsBR" enabled="true" stopProcessing="false">
                        <match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
                        <action type="Rewrite" value="br" />
                    </rule>
                    <rule name="css content type" preCondition="IsCSS" enabled="true" stopProcessing="false">
                        <match serverVariable="RESPONSE_CONTENT_TYPE" pattern="(.*)" />
                        <action type="Rewrite" value="text/css" />
                    </rule>
                    <rule name="js content type" preCondition="IsJS" enabled="true" stopProcessing="false">
                        <match serverVariable="RESPONSE_CONTENT_TYPE" pattern="(.*)" />
                        <action type="Rewrite" value="application/javascript" />
                    </rule>
                    <rule name="svg content type" preCondition="IsSVG" enabled="true" stopProcessing="false">
                        <match serverVariable="RESPONSE_CONTENT_TYPE" pattern="(.*)" />
                        <action type="Rewrite" value="image/svg+xml" />
                    </rule>
                    <preConditions>
                        <preCondition name="IsGZ">
                            <add input="{URL}" pattern="\.gz$" />
                        </preCondition>
                        <preCondition name="IsBR">
                            <add input="{URL}" pattern="\.br$" />
                        </preCondition>
                        <preCondition name="IsCSS">
                            <add input="{URL}" pattern="css" />
                        </preCondition>
                        <preCondition name="IsJS">
                            <add input="{URL}" pattern="js" />
                        </preCondition>
                        <preCondition name="IsSVG">
                            <add input="{URL}" pattern="svg" />
                        </preCondition>
                    </preConditions>
                </outboundRules>
            </rewrite>
            <urlCompression doStaticCompression="true" doDynamicCompression="false" />
            <httpCompression sendCacheHeaders="false" />
            <staticContent>
                <mimeMap fileExtension=".br" mimeType="application/brotli" />
                <clientCache cacheControlMode="UseMaxAge" />
            </staticContent>
        </system.webServer>
    </configuration>