Skip to content

[Interactive Server] State persistent doesn't work with navigation #59569

Closed as duplicate

Description

@ruxo

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Hello,

I've been experimenting with Blazor state persistence using PersistentComponentState and encountered an issue. It works as expected on the first page load, but it fails to persist state on subsequent activations triggered by link navigations. Below is the output and the test code that reproduces the issue.

Test Code:

@page "/"
@rendermode InteractiveServer

@implements IDisposable
@inject PersistentComponentState State

<PageTitle>Home</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.

@code {
    PersistingComponentStateSubscription? ps;

    static int count;

    [CascadingParameter] HttpContext? HttpContext { get; set; }

    int myCount = Interlocked.Increment(ref count);

    protected override void OnInitialized() {
        ps = State.RegisterOnPersisting(Persist);

        var existed = State.TryTakeFromJson<int>("data", out var c);

        Console.WriteLine($"{myCount}. Created with persisted={existed}, c={c}, service={State.GetHashCode()}, and context={HttpContext}");
    }

    Task Persist() {
        State.PersistAsJson("data", myCount);
        return Task.CompletedTask;
    }

    protected override void OnAfterRender(bool firstRender) {
        Console.WriteLine($"{myCount}. OnAfterRender {firstRender}");
    }

    public void Dispose() {
        ps?.Dispose();
    }
}

Output:

1. Created with persisted=False, c=0, service=16484805, and context=Microsoft.AspNetCore.Http.DefaultHttpContext
2. Created with persisted=True, c=1, service=36386200, and context=
2. OnAfterRender True
3. Created with persisted=False, c=0, service=38139429, and context=Microsoft.AspNetCore.Http.DefaultHttpContext
4. Created with persisted=False, c=0, service=36386200, and context=
4. OnAfterRender True

Observations:

  1. Events 1 and 2 correspond to the initial page load:
    • Event 1: Pre-rendering during the first load.
    • Event 2: Interactive rendering after the server connection is established.
  2. Events 3 and 4 occur when navigating away from the page and returning:
    • Persistent state is not retained. State.TryTakeFromJson returns false, and the persisted data is not retrieved.

Issue:

Based on the above, it appears that PersistentComponentState does not function correctly for subsequent page activations. Is this a bug or a limitation of the current implementation? Any guidance or confirmation would be appreciated

Environment

.NET SDK 9.0.101

Expected Behavior

Event 4 should have persisted=True since event 3 is a pre-rendering step.

Steps To Reproduce

  1. Create a new blazor project with dotnet new blazor -o Test
  2. Modify Home.razor using the code provided above.
  3. Run the project.

Exceptions (if any)

No response

.NET Version

9.0.101

Anything else?

.NET SDK:
 Version:           9.0.101
 Commit:            eedb237549
 Workload version:  9.0.100-manifests.054dcb45
 MSBuild version:   17.12.12+1cce77968

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.26100
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\9.0.101\

.NET workloads installed:
 [wasm-tools]
   Installation Source: SDK 9.0.100
   Manifest Version:    9.0.0/9.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\9.0.100\microsoft.net.workload.mono.toolchain.current\9.0.0\WorkloadManifest.json
   Install Type:              Msi

Configured to use loose manifests when installing new manifests.

Host:
  Version:      9.0.0
  Architecture: x64
  Commit:       9d5a6a9aa4

.NET SDKs installed:
  8.0.404 [C:\Program Files\dotnet\sdk]
  9.0.100 [C:\Program Files\dotnet\sdk]
  9.0.101 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Activity

  1. ghost added
    area-blazor
    on Dec 19, 2024
  2. ruxo commented on Dec 20, 2024

    @ruxo
    Author

    Not sure if I should create another ticket, but I hope it's okay to post here as this is closely related to the original issue.

    After further experimentation, I found that pre-rendering is actually broken. If I render myCount on the page, for example:

    <PageTitle>Home</PageTitle>
    
    <h1>Hello, world!</h1>
    <p>Count = @myCount</p> <!-- new -->
    
    Welcome to your new app.
    

    Every time I navigate back and forth between pages, the count increases by 2. However, I noticed that repeatedly navigating to the same page (e.g., clicking the "Home" menu item multiple times) only triggers a pre-rendering call, but it does not update the page.

    I can see the count increasing in the console log, but there are no visible changes on the screen (home page) unless I navigate to another page and return. This behavior suggests that the extra pre-rendering calls are wasteful because they don't actually update the UI.

    I believe this should be considered a bug.

  3. javiercn commented on Dec 20, 2024

    @javiercn
    Member

    @ruxo thanks for contacting us.

    This is a dupe of #51584

  4. added
    ✔️ Resolution: Duplicate
    on Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: DuplicateStatus: Resolvedarea-blazor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions