Skip to content

Instantly share code, notes, and snippets.

@benbarber
Created September 29, 2017 08:35
Show Gist options
  • Save benbarber/4abaf9ca216c9bc66981d2470fad1dde to your computer and use it in GitHub Desktop.
Save benbarber/4abaf9ca216c9bc66981d2470fad1dde to your computer and use it in GitHub Desktop.
Polyfill loader for for React 16
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