Last active
August 24, 2019 03:38
-
-
Save aapoalas/b06babab67b03bc437812882083c6fd7 to your computer and use it in GitHub Desktop.
ECMAScript dynamic import without CSP violation errors
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
export default url => new Promise((res, rej) => { | |
const script = document.createElement("script"); | |
script.src = url; | |
script.type = "module"; | |
const onload = () => { | |
script.remove(); | |
res(import(url)); | |
}; | |
const onerror = error => { | |
script.remove(); | |
rej(error); | |
}; | |
script.addEventListener("load", onload, { once: true }); | |
script.addEventListener("error", onerror, { once: true }); | |
document.head.append(script); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment