Skip to content

PersistentState attribute inconsistent behavior on page navigation #63129

Closed

Description

@soundaranbu

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Scenario 1: Persistent state does not restore after page navigation

  • Create a new Blazor Web App with Interactive Render mode with prerendering enabled(default)
  • Update the counter component as below
@page "/counter"
@rendermode InteractiveServer

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
+    [PersistentState]
+    public int currentCount { get; set; }

+    protected override async Task OnInitializedAsync()
+    {
+       if (!RendererInfo.IsInteractive)
+       {
+           currentCount = 10;
+       }
+    }

    private void IncrementCount()
    {
        currentCount++;
    }
}
  • Launch the application and go to the counter page. It correctly shows 10
  • Now, go back to the home page and come back to the counter page. The count flickers(unnoticable in the recording) and goes back to 0. When I debugged the component, first the component was server rendered and then rendered again in interactive mode on page navigation. But the persistent value was not restored
20250805-1059-20.9857512.mp4

Scenario 2: State restores after page navigation when added a small 1 second delay

  • Now, add a small delay (1 second) before and after setting the counter value
@code {
    [PersistentState]
    public int currentCount { get; set; } = 5;

    protected override async Task OnInitializedAsync()
    {
        if (!RendererInfo.IsInteractive)
        {
+          await Task.Delay(1000);
            currentCount = 10;
+          await Task.Delay(1000);
        }
    }

    private void IncrementCount()
    {
        currentCount++;
    }
}
  • Now, the same state is restored after page navigation
20250805-1111-05.8921160.mp4

This presents inconsistent behavior. My another question was - is it supposed to prerender on page navigation?

Expected Behavior

  • Persistent state should be restored consistently on page navigation

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

10.0.100-rc.1.25401.103

Anything else?

No response

Activity

  1. added
    area-blazor
    on Aug 5, 2025
  2. javiercn commented on Aug 5, 2025

    @javiercn
    Member

    @soundaranbu thanks for contacting us.

    Can you please provide your repro project as a public github repository so that we can take a look at it? There are many details that matter missing from your repro steps.

  3. javiercn commented on Aug 5, 2025

    @javiercn
    Member

    I would go ahead and say that if you are trying to use enhanced nav (as the sample suggests), it has likely not yet made it into a released build, since it got merged yesterday. Also to receive updates during enhanced navigation you need to use AllowUpdates.

    Finally, the component doesn't have a key specified, so it will get destroyed and re-created on every navigation

  4. added
    Needs: Author Feedback
    on Aug 5, 2025
  5. soundaranbu commented on Aug 5, 2025

    @soundaranbu
    Author

    Hey @javiercn, please find the repro project here BlazorPersistentState-63129.zip. This is a per page interactive server render mode project created from the Blazor Web template. The only change is the counter component like I described above. I tested this using the latest sdk version 10.0.100-rc.1.25401.103 downloaded today from the daily feed. However, I'm not sure if this comes under Enhanced Navigation. But let me know if that's the case, I'm happy to test this again later.

  6. added
    Needs: Attention 👋
    and removed
    Needs: Author Feedback
    on Aug 5, 2025
  7. added this to the 10.0-rc1 milestone on Aug 5, 2025
  8. javiercn commented on Aug 6, 2025

    @javiercn
    Member

    @soundaranbu yes it does, and the build that you are using is unlikely to have the support for it.

    You won't get state updates from the server unless you opt-in with allow updates during enhanced nav, and if your component doesn't have a key it will be destroyed and re-created on each enhanced nav.

  9. soundaranbu commented on Aug 6, 2025

    @soundaranbu
    Author

    @javiercn, has the change landed in today's build? let me give it a try again. Between, how do I specify the key for the component with page directive?

  10. javiercn commented on Aug 8, 2025

    @javiercn
    Member

    @soundaranbu I'd check the dotnet/sdk repo to see the commits for each repo and then see what changes are up to that commit. I suspect it'll take a few days for a build to be produced.

  11. added
    Needs: Author Feedback
    and removed
    Needs: Attention 👋
    on Aug 8, 2025
  12. soundaranbu commented on Aug 8, 2025

    @soundaranbu
    Author

    Hey @javiercn, I tried today's build (10.0.100-rc.1.25407.106) which appears to have the enhanced nav persistent state feature.

    I tried both AllowUpdates = true & also false. The behavior is still the same as described above. I tried to create a new component and use a @key attribute but that also has no effect either. Please correct me if I'm approaching this wrongly

    Home.razor

    @page "/"
    
    <CounterWithKey @key="@("counterKey")" />

    CounterWithKey.razor

    @rendermode InteractiveServer
    
    <p role="status">Current count: @currentCount</p>
    
    <button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
    
    @code {
        [PersistentState(AllowUpdates = true)]
        public int currentCount { get; set; } = 5;
    
    
        protected override async Task OnInitializedAsync()
        {
            if (!RendererInfo.IsInteractive)
            {
                currentCount = 10;
            }
        }
    
        private void IncrementCount()
        {
            currentCount++;
        }
    }

    What I don't understand is

    • Is the PersistentState supposed to only work on page load/refresh? If I navigate between pages, is it supposed to persist and restore the state?
      • If the former is true, why does it restore the state on page navigation after adding a small delay? (refer to Scenario 2 in the description)
      • If the latter is true, why does it not restore the state even after setting AllowUpdates = true and adding @key attribute ?

    In any of these cases, why adding a small delay changes the behavior? Please correct me otherwise if I'm missing something

  13. added
    Needs: Attention 👋
    and removed
    Needs: Author Feedback
    on Aug 8, 2025
  14. javiercn commented on Aug 8, 2025

    @javiercn
    Member

    @soundaranbu can you push the project as a public github repository?

  15. added
    Needs: Author Feedback
    and removed
    Needs: Attention 👋
    on Aug 8, 2025
  16. added
    Needs: Attention 👋
    and removed
    Needs: Author Feedback
    on Aug 8, 2025
  17. javiercn commented on Aug 8, 2025

    @javiercn
    Member
  18. javiercn commented on Aug 15, 2025

    @javiercn
    Member

    Fixed in #63194

  19. removed
    Needs: Attention 👋
    on Aug 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-blazor

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions