Skip to content

Fix Blazor crash when interactive component is in layout with enhanced nav#66007

Draft
maraf wants to merge 1 commit intomainfrom
maraf/fix-64722-enhanced-nav-layout-crash
Draft

Fix Blazor crash when interactive component is in layout with enhanced nav#66007
maraf wants to merge 1 commit intomainfrom
maraf/fix-64722-enhanced-nav-layout-crash

Conversation

@maraf
Copy link
Copy Markdown
Member

@maraf maraf commented Mar 27, 2026

Fix Blazor crash when interactive component is in layout with enhanced navigation

Fixes #64722

Problem

When an InteractiveAuto or InteractiveWebAssembly component is placed in the layout (e.g. <Counter /> in MainLayout.razor), 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 physical DOM during discoverWebAssemblyOptions(), 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 for insertBefore, crashing because parentNode was null.

Changes

  1. Reorder registerAllComponentDescriptors — Call discoverWebAssemblyOptions before upgradeComponentCommentsToLogicalRootComments, ensuring metadata comments are removed from the DOM before the logical tree is built.

  2. Strip metadata in preprocessAndSynchronizeDomContent — Remove metadata comments from new enhanced-nav content before building the logical tree, preventing orphaned nodes during navigation.

  3. Defensive null check in insertLogicalChild — Replace the non-null assertion parentNode!.insertBefore(...) with a null check, falling back to appendDomNode if the reference node is disconnected.

@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Mar 27, 2026
@maraf maraf force-pushed the maraf/fix-64722-enhanced-nav-layout-crash branch from 22a336b to b530b67 Compare March 27, 2026 12:53
…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>
@maraf maraf force-pushed the maraf/fix-64722-enhanced-nav-layout-crash branch from b530b67 to 4f13842 Compare March 27, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Blazor Web App Crash in .NET 10 When Auto Component on the Homepage

1 participant