diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index 1dcffff056..55691aecf6 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -446,8 +446,8 @@ export default function dom( this._root =this.attachShadow({ mode: '${options.shadowDom}' }); ` || b` this._copycontent(); - const observer = new MutationObserver(() => this._slotcontent()); - observer.observe(this, {childList: true, subtree: true}); + this.slotObserver = new MutationObserver(() => this._slotcontent()); + this.slotObserver.observe(this, {childList: true, subtree: true}); `} ${css.code && !lightDom && b`this._root.innerHTML = \`\`;`} ${should_add_css && lightDom && b`if (!@_document.getElementById("${component.stylesheet.id}-style")) ${add_css}();`} @@ -544,6 +544,17 @@ export default function dom( } }` as FunctionExpression }); + + declaration.body.body.push({ + type: 'MethodDefinition', + kind: 'method', + static: false, + computed: false, + key: { type: 'Identifier', name: 'disconnectedCallback' }, + value: x`function() { + this.slotObserver.disconnect(); + }` as FunctionExpression + }); } declaration.body.body.push(...accessors);