Course contentsShow
← C#
Lesson 1407 of 1,90812. Modern Frontend with Razor and BlazorFree lesson

WebAssembly Bootstrapping

Configure the loading sequence of Blazor Wasm to improve time-to-interactive metrics.

In this lesson, you will learn how to optimize the Blazor WebAssembly bootstrapping process to significantly improve your application's Time-to-Interactive (TTI) metrics.

Understanding the Blazor Bootstrapping Lifecycle

When a user visits a Blazor Wasm app, the browser must download the blazor.webassembly.js file, fetch the .NET runtime, download your application assemblies, and finally initialize the WebAssembly environment. By default, this is a "monolithic" load—the browser waits for the core runtime and all dependencies before rendering the UI.

To improve TTI, we can manipulate the blazor.webassembly.js bootstrapping sequence. Instead of letting the framework load everything immediately, we can use the autostart="false" attribute on the script tag to take control of the lifecycle.

Manual Bootstrapping Implementation

By setting autostart="false", you prevent Blazor from running automatically, allowing you to prioritize the critical path (like showing a splash screen or pre-fetching essential assets) before invoking the boot process manually:

<script src="_framework/blazor.webassembly.js" autostart="false"></script>
<script>
  // Perform your custom logic here (e.g., showing a custom loader)
  document.getElementById('app').innerHTML = "Initializing engine...";

  Blazor.start().then(() => {
    console.log("Blazor runtime loaded and ready!");
  });
</script>

Optimizing the Loading Sequence

Once you have control via Blazor.start(), you can refine the loading behavior to improve TTI:

  1. Lazy Loading Assemblies: Identify non-critical assemblies (like large administrative dashboards) and move them out of the initial boot sequence. You can fetch these only when the user navigates to the specific route, reducing the initial payload size.
  2. Resource Prioritization: Use <link rel="preload"> tags in your index.html to tell the browser to prioritize the dotnet.wasm runtime file and your primary assembly before secondary assets.
  3. Deferring Non-essential Services: If you have heavy logging or telemetry services that initialize during Program.cs, consider wrapping them in a delayed task that executes only after the initial render cycle has completed.

By breaking the "all-at-once" initialization, you ensure that the browser is busy parsing and rendering meaningful UI rather than waiting for background infrastructure to finish.

Key Takeaway: By disabling autostart and managing the Blazor.start() sequence, you can effectively sequence your resource delivery to ensure the UI becomes interactive as soon as the minimal required runtime is ready.