-
-
Save jakearchibald/1b12a0e5414a69d9350f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link rel="preload" href="/a.js" type="application/javascript" class="script-preload"> | |
<link rel="preload" href="/b.js" type="application/javascript" class="script-preload"> | |
<link rel="preload" href="/c.js" type="application/javascript" class="script-preload"> | |
<script> | |
Array.from(document.querySelectorAll('.script-preload')).reduce(function(chain, link) { | |
var err = false; | |
var oldOnerror; | |
function errorListener(msg) { | |
err = msg; | |
if (oldOnerror) { | |
return oldOnerror.apply(arguments); | |
} | |
} | |
return chain.then(function() { | |
return link.loaded(); | |
}).then(function() { | |
oldOnerror = window.onerror; | |
window.onerror = errorListener; | |
var script = document.createElement('script'); | |
script.src = link.href; | |
document.head.appendChild(script); | |
return script.loaded(); | |
}).then(function() { | |
window.onerror = oldOnerror; | |
if (err) throw Error(err); | |
}); | |
}, Promise.resolve()); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment