BlazorWasmBrotliLoader.Build

A NuGet package that provides post published processing to rewrite the fallback page to load Brotli compressed Blazor WebAssembly application files.

MPL-2.0 License

Stars
6

BlazorWasmBrotliLoader.Build

📝Summary

When you publish your Blazor WebAssembly app, this package rewrites the fallback page file (wwwroot/index.html) to be loading Brotli pre-compressed application files, such as *.wasm.br.

Pre-compressed files are smaller than original one, so this package will help make your Blazor WebAssembly app boot much faster than usual, sinse it reduce the initial content loading time.

🚀Quick Start

Install this package to your Blazor WebAssembly project.

dotnet add package BlazorWasmBrotliLoader.Build

Basically, that's all.

Once you install this package, the output of the dotnet publish command will make the wwwroot/index.html load *.wasm.br files! 🎉

[!IMPORTANT] If the Blazor WebAssembly app is hosted on a server that supports Brotli compression, you don't have to use this package. For example, the ASP.NET Core server of .NET9 will handle them well (see also here). This package is only useful for an app hosted on a server that doesn't support Brotli compression, such as a simple static content server, such as GitHub Pages.

[!NOTE] In fact, you can implement the feature of loading Brotli pre-compressed files by yourself without depending on this package. Microsoft's official document tells us how to do it. But it can be hard work, particularly if you are implementing a PWA. This NuGet package allows us to use pre-compression application files out of the box!

[!NOTE] For years, the feature of loading Brotli pre-compressed files has been built into the "PublishSPAforGitHubPages.Build" package. However, since it is strongly tied to publishing on GitHub Pages, it can not be used on other platforms. So, I decided to split this feature into an individual package.

🛜 PWAs and Service Workers

If your Blazor WebAssembly PWA loading Brotli pre-compressed files needs to run offline, please ensure the service worker loads "*.br" files in the offline cache. For example, if you generated your Blazor WebAssmebly PWA project by the standard project template, you will need to update the wwwroot/service-worker-published.js file. In this case, you need to append the /\.br$/ regex pattern to the definition of the offlineAssetsInclude array constant variable in the wwwroot/service-worker-published.js file.

// 🗒️ wwwroot/service-worker-published.js
...
const offlineAssetsInclude = [ 
  /\.dll$/, /\.pdb$/, /\.wasm/, /\.html/, /\.js$/, /\.json$/, /\.css$/, /\.woff$/, /\.png$/, /\.jpe?g$/, /\.gif$/, /\.ico$/,
  /\.br$/ // 👈 Add this line
];

⚙️Configurations

If you don't want to enable the Brotli pre-compressed file loading feature, set the BrotliLoaderInjectLoader MSBuild property to false. For example, you can do it in the dotnet publish command as below.

dotnet publish -p BrotliLoaderInjectLoader=false

🎉Release notes

Release notes

📢License

Mozilla Public License Version 2.0

Badges
Extracted from project README's
NuGet Package Discord
Related Projects