Created
September 29, 2017 08:35
-
-
Save benbarber/4abaf9ca216c9bc66981d2470fad1dde to your computer and use it in GitHub Desktop.
Polyfill loader for for React 16
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
const features = [ | |
'Map', | |
'Set', | |
'requestAnimationFrame' | |
] | |
function browserSupportsAllFeatures() { | |
return features.every(f => window[f]) | |
} | |
function missingFeatures() { | |
return features.filter(f => !window[f]) | |
} | |
function loadScript(done) { | |
// eslint-disable-next-line prefer-template | |
const cdn = 'https://cdn.polyfill.io/v2/polyfill.min.js?features=' + missingFeatures().join() | |
const js = document.createElement('script') | |
js.src = cdn | |
js.onload = function onLoad() { | |
done() | |
} | |
js.onerror = function onError() { | |
// eslint-disable-next-line prefer-template | |
done(new Error('Failed to load script ' + cdn)) | |
} | |
document.head.appendChild(js) | |
} | |
module.exports = (done) => { | |
if (browserSupportsAllFeatures()) { | |
return done() | |
} | |
loadScript(done) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment