Skip to content

Instantly share code, notes, and snippets.

@luan0ap
Created January 5, 2018 14:45
Show Gist options
  • Save luan0ap/31ccd6168566819dc744c3d28b83483d to your computer and use it in GitHub Desktop.
Save luan0ap/31ccd6168566819dc744c3d28b83483d to your computer and use it in GitHub Desktop.
Loading polyfill only when needed
const browserSupportsAllFeatures = () => window.Promise && window.fetch && window.Symbol
const loadScript = (src = '', done) => {
var js = document.createElement('script')
js.src = src
document.head.appendChild(js)
return done()
}
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment