Skip to content

Commit

Permalink
fix: add mutation observer only to head not body (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Jan 20, 2025
1 parent 6480d00 commit aaa63a5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,13 @@ In polyfill mode, multiple import maps are supported.
Support for dynamically injecting import maps with JavaScript via e.g.:

```js
document.body.appendChild(Object.assign(document.createElement('script'), {
document.head.appendChild(Object.assign(document.createElement('script'), {
type: 'importmap',
innerHTML: JSON.stringify({ imports: { x: './y.js' } }),
}));
```

is also provided using mutation observers.
is also provided using mutation observers. Note, only `document.head` appending of scripts, importmaps and preloads is supported - we do not observe body mutations to `document.body` for performance reasons.

The caveat for multiple import map support polyfill support in browsers that only support a single import map is per the usual "polyfill rule" for es-module-shims - only those top-level graphs with static import feailures can be polyfilled.

Expand Down
2 changes: 1 addition & 1 deletion src/es-module-shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ function attachMutationObserver() {
}
}
});
observer.observe(document, { childList: true });
observer.observe(document.head, { childList: true });
observer.observe(document.body, { childList: true });
processScriptsAndPreloads();
}

Expand Down
4 changes: 2 additions & 2 deletions test/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ suite('Errors', function () {
type: 'importmap-shim',
innerHTML: JSON.stringify(importMap),
});
document.body.appendChild(script);
return () => document.body.removeChild(script);
document.head.appendChild(script);
return () => document.head.removeChild(script);
}
});

Expand Down

0 comments on commit aaa63a5

Please sign in to comment.