Skip to content

Instantly share code, notes, and snippets.

@aapoalas
Last active August 24, 2019 03:38
Show Gist options
  • Save aapoalas/b06babab67b03bc437812882083c6fd7 to your computer and use it in GitHub Desktop.
Save aapoalas/b06babab67b03bc437812882083c6fd7 to your computer and use it in GitHub Desktop.
ECMAScript dynamic import without CSP violation errors
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