Blazor / ASP.NET文本压缩-Google速度测试不同意,为什么?

问题描述

我有一个炫酷的应用程序,在其中添加了文本压缩功能

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

    // We use brotli by default : https://docs.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;
    });

app.UseResponseCompression();

当我在浏览器中签入时,似乎压缩已激活:

enter image description here

现在,我测试网站的速度,第一个建议是添加文本压缩。 所以,我不明白为什么会有这样的消息:

enter image description here

有人对这个问题有任何想法吗?

解决方法

The ASP.NET Core docs概述,使用CompressionLevel.Fastest将导致压缩完成最快,而不是网页加载最快。

要获得最高级别的压缩,您应该使用CompressionLevel.Optimal

enter image description here

,

我找到了问题,希望它可以对其他人有所帮助。必须在UseStaticFiles之前放置对UseResponseCompression的调用,这样它也将考虑所有静态​​(css,js)文件。

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

app.UseStaticFiles();