From 46cdd9ce3dca6217ed87862c2b37c6c0b3f643ab Mon Sep 17 00:00:00 2001 From: Yugandhar Yashwantrao Kamdi Date: Mon, 9 Sep 2024 19:10:00 +0530 Subject: [PATCH] Fix loadScript --- scripts/aem.js | 54 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/scripts/aem.js b/scripts/aem.js index 99f56f5442..fc32d20e31 100644 --- a/scripts/aem.js +++ b/scripts/aem.js @@ -261,21 +261,63 @@ async function loadCSS(href) { */ async function loadScript(src, attrs) { return new Promise((resolve, reject) => { - if (!document.querySelector(`head > script[src="${src}"]`)) { - const script = document.createElement('script'); + let script = document.querySelector(`head > script[src="${src}"]`); + const isLoaded = script && script.dataset.loaded; + const isFailed = script && script.dataset.failed; + if (isLoaded) { + resolve(); + return + } + + if (isFailed) { + reject(); + return; + } + + if (!script) { + script = document.createElement('script'); script.src = src; + + script.onload = () => { + script.dataset.loaded = true; + resolve(); + document.dispatchEvent(new CustomEvent('script-loaded', { + detail: { + src, + } + })) + } + script.onerror = (error) => { + script.dataset.failed = true; + reject(); + document.dispatchEvent(new CustomEvent('script-failed', { + detail: { + src, + error, + } + })) + } + if (attrs) { // eslint-disable-next-line no-restricted-syntax, guard-for-in for (const attr in attrs) { script.setAttribute(attr, attrs[attr]); } } - script.onload = resolve; - script.onerror = reject; + document.head.append(script); - } else { - resolve(); + return; } + + const successHandler = (evt) => { + if (evt.detail.src !== src) { + return; + } + + resolve(); + document.removeEventListener('script-loaded', successHandler); + }; + document.addEventListener('script-loaded', successHandler); }); }