A .NET library that brings the stale-while-revalidate (SWR) caching strategy to Blazor and ASP.NET Core. Serve cached data instantly while revalidating in the background — the user always sees data fast, and it's always eventually fresh.
- Stale — Return cached data instantly. The UI is never blocked.
- Revalidate — Fetch fresh data in the background.
- Update — Swap in new data. Every subscriber re-renders automatically.
dotnet add package Swr.Net --prereleaseRegister the services in Program.cs:
builder.Services.AddSwrForBlazor();Use ISwr in any Blazor component:
@using Swr.Net
@inject ISwr Swr
@implements IDisposable
@if (result is not null)
{
@if (result.IsLoading)
{
<p>Loading...</p>
}
@if (result.Data is not null)
{
<h2>@result.Data.Name</h2>
}
}
@code {
private SwrResult<User>? result;
protected override async Task OnInitializedAsync()
{
result = await Swr.GetAsync<User>("/api/users/me");
}
public void Dispose() => result?.Dispose();
}Register the services in Program.cs:
builder.Services.AddSwrForAspNetCore();- Stale-While-Revalidate — Instant UI with cached data, fresh data in the background
- Request Deduplication — Concurrent requests for the same key share one network call
- Automatic Retry — Exponential backoff on failure with configurable attempts
- Cache Mutations — Invalidate by key, prefix, or clear all
- Reactive Results —
INotifyPropertyChanged+OnRevalidatedevent for UI binding - Platform-Aware DI — Scoped for Blazor (per-circuit), Singleton for ASP.NET Core
- Pluggable Storage —
ISwrStoreinterface for custom cache backends