-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Open
Copilot
wants to merge
12
commits into
main
Choose a base branch
from
copilot/fix-62331
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+355
−25
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
Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo! |
…tence improvements Co-authored-by: javiercn <[email protected]>
Co-authored-by: javiercn <[email protected]>
…tence Co-authored-by: javiercn <[email protected]>
Copilot finished work on behalf of
javiercn
June 17, 2025 11:39
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
@key
directive information was lost when transitioning between prerendering and interactive modes, causing different persistence keys to be generated.Problem
The issue occurred because
GetSerializableKey
inSupplyParameterFromPersistentComponentStateValueProvider
produced 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@key
directives from the parent component's render tree, providing a consistent interface across render modes.Enhanced
EndpointComponentState
- OverridesGetComponentKey()
to detectSSRRenderModeBoundary
components and return theComponentMarkerKey
for consistent root component identification.Specialized ComponentState subclasses - Created
WebAssemblyComponentState
andRemoteComponentState
that acceptComponentMarkerKey
parameters and return them fromGetComponentKey()
when available.Improved key computation - Moved
ComputeKey
logic toComponentState
with enhanced parent type filtering that excludesSSRRenderModeBoundary
components 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:
ComputeKey
algorithm across all render modesComponentMarkerKey
consistently for root componentsSSRRenderModeBoundary
from parent type calculationsExample
Before this fix, a root component with state persistence would fail:
The
count
value 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()
APIComponentMarkerKey
integrationFixes #62331.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.