Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Last active August 29, 2015 13:57
Show Gist options
  • Save jakearchibald/120309d88a8bf025e92e to your computer and use it in GitHub Desktop.
Save jakearchibald/120309d88a8bf025e92e to your computer and use it in GitHub Desktop.
<link rel="preload" href="/a.js" type="application/javascript" class="core">
<link rel="preload" href="/b.js" type="application/javascript" class="cms-1-preload">
<link rel="preload" href="/c.js" type="application/javascript" class="cms-2-preloads">
<link rel="preload" href="/d.js" type="application/javascript" class="cms-2-preloads">
<script type="module" name="load-core">
var corePromise;
export default function() {
if (!corePromise) {
corePromise = document.querySelector('.core').loaded().then(function(link) {
var script = document.createElement('script');
script.src = link.href;
document.head.appendChild(script);
});
}
return corePromise;
};
</script>
<!-- CMS plugin 1 -->
<script type="module">
import loadCore from "load-core";
document.ready(function() {
document.querySelector('.button').addEventListener('click', function(event) {
loadCore().then(function() {
var link = document.querySelector('.cms-1-preload');
var script = document.createElement('script');
script.src = link.href;
document.head.appendChild(script);
});
event.preventDefault();
});
});
</script>
<!-- CMS plugin 2 -->
<script type="module">
import loadCore from "load-core";
loadCore().then(function() {
Array.from(document.querySelectorAll('.cms-2-preloads')).reduce(function(chain, link) {
return chain.then(function() {
var script = document.createElement('script');
script.src = link.href;
document.head.appendChild(script);
return script.loaded();
});
}, Promise.resolve());
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment