Skip to content

Instantly share code, notes, and snippets.

@jzeltman
Created June 13, 2019 13:29
Show Gist options
  • Save jzeltman/4e7f57a95d2adab99b3b0b12e797d199 to your computer and use it in GitHub Desktop.
Save jzeltman/4e7f57a95d2adab99b3b0b12e797d199 to your computer and use it in GitHub Desktop.
Load Polyfills based on Feature Detection
function browserSupportsAllFeatures() {
// Your Polyfill needs go here
return window.Promise && window.fetch && window.Symbol;
}
function loadScript(src, done) {
let js = document.createElement('script');
js.src = src;
js.onload = function() {
done();
};
js.onerror = function() {
done(new Error('Failed to load script ' + src));
};
document.head.appendChild(js);
}
if (browserSupportsAllFeatures()) {
// Browsers that support all features run `main()` immediately.
main();
} else {
// All other browsers loads polyfills and then run `main()`.
loadScript('/path/to/polyfills.js', main);
}
function main(err) {
if (err) { console.error('An error occurred loading the necessary scripts: %O', err);
// Initiate all other code paths.
// If there's an error loading the polyfills, handle that
// case gracefully and track that the error occurred.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment