May 2020 marks the long-awaited General Availability of Blazor WebAssembly. This release allows C# developers to build Single Page Applications (SPAs) that run entirely in the browser using standard web technologies—no plugins required. We explore the trimming (linker) improvements and the architecture of a production-ready Blazor Wasm app.
The Download Size Challenge
Running .NET in the browser requires shipping the runtime. The IL Linker (Trace-based tree shaking) is crucial.
- **Debug Build**: ~10MB (Includes full symbols and assemblies).
- **Release Build**: ~1.8MB (Unused code stripped out, Brotli compressed).
<PropertyGroup>
<BlazorEnableCompression>true</BlazorEnableCompression>
<!-- Aggressive trimming -->
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
Lazy Loading Assemblies
For large apps, verify the route before downloading the DLL.
<ItemGroup>
<BlazorWebAssemblyLazyLoad Include="AdminDashboard.dll" />
</ItemGroup>
@inject LazyAssemblyLoader AssemblyLoader
@code {
protected override async Task OnNavigateAsync(NavigationContext context)
{
if (context.Path == "admin")
{
await AssemblyLoader.LoadAssembliesAsync(new[] { "AdminDashboard.dll" });
}
}
}
Key Takeaways
- Release builds are mandatory for performance testing.
- Lazy Loading significantly improves Time to Interactive (TTI).
- Use PWA capabilities to cache the runtime for repeat visits.
Discover more from C4: Container, Code, Cloud & Context
Subscribe to get the latest posts sent to your email.