Blazor / ASP.NET Text Compression - Google speed test do not agree, why?

1.6k views Asked by At

I have a blazor application where I added text compression, that way:

    context.Services.AddResponseCompression(o =>
    {
        o.EnableForHttps = true;
    });

    // We use Brotli by default : https://learn.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-3.1
    //services.Configure<GzipCompressionProviderOptions>(o => o.Level = System.IO.Compression.CompressionLevel.Optimal);
    context.Services.Configure<BrotliCompressionProviderOptions>(options =>
    {
        options.Level = CompressionLevel.Fastest;
    });

and

app.UseResponseCompression();

When I check in my browser, it seems that the compression is activated:

enter image description here

Now, I test the web site speed and the first proposal is to add Text Compression. So, I do not understand why I have such a message:

enter image description here

Does someone have any idea of the problem?

3

There are 3 answers

1
Matt Hensley On

The ASP.NET Core docs outline that using CompressionLevel.Fastest will result in the compression completing the fastest, not the webpage loading the fastest.

To get the highest level of compression you should use CompressionLevel.Optimal.

enter image description here

0
ClubberLang On

I have find the issue, hope it will help anyone else. The call to UseResponseCompression must be placed BEFORE UseStaticFiles, that way it will also account for all the static (css,js) files.

// Must be before UseStaticFiles to compress static
//files and UseMvc to compress MVC responses
app.UseResponseCompression();

app.UseStaticFiles();
0
mrndelfino On

Don't know if you solved it but, you are missing the provider. Also, Fastest in better than Optimal in most scenarios. Optimal is slower and in certain scenarios Fastest compression does better than Optimal at compression, and also is faster. So, in most scenarios Fastest is the way to go.

services.AddResponseCompression(options =>
        {
            options.Providers.Add<BrotliCompressionProvider>();
            options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                new[] { "application/octet-stream" });
            options.EnableForHttps = true;
        });