Issue disposing a component's JS complement in a Blazor Web App using mixed render modes
- 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)

### .NET Version
9.0.300
### Anything else?
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.