Blazor rendering pipeline that uses browser fetch
instead of SignalR
This means that you can:
- Forget about disconnected sessions
- Avoid paying for an expensive server
- Access your backend services without an API
2024-09-23.11-31-47.webm
The project is still in the experimental phase. Very little real-world testing has been done, so please submit any issues you encounter here.
Install the Blazor.LightMode NuGet package:
dotnet add package Blazor.LightMode
Here's a sample application template with the necessary changes applied:
using Blazor.LightMode;
using BlazorApp4.Components;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddLightMode(); // ADD THIS LINE
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>();
.AddInteractiveServerRenderMode(); // Optionally comment out this line
app.UseLightMode(); // ADD THIS LINE
app.Run();
Replace the existing Blazor script reference:
<body>
<Routes/>
<script src="_framework/blazor.server.js"></script> // REMOVE THIS
<script src="_content/Blazor.LightMode/blazor.lightmode.js"></script> // ADD THIS
</body>
You can now build your Blazor Server application as usual. All components and pages will function without persistent WebSocket connections.
Traditional Blazor Server uses SignalR to push render updates to the client. In contrast, LightMode is a traditional request/response model where the response acts as a push.
Blazor Server uses circuits to maintain the session state, and so does LightMode. The difference is that Blazor Server relies on the websocket disconnect event to handle circuit lifetime, while LightMode doesn't know if the client has disconnected. Instead, it utilizes a very simple garbage collection technique to dispose of sessions that are likely unused. If the user switches back to the tab after GC disposes of the session, the server will return a 404 response, prompting the browser to reload the page. You can configure GC values using DefaultLightModeCircuitManager
static properties or implement your own ILightModeCircuitManager
.
Contributions are welcome! If you'd like to contribute, please follow these steps:
- Fork the repo
- Run
git submodule update --init --recursive
- Go to
src\Blazor.LightMode\js\
and runnpm install
- Now open the solution file in the root directory and build
This project is licensed under the MIT License.