Created
June 13, 2019 13:29
-
-
Save jzeltman/4e7f57a95d2adab99b3b0b12e797d199 to your computer and use it in GitHub Desktop.
Load Polyfills based on Feature Detection
This file contains hidden or 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
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