Skip to content

Commit

Permalink
load event refinement
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Mar 28, 2024
1 parent c7a0538 commit 5bfdac4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,9 @@ Alternatively, add a `blob:` URL policy with the CSP build to get CSP compatibil

### No Load Event Retriggers

Because of the extra processing done by ES Module Shims it is possible for static module scripts to execute after the `DOMContentLoaded` or `readystatechange` events they expect, which can cause missed attachment.
Because of the extra processing done by ES Module Shims it is possible for static module scripts to execute after the `load`, `DOMContentLoaded` or `readystatechange` events they expect, which can cause missed attachment.

In addition, script elements will also have their load events refired when polyfilled.

In order to ensure libraries that rely on these event still behave correctly, ES Module Shims will always double trigger these events that would normally have executed before the document ready state transition to completion, once all the static module scripts in the page have been completely executed through ES module shims.

Expand All @@ -676,7 +678,7 @@ In such a case, this double event firing can be disabled with the `noLoadEventRe
```js
<script type="esms-options">
{
// do not re-trigger DOM events (onreadystatechange, DOMContentLoaded)
// do not re-trigger DOM events (load, onreadystatechange, DOMContentLoaded)
"noLoadEventRetriggers": true
}
</script>
Expand Down
5 changes: 3 additions & 2 deletions src/es-module-shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,9 @@ function processScript (script, ready = readyStateCompleteCnt > 0) {
if (isDomContentLoadedScript) domContentLoadedCnt++;
if (self.ESMS_DEBUG) console.info(`es-module-shims: processing ${script.src || '<inline>'}`);
const loadPromise = topLevelLoad(script.src || pageBaseUrl, getFetchOpts(script), !script.src && script.innerHTML, !shimMode, isBlockingReadyScript && lastStaticLoadPromise)
.catch(throwError)
.then(() => script.dispatchEvent(new Event('load')));
.catch(throwError);
if (!noLoadEventRetriggers)
loadPromise.then(() => script.dispatchEvent(new Event('load')));
if (isBlockingReadyScript)
lastStaticLoadPromise = loadPromise.then(readyStateCompleteCheck);
if (isDomContentLoadedScript)
Expand Down

0 comments on commit 5bfdac4

Please sign in to comment.