-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fix Blazor root component state persistence across render modes #62370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…tence improvements Co-authored-by: javiercn <[email protected]>
Co-authored-by: javiercn <[email protected]>
…tence Co-authored-by: javiercn <[email protected]>
Copilot
AI
changed the title
[WIP] [Blazor] Support state persistence on root components
Fix Blazor root component state persistence across render modes
Jun 17, 2025
Copilot finished work on behalf of
javiercn
June 17, 2025 11:39
|
/azp run Build Analysis |
|
No pipelines are associated with this pull request. |
ilonatommy
approved these changes
Jun 18, 2025
src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.Prerendering.cs
Outdated
Show resolved
Hide resolved
src/Components/Endpoints/src/Rendering/SSRRenderModeBoundary.cs
Outdated
Show resolved
Hide resolved
Address feedback Co-authored-by: Ilona Tomkowicz <[email protected]>
|
/backport to release/10.0-preview6 |
|
Started backporting to release/10.0-preview6: https://github.com/dotnet/aspnetcore/actions/runs/15737443923 |
Closed
11 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes state persistence for Blazor root components where the
@keydirective information was lost when transitioning between prerendering and interactive modes, causing different persistence keys to be generated.Problem
The issue occurred because
GetSerializableKeyinSupplyParameterFromPersistentComponentStateValueProviderproduced different values when rendering statically vs. interactively. During prerendering, the parent component hierarchy differed from interactive mode, leading to inconsistent key generation and failed state persistence for root components.Solution
Core Changes
Added
ComponentState.GetComponentKey()- New protected virtual method that extracts@keydirectives from the parent component's render tree, providing a consistent interface across render modes.Enhanced
EndpointComponentState- OverridesGetComponentKey()to detectSSRRenderModeBoundarycomponents and return theComponentMarkerKeyfor consistent root component identification.Specialized ComponentState subclasses - Created
WebAssemblyComponentStateandRemoteComponentStatethat acceptComponentMarkerKeyparameters and return them fromGetComponentKey()when available.Improved key computation - Moved
ComputeKeylogic toComponentStatewith enhanced parent type filtering that excludesSSRRenderModeBoundarycomponents from the hierarchy calculation.SSRRenderModeBoundary helper - Added
GetComponentMarkerKey()method to expose the internal marker key for state persistence.Key Algorithm
The solution ensures consistent key generation by:
ComputeKeyalgorithm across all render modesComponentMarkerKeyconsistently for root componentsSSRRenderModeBoundaryfrom parent type calculationsExample
Before this fix, a root component with state persistence would fail:
The
countvalue would be lost during prerender-to-interactive transitions because different keys were generated in each mode. With this fix, the same key is used consistently, preserving the state.Testing
ComponentState.ComputeKey()APIComponentMarkerKeyintegrationFixes #62331.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.