diff --git a/src/entrypoints/custom-panel.ts b/src/entrypoints/custom-panel.ts index f03ccf9b07..3665931479 100644 --- a/src/entrypoints/custom-panel.ts +++ b/src/entrypoints/custom-panel.ts @@ -28,6 +28,7 @@ window.loadES5Adapter = () => { }; let panelEl: HTMLElement | undefined; +let initialized = false; function setProperties(properties) { if (!panelEl) { @@ -128,13 +129,23 @@ function initialize( }); } -document.addEventListener( - "DOMContentLoaded", - () => window.parent.customPanel!.registerIframe(initialize, setProperties), - { once: true } -); +function handleReady() { + if (initialized) return; + initialized = true; + window.parent.customPanel!.registerIframe(initialize, setProperties); +} -window.addEventListener("unload", () => { +// Initial load +if (document.readyState === "loading") { + document.addEventListener("DOMContentLoaded", handleReady, { once: true }); +} else { + handleReady(); +} + +window.addEventListener("pageshow", handleReady); + +window.addEventListener("pagehide", () => { + initialized = false; // allow disconnected callback to fire while (document.body.lastChild) { document.body.removeChild(document.body.lastChild);