-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Fix Blazor persistent component state restoration for components without keys and add E2E test coverage #63194
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
Conversation
src/Components/Components/test/PersistentValueProviderComponentSubscriptionTests.cs
Outdated
Show resolved
Hide resolved
We should look at the E2E test cases in #62824 to add also support for these other two scenarios. Specifically src/Components/test/E2ETest/Tests/StatePersistenceTest.cs and src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/PersistentState/PageWithDeclarativeEnhancedNavigationPersistentComponents.razor Our test could be designed to use a query string to choose to render or not render the components. There would be one component without a key that renders always and a component that gets added/removed depending on the value of the query string. In both cases the state should be restored successfully |
I've added E2E test support for the two scenarios you mentioned:
The page follows the same pattern as the existing declarative enhanced navigation page, with links in PageWithoutComponents.razor for navigation. Both scenarios should validate that persistent state restoration works correctly regardless of when components are destroyed and recreated. Implemented in 6b82560. |
Co-authored-by: javiercn <[email protected]>
Co-authored-by: javiercn <[email protected]>
…ion scenarios Co-authored-by: javiercn <[email protected]>
…tent state restoration Co-authored-by: javiercn <[email protected]>
db2a683
to
eea088a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a bug in Blazor's persistent component state restoration where restored values were being ignored for components without @key
attributes or during navigation scenarios. The core issue was that the _ignoreComponentPropertyValue
flag was only being set conditionally within notification blocks, causing restored values to be overridden by component property values when notifications were skipped.
Key changes:
- Moved the
_ignoreComponentPropertyValue = true
assignment outside conditional notification blocks to ensure it's always set when state is restored - Added comprehensive E2E test infrastructure with a new test page for conditional component rendering scenarios
- Enhanced existing unit tests to better validate the flag behavior
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
PersistentValueProviderComponentSubscription.cs |
Core fix - moves the _ignoreComponentPropertyValue = true assignment outside conditional blocks |
PageWithConditionalPersistentComponents.razor |
New test page supporting conditional component rendering scenarios |
PageWithoutComponents.razor |
Updated navigation links and added support for new test scenarios |
PageWithDeclarativeEnhancedNavigationPersistentComponents.razor |
Enhanced to support components with and without @key attributes |
StatePersistenceTest.cs |
Updated existing E2E tests to support new test parameters |
PersistentValueProviderComponentSubscriptionTests.cs |
Enhanced unit tests with better validation of state restoration behavior |
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
...stServer/RazorComponents/Pages/PersistentState/PageWithConditionalPersistentComponents.razor
Outdated
Show resolved
Hide resolved
...stServer/RazorComponents/Pages/PersistentState/PageWithConditionalPersistentComponents.razor
Outdated
Show resolved
Hide resolved
...sets/Components.TestServer/RazorComponents/Pages/PersistentState/PageWithoutComponents.razor
Outdated
Show resolved
Hide resolved
…onents/Pages/PersistentState/PageWithoutComponents.razor
The
_ignoreComponentPropertyValue
flag inPersistentValueProviderComponentSubscription
was only being set conditionally withinif (!skipNotifications)
blocks, causing restored values to be ignored when components are destroyed and re-added during navigation or when components don't use@key
.Problem
When a component is added, removed, then added again (for example, during page navigation or when a component doesn't use
@key
), the restored value from persistent state storage was being ignored. This happened because:skipNotifications
is oftentrue
_ignoreComponentPropertyValue = true
assignment was inside theif (!skipNotifications)
blockGetOrComputeLastValue()
would return the component's current property value instead of the restored valueSolution
Moved
_ignoreComponentPropertyValue = true;
outside the conditional blocks so it's set unconditionally whenever we successfully restore from persistent state. This ensures that the restored value always takes precedence over the component's initial property value, regardless of notification settings.Before:
After:
Test Coverage
Added comprehensive test coverage including:
Unit Tests
_ignoreComponentPropertyValue
flag behaviorE2E Tests
Following feedback from @javiercn, added E2E test infrastructure for two critical scenarios:
PageWithConditionalPersistentComponents.razor - New test page supporting:
StatePersistenceTest.cs - New E2E tests covering:
CanRestoreStateForComponentsWithoutKeysAndConditionalRendering
- Validates both components restore state correctlyCanRestoreStateAfterConditionalComponentToggling
- Tests state restoration after component togglingThe E2E tests use query string parameters to control component rendering and validate that persistent state works correctly in all scenarios where components are dynamically added/removed, particularly during enhanced navigation in Blazor applications.
Fixes #63193.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.