Skip to content

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

Closed

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


BlazorNet10BugRepro.zip

In a Blazor Web App, when I add the Counter component to the Home page, the site will crash when I navigate from Home to Counter (the dedicated page) and then back to Home. I get a JavaScript error in the Blazor.web.js file that states TypeError: Cannot read properties of null (reading 'insertBefore')

This does not happen in .NET 9 projects, only if I build a .NET 10 project. Also, this happens whether I am using Visual Studio 2026, VS Code, or even just dotnet run. It does not matter if I use localhost or use the new dev.localhost TLD.

Also, if I navigate to a different page (I created a /Demo page that was InteractiveServer) after the site starts up on Home, I can then use the site without issues. I can go to /Counter without a problem, I can go to /Home, then to /Counter and back without a problem. It is only if I navigate from /Home to /Counter directly on load that there is an issue. And that is only an issue when the Counter component is also on the /Home page.

I have attached a simple demo project that shows off the issue.


Original Comments

Feedback Bot on 12/10/2025, 06:26 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Activity

  1. added
    Author: Migration Bot 🤖
    on Dec 10, 2025
  2. added
    area-blazor
    on Dec 10, 2025
  3. added
    Needs: Repro
    on Dec 11, 2025
  4. added this to the Backlog milestone on Dec 11, 2025
  5. added
    Needs: Author Feedback
    on Dec 11, 2025
  6. TimCorey commented on Dec 11, 2025

    @TimCorey

    Here is a repro project for this issue: https://github.com/IAmTimCorey/BlazorApp11

  7. added
    Needs: Attention 👋
    and removed
    Needs: Author Feedback
    on Dec 11, 2025
  8. removed this from the Backlog milestone on Dec 17, 2025
  9. ibeuel commented on Dec 18, 2025

    @ibeuel

    I’m experiencing the same issue as well. 2026 looks very serious.

  10. added and removed
    Needs: Attention 👋
    Needs: Repro
    on Dec 18, 2025
  11. added this to the Backlog milestone on Dec 18, 2025
  12. Notallthatevil commented on Jan 29, 2026

    @Notallthatevil

    I am experiencing this as well. For me, it seems to occur when a Layout contains a component with a interactive render mode. When this happens and I navigate to a page that also has interactivity it loads just fine, however, when I navigate to a page without, it causes the Cannot read properties of null (reading 'insertBefore') error and navigation fails to load correctly.
    Using the default Blazor Web App template, if you modify the MainLayout.razor file to include the counter page you can reproduce this error.

    @using BlazorApp1.Client.Pages
    @using BlazorApp1.Components.Pages
    @inherits LayoutComponentBase
    
    <div class="page">
    	<div class="sidebar">
    		<NavMenu />
    	</div>
    
    	<main>
    		<div class="top-row px-4">
    			<a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
    		</div>
    
    		<article class="content px-4">
    			@Body
    			<Counter />
    			@* <Weather /> *@
    		</article>
    	</main>
    </div>
    
    <div id="blazor-error-ui" data-nosnippet>
    	An unhandled error has occurred.
    	<a href="." class="reload">Reload</a>
    	<span class="dismiss">🗙</span>
    </div>

    If you comment out the Counter and uncomment Weather, no issues occur

  13. maraf commented on Mar 27, 2026

    @maraf
    Member

    Root Cause Analysis

    Reproduction

    Default Blazor Web App (.NET 10, dotnet new blazor --interactivity Auto), add <Counter /> to MainLayout.razor alongside @Body. Navigate: Home → /counter → Home → crash.

    Error

    TypeError: Cannot read properties of null (reading 'insertBefore')
      at insertLogicalChild (LogicalElements.ts)
      at insertLogicalChildBefore
      at treatAsInsertion (DomSync.ts)
      at synchronizeDomContentCore
    

    Root Cause: Detached metadata comment in the logical children array

    The <!--Blazor-WebAssembly:{...}--> metadata comment is physically removed from the DOM during component/WebAssembly-options discovery, but it remains in the parent element's logical children array as an orphaned node (isConnected === false, parentNode === null).

    Using Playwright to dump Blazor's internal logical tree at each step:

    1. Home page load — <article> has 5 logical children, including:

    [3] Comment("Blazor-WebAssembly:{...}") [connected=false, parentNode=null]  ← ALREADY DETACHED
    [4] Comment("!") — Counter component root (interactive, 6 logical children)
    

    2. After navigating to /counter — <article> has 4 logical children:

    [0] Comment("Blazor-WebAssembly:{...}") [connected=false, parentNode=null]  ← STILL DETACHED
    [1] Comment("!") — Counter from @Body (the /counter page)
    [2] Text("\n            ")
    [3] Comment("!") — Counter from layout
    

    3. Navigating back to Home — the DOM sync (DomSync.ts) computes a Levenshtein edit script to transform the current 4 logical children into the new server-rendered content (which has ~5 logical children — the Blazor-WebAssembly comment is skipped by isMetadataComment when building the new content's logical tree). The first edit operation is Insert (insert <h1>Hello, world!</h1> before the first current logical child). This triggers:

    // In insertLogicalChild() — LogicalElements.ts line ~155:
    const newSiblings = getLogicalChildrenArray(parent);      // article's logical children
    const nextSibling = newSiblings[childIndex] as any as Node; // = the detached WasmMeta comment
    nextSibling.parentNode!.insertBefore(nodeToInsert, nextSibling);
    // CRASH: parentNode is null — the metadata comment was removed from the DOM
    //        but never cleaned out of the logical children array

    Why this happens

    1. When the page is first loaded, toLogicalElement(article, true) makes all physical child nodes into logical children — including the <!--Blazor-WebAssembly:{...}--> metadata comment.
    2. During component discovery, discoverWebAssemblyOptions() (or related code) consumes and removes this metadata comment from the physical DOM.
    3. But it is not removed from the parent's logical children array. It remains as an orphaned entry: parentNode === null, isConnected === false.
    4. On subsequent enhanced navigations, the DOM sync's insertLogicalChild() assumes all nodes in the logical children array are attached to the DOM (the ! in parentNode!.insertBefore is a non-null assertion). When it picks the orphaned metadata comment as the reference node for insertBefore, it crashes.

    Why it only manifests with an interactive component in the layout

    The <!--Blazor-WebAssembly:{...}--> metadata comment is emitted in the SSR output for pages containing InteractiveAuto or InteractiveWebAssembly components. Placing such a component in the layout means every page carries this metadata comment. It gets orphaned on the first load and then sits in the logical children array, where any subsequent DOM sync can hit it.

    Files involved

    Workaround

    Disable DOM preservation in enhanced navigation by manually starting Blazor in App.razor:

    <script src="_framework/blazor.web.js" autostart="false"></script>
    <script>
        Blazor.start({ ssr: { disableDomPreservation: true } });
    </script>

    This bypasses the DOM sync code path entirely, avoiding the crash. Navigation will still work but will do full page replacements instead of incremental DOM patching.

  14. added 3 commits that reference this issue on Mar 27, 2026
    22a336b
    b530b67
    4f13842
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Author: Migration Bot 🤖area-blazorinvestigate

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions