Search code examples
azure-web-app-servicecompressionazure-blob-storageblazor-server-side

Should I turn on compression for Azure App Server and Azure BLOBs? And if so, how?


I have a Blazor server app. Every byte[] object in the app is a BLOB. It does have .css and .js files as local files under wwwroot but every png, docx, svg, and the few static html files are BLOBs.

So the first question is, do I want to turn on compression for the App Service and/or BLOB Storage? png & docx files are already compressed. The svg & html files I have are small so a win on less bandwidth, but a performance hit on decompressing. And for the SPA rendered by Blazor server, that's components being built/changed on the fly over a SignalR connection. So is compression there useful (same small sizes issue)?

And if it's a win, how do I turn it on? As I see it there are three places to enable it. For SignalR & BLOB I can't find where/how. And for the website static files, there's different answers on each search result.

  1. SignalR connection rendering pages
  2. Static .css & .js files under wwwroot local to the website
  3. BLOB files that are compressible (html, svg)

Solution

  • Yes, you can turn on compression for Azure App Service.

    As per this MSDoc

    When a Blazor WebAssembly app is published, the output is statically compressed during publish to reduce the app’s size and remove the overhead for runtime compression.

    You can also see the option to enable the Compression in the same doc.

    Add the below line in .csproj file.

    <BlazorEnableCompression>true</BlazorEnableCompression>
    

    To open .csproj file double click on the Solution folder

    OR

    Right click on the Project folder and click on Unload Project (Reload the project once you are done with changes).

    enter image description here

    SignalR Connection rendering pages.

    From this MSDoc,I found Nuget package to add the Compression middleware for SignalR.

    But it seems to be deprecated.

    enter image description here

    • I am able to add AddResponseCompression and UseResponseCompression() without any reference packages.

    My Program.cs file

    builder.Services.AddResponseCompression(options =>
    {
        options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                   new[] { "application/octet-stream" });
    });
    var app = builder.Build();
    
    app.UseResponseCompression();
    

    As you are using a Blazor Server App, after deploying the App to Azure App service a Web.config file is generated.

    We can also edit the Web.config file and add the settings to compress the App Service by adding the mime types.

    Refer this SO Thread which explains the same issue.