dotnet / dotnet/aspnetcore

Issue disposing a component's JS complement in a Blazor Web App using mixed render modes

Open
#62,230 4 comments 4 reactions 0 assignees View on GitHub
area-blazor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

There is no reliable way for disposing a component's JS complement in a Blazor Web App using mixed render modes.

Additionally, the documentation confirms this behavior: [DOM cleanup tasks during component disposal](https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/?view=aspnetcore-9.0#dom-cleanup-tasks-during-component-disposal), suggesting the use of a `MutationObserver` on the component’s parent HTML element. However, this approach fails in scenarios where a higher-level ancestor (e.g., the parent of the parent) is removed. Using a `MutationObserver` on the entire document to catch such removals is inefficient.

Calling dispose JS method from the .NET side (during component disposal) to JavaScript doesn't work in apps with mixed render modes — the element mappings are cleared when navigating from a page with one render mode to another with a different render mode. ( ⚠ with Blazor Server or Blazor WebAssembly apps there is no such issue).

Please, check the attached sample.

### Expected Behavior

component's element mapping is retained until its .NET side is disposed.

### Steps To Reproduce

Run sample: [BlazorMixedRenderModeIssueWebApp.zip](https://github.com/user-attachments/files/20577917/BlazorMixedRenderModeIssueWebApp.zip)

- navigate to Counter page
- navigate to Weather page

### Exceptions (if any)

![Image](https://github.com/user-attachments/assets/78d149ff-5c1f-4fdc-9628-11529f52cff6)

### .NET Version

9.0.300

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.