Skip to content

Commit 0751fbd

Browse files
authored
MutationObserver and DOM element removal (#34379)
1 parent e203aa3 commit 0751fbd

File tree

1 file changed

+7
-0
lines changed
  • aspnetcore/blazor/javascript-interoperability

1 file changed

+7
-0
lines changed

aspnetcore/blazor/javascript-interoperability/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,13 @@ export class DOMCleanup {
315315
window.DOMCleanup = DOMCleanup;
316316
```
317317

318+
The preceding approaches attach the `MutationObserver` to `target.parentNode`, which works until `parentNode` itself is removed from the DOM. This is a common scenario, for example, when navigating to a new page, which causes the entire page component to be removed from the DOM. In such cases, any child components observing changes within the page aren't cleaned up properly.
319+
320+
Don't assume that observing `document.body`, instead of `target.parentNode`, is a better target. Observing `document.body` has performance implications because callback logic is executed for *all* DOM updates, whether or not they have anything to do with your element. Use either of the following approaches:
321+
322+
* In cases where you can identify a suitable ancestor node to observe, use `MutationObserver` with it. Ideally, this ancestor is scoped to the changes that you want to observe, rather than `document.body`.
323+
* Instead of using `MutationObserver`, consider using a [custom element and `disconnectedCallback`](https://developer.mozilla.org/docs/Web/API/Web_components/Using_custom_elements). The event always fires when your custom element is disconnected, no matter where it resides in the DOM relative to the DOM change.
324+
318325
## JavaScript interop calls without a circuit
319326

320327
*This section only applies to server-side apps.*

0 commit comments

Comments
 (0)