Skip to content

[PersistentState] not working as expected (or an issue with enhanced navigation?) #64384

Closed as duplicate

Description

@Stuart88

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Sorry I don't know if this is a bug, or my own misunderstanding of how [PersistentState] is supposed to work.

I'm trying to persist state for an injected service.

I've followed the docs, I've followed the few discussions I can find online, but the state still doesn't seem to persist as I might expect.

It's a Blazor web app, InteractiveAuto, set to per page/component.

The service is set up as a singleton on the server and client projects, and is registered for persistence in the server project

// Server

builder.Services.AddSingleton<IMyService, MyService>();

builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents()
    .AddInteractiveWebAssemblyComponents()
    .RegisterPersistentService<IMyService>(RenderMode.InteractiveWebAssembly);

// Client

builder.Services.AddSingleton<IMyService, MyService>();

I've tried all combinations of [PersistentState], I've tried with/without a key on the component placement, but always there is a UI flicker between pre-render state and wasm state.

I'm sure I've just misunderstood it, or there is perhaps a limitation to what can be done in terms of typical user expectations.

What I want / hoped for with [PersistentState], is that it resolves page flicker when transferring to wasm, and from that point forward the state is handled client side.

However, I find that there is still always the initial (first ever) state for a moment before it settles back into the state the the user put it in.

See the attached video. The service is set (and successfully maintains) a state from pre-render, but when wasm is ready it has a different (maintained) state.

In the video, in OnInitialized the values of MyService.MyNumbers_True_Default etc are set only once in, on the first ever render. After that, the values are always non-null, so page navigation skips the ??= checks, instead using the (mismatched) values of the services that seem to be stored (but different) between pre-render and wasm.

persistentState.mp4

Expected Behavior

After wasm is ready, I would expect the pre-render work to not care about the state that the service needs to be in, because the client is now handling it.

Steps To Reproduce

Repro: https://github.com/Stuart88/TestPersistentState

Run app.

Note initial values of each item in bulletpoints.

Click "Change the numbers"

Place breakpoint at start of OnInitialized of MyComponent.razor

Navigate away from page, then navigate back.

  • On first time breakpoint is hit (during pre-render), click continue in debug.
  • On second time breakpoint is hit (wasm ready), inspect values of numbers in the UI (it's showing the pre-render values).
  • Click continue in debug again.

Notice the numbers change in the UI, to show the values that were held in the wasm version of the service

.NET Version

10

Anything else?

No response

Activity

  1. added
    area-blazor
    on Nov 16, 2025
  2. Stuart88 commented on Nov 16, 2025

    @Stuart88
    Author

    Ok never mind, just seen it was reported last month!

    here

    #63895

    and here

    #63996

    [PersistentState] is one of the best improvements for me personally in .NET 10, I would love to see this issue sorted as it would make the SSR->wasm transfer perfectly seamless, the SPA dev's dream come true.

  3. javiercn commented on Nov 16, 2025

    @javiercn
    Member

    @Stuart88 thanks for contacting us.

    We'll take a look to better understand what the issue here is, but we are going to use #63895 to track the investigation.

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

    area-blazor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions