AugusteVN.Razor.PersistState 1.1.3

dotnet add package AugusteVN.Razor.PersistState --version 1.1.3
                    
NuGet\Install-Package AugusteVN.Razor.PersistState -Version 1.1.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AugusteVN.Razor.PersistState" Version="1.1.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AugusteVN.Razor.PersistState" Version="1.1.3" />
                    
Directory.Packages.props
<PackageReference Include="AugusteVN.Razor.PersistState" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AugusteVN.Razor.PersistState --version 1.1.3
                    
#r "nuget: AugusteVN.Razor.PersistState, 1.1.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AugusteVN.Razor.PersistState@1.1.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AugusteVN.Razor.PersistState&version=1.1.3
                    
Install as a Cake Addin
#tool nuget:?package=AugusteVN.Razor.PersistState&version=1.1.3
                    
Install as a Cake Tool

Persist Component State

Blazor renders components twice when prerendering is enabled. PersistStateProvider prevents duplicate fetches by restoring JSON state during the interactive render. It also replaces subscriptions for repeated keys, logs persistence failures, and keeps those failures from breaking the circuit.

Usage Example

Inheritance

public class BlogHttpClient : PersistStateProvider
{
    private readonly HttpClient _http;
    public BlogHttpClient(HttpClient http, PersistentComponentState appState) : base(appState)
    {
        _http = http;
    }
    
    public async Task<BlogPostResponse?> FetchSelectedBlogPost(GetBlogPostForSlugRequest request)
    {
        return await FetchAsync<BlogPostResponse?>(
            key: $"{nameof(GetBlogPostForSlug)}/{request.Slug}",
            fetchMethod: () => GetBlogPostForSlug(request),
            reFetchFromMethod: false);
    }

    private async Task<BlogPostResponse?> GetBlogPostForSlug(GetBlogPostForSlugRequest request)
    {
        try
        {
            return await _http.GetFromJsonAsync<BlogPostResponse>($"{ApiRoutes.BlogPosts}/{request.Slug}");
        }
        catch (Exception e)
        {
            _logger.LogWarning("Could not get blog post with slug: {slug}, {@errorMessage}", request.Slug, e.Message);
            return null;
        }
    }
    
}

Composition

builder.Services.AddSingleton<PersistStateProvider>();
public class BlogHttpClient
{
    private readonly HttpClient _http;
    private readonly PersistentComponentState _appState;
    private readonly PersistStateProvider _stateProvider;
    public BlogHttpClient(HttpClient http, PersistentComponentState appState, PersistStateProvider stateProvider)
    {
        _http = http;
        _appState = appState;
        _stateProvider = stateProvider.Create(appState);
    }
    
    public async Task<BlogPostResponse?> FetchSelectedBlogPost(GetBlogPostForSlugRequest request)
    {
        return await _stateProvider.FetchAsync<BlogPostResponse?>(
            key: $"{nameof(GetBlogPostForSlug)}/{request.Slug}",
            fetchMethod: () => GetBlogPostForSlug(request),
            reFetchFromMethod: false);
    }

    private async Task<BlogPostResponse?> GetBlogPostForSlug(GetBlogPostForSlugRequest request)
    {
        try
        {
            return await _http.GetFromJsonAsync<BlogPostResponse>($"{ApiRoutes.BlogPosts}/{request.Slug}");
        }
        catch (Exception e)
        {
            _logger.LogWarning("Could not get blog post with slug: {slug}, {@errorMessage}", request.Slug, e.Message);
            return null;
        }
    }
}

Blazor Component

ExampleComponent.razor

@implements IDisposable
@inject PersistStateProvider PersistState
@inject PersistentComponentState AppState

<p>Count: @Counter</p>

@code {
    private int Counter { get; set; }

    protected override async Task OnInitializedAsync()
    {
        var persistState = PersistState.Create(AppState);
        
        persistState.FetchAsync<int>(
            key: nameof(Counter),
            fetchMethod: () => {},
            reFetchFromMethod: false);
    }

    public void Dispose()
    {
        PersistState.Dispose();
    }
}

To see it in action, watch: Prevent this Blazor Prerendering ISSUE C# .NET 8.

Brought to you by: kiss-code.com.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AugusteVN.Razor.PersistState:

Package Downloads
AugusteVN.Razor.State

Helper classess and components for state management.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.3 155 9/16/2026
1.1.2 110 9/15/2026
1.1.1 136 9/14/2026
1.1.0 599 7/9/2026
1.0.3 399 12/17/2025
1.0.2 286 6/7/2025
1.0.1 191 6/6/2025

Upgraded to .NET 10.