From 347eaffd4a9eec87ff1a46caa1ee13290ff1c997 Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Sun, 17 May 2020 08:49:37 -0700 Subject: [PATCH] WIP: Don't attempt to unsub if the iframe is destroyed (#4782) Fixes #4752 by not attempting to call .removeEventListener if the iframe.contentWindow no longer exists. --- src/runtime/internal/dom.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index 1823cba273..02fd3dc23e 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -270,9 +270,11 @@ export function add_resize_listener(node: HTMLElement, fn: () => void) { iframe.setAttribute('aria-hidden', 'true'); iframe.tabIndex = -1; + const crossorigin = is_crossorigin(); + let unsubscribe: () => void; - if (is_crossorigin()) { + if (crossorigin) { iframe.src = `data:text/html,`; unsubscribe = listen(window, 'message', (event: MessageEvent) => { if (event.source === iframe.contentWindow) fn(); @@ -287,8 +289,13 @@ export function add_resize_listener(node: HTMLElement, fn: () => void) { append(node, iframe); return () => { + if (crossorigin) { + unsubscribe(); + } else if (unsubscribe && iframe.contentWindow) { + unsubscribe(); + } + detach(iframe); - if (unsubscribe) unsubscribe(); }; }