Blazor Integration
Swr.Net works with both Blazor Server and Blazor WebAssembly. AddSwrForBlazor() registers ISwr as Scoped, meaning each Blazor circuit gets its own independent cache — user data never leaks between sessions.
builder.Services.AddSwrForBlazor();Optionally configure global defaults:
builder.Services.AddSwrForBlazor(options =>{ options.StaleTime = TimeSpan.FromSeconds(30); options.CacheTime = TimeSpan.FromMinutes(5);});Fetching Data
Section titled “Fetching Data”Use Use<T> in OnInitialized with a fetcher and onChange callback to load data without blocking the page render:
@using System.Net.Http.Json@using Swr.Net@inject ISwr Swr@inject HttpClient Http
@if (result?.Data is not null){ <h2>@result.Data.Summary</h2> <p>Temperature: @result.Data.TemperatureC °C</p>}else if (result is null or { IsLoading: true }){ <p>Loading...</p>}else if (result?.Error is not null){ <p class="error">@result.Error</p>}
@code { private SwrResult<WeatherForecast>? result;
protected override void OnInitialized() { result = Swr.Use<WeatherForecast>("/api/weather", async key => await Http.GetFromJsonAsync<WeatherForecast>(key), onChange: () => InvokeAsync(StateHasChanged)); }}Use<T>returns immediately — if cached data exists, it’s available right away; otherwiseIsLoadingistrue- The
onChangecallback fires when the fetch or background revalidation completes, triggering a re-render with fresh data - The first argument is the cache key, the second is the fetcher function that knows how to retrieve the data
- Data is checked before the loading state so that cached data renders instantly, even while revalidating
Loading and Error States
Section titled “Loading and Error States”A SwrResult<T> can be in three states:
IsLoading == true— a network request is in progressError is not null— the fetch failed; checkErrorfor the messageData is not null— data is available (may be fresh or from cache)
@if (result?.Data is not null){ <h2>@result.Data.Name</h2> @if (result.IsFromCache) { <span class="badge">Refreshing...</span> }}Per-Request Options
Section titled “Per-Request Options”Override global options for a specific fetch:
var options = new SwrOptions{ StaleTime = TimeSpan.FromSeconds(10), CacheTime = TimeSpan.FromMinutes(1), RetryCount = 5};result = Swr.Use<Dashboard>("/api/dashboard", async key => await Http.GetFromJsonAsync<Dashboard>(key), options: options, onChange: () => InvokeAsync(StateHasChanged));Per-request options take precedence over the global options configured in Program.cs.
Mutations
Section titled “Mutations”MutateAsync
Section titled “MutateAsync”Use MutateAsync to execute a mutation and invalidate related cache entries:
// Execute a mutation and invalidate related cache entriesawait Swr.MutateAsync( async () => await Http.PutAsJsonAsync($"/api/users/{userId}", updates), "/api/users/me", "/api/users");If the mutation throws, no cache keys are evicted — the cache stays consistent.
Manual Invalidation
Section titled “Manual Invalidation”Force a cache refresh without executing a mutation:
Swr.Invalidate("/api/users/me"); // Remove one exact keySwr.InvalidateByPrefix("/api/users"); // Remove all keys starting with prefixSwr.InvalidateAll(); // Clear the entire cacheNext Steps
Section titled “Next Steps”- Getting Started — Installation and first component
- ASP.NET Core Integration — Singleton cache for server-side usage
- API Reference — Complete documentation for all public types