-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Allow JS root components to reinitialize on circuit restart #64536
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
base: main
Are you sure you want to change the base?
Conversation
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 enables JS root components to reinitialize when a Blazor circuit restarts, while still preventing multi-host scenarios. Previously, any attempt to re-enable JS root components would throw an error, even for valid circuit restart scenarios.
Key Changes:
- Modified JS root component initialization logic to differentiate between circuit restarts (same renderer type) and multi-host scenarios (different renderer types)
- Consolidated test coverage by moving dynamic JS root component tests into the main state persistence test suite
- Removed configuration-based component registration in favor of always registering the test component
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/Components/Web.JS/src/Rendering/JSRootComponents.ts |
Added currentRendererId and hasInitializedJsComponents tracking to support circuit restart while preventing multi-host scenarios |
src/Components/Web.JS/src/Rendering/WebRendererInteropMethods.ts |
Updated enableJSRootComponents call to include rendererId parameter |
src/Components/test/E2ETest/Tests/StatePersistenceTest.cs |
Added test for persistent state support in dynamic JS roots |
src/Components/test/E2ETest/Tests/StatePersistanceJSRootTest.cs |
Removed separate test file (consolidated into StatePersistenceTest.cs) |
src/Components/test/testassets/Components.TestServer/RazorComponentEndpointsStartup.cs |
Simplified to always register dynamic JS root component instead of using configuration |
src/Components/test/testassets/Components.TestServer/Program.cs |
Removed separate test scenario for JS root components |
| if (!hasInitializedJsComponents) { | ||
| // Call the registered initializers. This is an arbitrary subset of the JS component types that are registered | ||
| // on the .NET side - just those of them that require some JS-side initialization (e.g., to register them | ||
| // as custom elements). | ||
| for (const [initializerIdentifier, componentIdentifiers] of Object.entries(jsComponentInitializers)) { | ||
| const initializerFunc = DotNet.findJSFunction(initializerIdentifier, 0) as JSComponentInitializerCallback; | ||
| for (const componentIdentifier of componentIdentifiers) { | ||
| const parameters = jsComponentParameters[componentIdentifier]; | ||
| initializerFunc(componentIdentifier, parameters); | ||
| } | ||
| } | ||
|
|
||
| hasInitializedJsComponents = true; |
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.
I think there is potentially more to this than just fixing this error. Try and trigger a pause and a resume operation manually. There are likely other things that fail? (happy to be wrong through)
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.
Update: the disconnection test should be done on JS root component.
Fixes #64523.
Tests cleanup:
This PR reverts changes to tests architecture done in #64159 to avoid fixing multiple issues in one PR. It removes the additional test file and moves
PersistentStateIsSupportedInDynamicJSRootstoPersistentState-connected test file.Framework fixes:
The goal of the changes is to differentiate between the following scenarios:
Multi-Host / Different Renderer Types (Unsupported - Throws Error)
rendererId= 0)enableJSRootComponents: With Server'srendererIdandmanagerInstancemanageris set,currentRendererId = 0enableJSRootComponents: With WebAssembly'srendererId(e.g., 1) and its ownmanagerInstance.Same Renderer Type Circuit Restart (Supported)
managervariable holds the oldDotNetObjectReferencefrom Circuit 1.currentRendererIdholds the renderer ID (e.g., Server renderer = 0)enableJSRootComponents: The framework calls this with:rendererId](e.g., 0 for Server)managerInstance(a fresh DotNetObjectReference from Circuit 2)managerInstancereplaces the old stale onehasInitializedJsComponentsis already true, so initializers don't run again.Blazor.rootComponents.add()with the new valid manager.