Fix Blazor crash when interactive component is in layout with enhanced nav#66007
Draft
Fix Blazor crash when interactive component is in layout with enhanced nav#66007
Conversation
22a336b to
b530b67
Compare
…d nav When an InteractiveAuto/InteractiveWebAssembly component is placed in the layout, navigating between pages via enhanced navigation causes: TypeError: Cannot read properties of null (reading insertBefore) Root cause: The Blazor-WebAssembly metadata comment was removed from the DOM during discoverWebAssemblyOptions() but remained in the logical children array as an orphaned node. On subsequent enhanced navigations, insertLogicalChild would pick this orphaned comment as a reference node for insertBefore, crashing because parentNode was null. Fix: 1. Reorder registerAllComponentDescriptors to call discoverWebAssemblyOptions before upgradeComponentCommentsToLogicalRootComments, ensuring metadata comments are removed before the logical tree is built. 2. Strip metadata comments from new content in preprocessAndSynchronizeDomContent before building the logical tree during enhanced navigation. 3. Add defensive null check in insertLogicalChild for disconnected reference nodes. Fixes #64722 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
b530b67 to
4f13842
Compare
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
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.
Fix Blazor crash when interactive component is in layout with enhanced navigation
Fixes #64722
Problem
When an
InteractiveAutoorInteractiveWebAssemblycomponent is placed in the layout (e.g.<Counter />inMainLayout.razor), navigating between pages via enhanced navigation causes:Root Cause
The
<!--Blazor-WebAssembly:{...}-->metadata comment was removed from the physical DOM duringdiscoverWebAssemblyOptions(), but remained in the parent element's logical children array as an orphaned node (parentNode === null). On subsequent enhanced navigations,insertLogicalChild()picked this orphaned comment as a reference node forinsertBefore, crashing becauseparentNodewas null.Changes
Reorder
registerAllComponentDescriptors— CalldiscoverWebAssemblyOptionsbeforeupgradeComponentCommentsToLogicalRootComments, ensuring metadata comments are removed from the DOM before the logical tree is built.Strip metadata in
preprocessAndSynchronizeDomContent— Remove metadata comments from new enhanced-nav content before building the logical tree, preventing orphaned nodes during navigation.Defensive null check in
insertLogicalChild— Replace the non-null assertionparentNode!.insertBefore(...)with a null check, falling back toappendDomNodeif the reference node is disconnected.