The Power of Blazor Hybrid: Rendering Modes, Bridging WASM and Server
Author: Lance Wright (Magic Coding Man)
Published: October 11, 2024
As a longtime Blazor enthusiast, I’ve worked extensively with Blazor, both as a hobbyist and professionally. Through my experiences, I’ve learned the strengths and limitations of the Blazor platform. With the arrival of .NET 8 and the Blazor Hybrid model, Blazor has evolved into something more powerful, simplifying the choice between Blazor WASM and Blazor Server.
Before Blazor Hybrid, developers had two primary options: Blazor Server or Blazor WASM. I favored Blazor WASM for its power and flexibility, but it came with a drawback—long load times due to client-side downloading. Blazor Server, on the other hand, offered quick initial load times but suffered from latency and potential performance issues as the application scaled, making it less ideal for highly interactive apps.
Understanding the New Blazor Hybrid Model
With Blazor Hybrid, developers now have the ability to combine the strengths of Blazor Server and WASM in ways that weren’t possible before. This new platform allows us to overcome many of the limitations of either approach while still retaining their best features.
Blazor Hybrid didn't eliminate the need for Blazor WASM, particularly when building static or Progressive Web Apps (PWA). However, it offers the option to leverage both WASM and Server interactivity where needed, making it the most flexible option yet.
Exploring Render Modes
Blazor Hybrid introduces several render modes that developers can choose based on their application’s needs. The default is pre-rendered, which means the page renders on the server before any interactivity occurs. Other modes include:
- InteractiveServer: Keeps the interaction on the server, ideal for real-time data changes and security-sensitive operations, but less optimal for heavy rendering tasks.
- InteractiveWebAssembly: Runs the interaction in the browser, perfect for scenarios like data-heavy tables or client-side interactivity without burdening the server.
- InteractiveAuto: The best of both worlds—starts as a server-rendered page and switches to WASM once it’s fully loaded. This mode reduces initial load times while offloading complex interactions to the client. Auto is likely to be the most commonly used option.
Recommended Use Cases
Each mode has its use cases. For example, use InteractiveWebAssembly for complex tables to reduce server strain and improve responsiveness. Conversely, InteractiveServer might be best for sensitive, real-time data interactions that require server-side processing.
InteractiveAuto is a great default because it dynamically chooses between server and client-side processing. However, if your application is purely static or needs quick load times, you might still opt for Blazor WASM.
Stream Rendering in Blazor
We won’t cover stream rendering in detail, but it’s worth checking out the Weather.razor page in your project. Stream rendering allows components to render progressively as data becomes available, making it crucial for scenarios involving large datasets or slow API calls.
Setting Up Your Blazor Hybrid Project
When creating a Blazor Hybrid project in Visual Studio, you’ll notice two projects: a server project and a client project. The server project handles server-rendered components, while the client project focuses on WebAssembly. I suggest moving shared components like the NavMenu to the client project in a "Share" folder.
Add this line in the `_Imports.razor` file in your server project to reference the shared folder:
@using {Your_Project_Name}.Client.Share
Optimizing Your Application with InteractiveAuto
By default, Blazor’s template uses pre-rendering for components like the NavMenu. I recommend changing this to `InteractiveAuto` to balance initial load times with dynamic interactivity. Here’s how to modify the NavMenu:
@rendermode InteractiveAuto
This adjustment ensures that dropdowns and other dynamic elements in the menu work correctly without causing server load each time a page is navigated. For any top-of-the-page components, I suggest a similar approach to keep your app performant.
Final Thoughts on Blazor Hybrid
Blazor Hybrid brings a new era of web development, giving developers the power to mix and match server-side and client-side processing in ways that have never been possible before. By combining Blazor Server and Blazor WASM, we now have full control over how our pages render, making Blazor Hybrid the most versatile web development platform available today.
Whether you’re building static apps, PWAs, or complex, real-time applications, Blazor Hybrid offers flexibility and performance that no other platform can match. I hope this article has given you a glimpse into the future of web development with Blazor.