Skip to content

Instantly share code, notes, and snippets.

@neopunisher
Created March 19, 2024 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neopunisher/4e56d2c5874852bca8652d786a67b7b8 to your computer and use it in GitHub Desktop.
Save neopunisher/4e56d2c5874852bca8652d786a67b7b8 to your computer and use it in GitHub Desktop.
Dynamic importmap
if (HTMLScriptElement.supports && HTMLScriptElement.supports('importmap')) {
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap
const importMap = {
imports: {
lazyload: 'IntersectionObserver' in window
? './lazyload.js'
: './lazyload-fallback.js',
},
};
const im = document.createElement('script');
im.type = 'importmap';
im.textContent = JSON.stringify(importMap);
document.currentScript.after(im);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment