Sunday 5 January 2020

Why is Brotli compression not working in ASP.NET Core?

Now that Brotli has become a widely supported encoding format, I thought of trying it out myself so that I can experience the gains on network bandwidth usage.

However, I faced an interesting challenge. I guess anyone, who tries to use Brotli compression on an ASP.NET core website, might face it and this challenge is primarily related to the way ASP.NET core lines up the modules in its pipeline.

I created an empty ASP.NET Core ReactJS website (I called it "BrotliExperiment") and added following code in the ConfigureServices method of Startup.cs.

             services.Configure(options =>
            {
                options.Level = CompressionLevel.Optimal;
            });

            services.AddResponseCompression(options =>
            {
                options.EnableForHttps = true;
                options.Providers.Add();
            });

I also enabled response compression in the Configure method of Startup.cs using following code.

           app.UseResponseCompression();

However, when i tried to check the response headers, it was not present :(.


So, i figured out from the internet that the issue is that UseResponseCompression method should called before UseStaticFiles method. So I moved it above the UseStaticFiles method.

And it works :)