Repository navigation
Closed
Description
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
Metadata
Metadata
Assignees
Labels
area-blazorIncludes: Blazor, Razor Components
Activity
MihuBot commentedon Aug 5, 2025
Possible related and/or duplicate issues:
javiercn commentedon Aug 5, 2025
@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.
javiercn commentedon Aug 5, 2025
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
soundaranbu commentedon Aug 5, 2025
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.
javiercn commentedon Aug 6, 2025
@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.
soundaranbu commentedon Aug 6, 2025
@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?
javiercn commentedon Aug 8, 2025
@soundaranbu I'd check the
dotnet/sdkrepo 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.soundaranbu commentedon Aug 8, 2025
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& alsofalse. The behavior is still the same as described above. I tried to create a new component and use a@keyattribute but that also has no effect either. Please correct me if I'm approaching this wronglyHome.razor
CounterWithKey.razor
What I don't understand is
AllowUpdates = trueand adding@keyattribute ?In any of these cases, why adding a small delay changes the behavior? Please correct me otherwise if I'm missing something
javiercn commentedon Aug 8, 2025
@soundaranbu can you push the project as a public github repository?
soundaranbu commentedon Aug 8, 2025
Here you go https://github.com/soundaranbu/BlazorPersistentState-63129 @javiercn
javiercn commentedon Aug 8, 2025
@soundaranbu thanks for the additional details
After fiddling with it a bit, the issue is likely that https://github.com/dotnet/aspnetcore/blob/main/src/Components/Components/src/PersistentState/PersistentValueProviderComponentSubscription.cs#L148 needs to be outside of the if clause.
javiercn commentedon Aug 15, 2025
Fixed in #63194