A little script that allows you to only polyfill a feature when absolutely necessary - no wasted requests on browsers that have native support! ??
This script is ~4.2KB unminified (121 lines) (1.04KB unminified and gzipped), ~8.37KB unminified with inline comments (121 lines) (2.12KB unminified and gzipped), or ~1.9KB minified (1 line) (743B minified and gzipped), so it's fairly light. ?
See deployment for notes on how to deploy the project on a live system.
Make sure you know what features your script is reliant on and polyfill those not natively supported on the browsers you support (you can check https://caniuse.com/).
npm i dynamic-polyfillerimport React from 'react';
import {render} from 'react-dom';
//The import below is the bit you need!
import dynamicPolyfill from 'dynamic-polyfiller';
dynamicPolyfill(
["IntersectionObserver", "Object.assign"],
'https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js',
'quicklink();'
);dynamicPolyfill(); Note: Case-sensitive
dynamicPolyfill( ["IntersectionObserver", "Object.assign"], 'https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js', 'quicklink();' )
onLoad attribute to the tag calling the dynamicPolyfill() function and passing your parameters
string or an array, but can be blank ('') or null if you're not loading a third party script.string or an array .<script></script> tag linking to this script
<script src='https://cdn.jsdelivr.net/gh/willstocks-tech/dynamically-polyfill-features-for-a-script@master/dynamicpolyfill.min.js'>
</script>
onLoad attribute to the tag calling the dynamicPolyfill() function and passing your parameters
string or an array, but can be blank ('') or null if you're not loading a third party script.string or an array .Note: Loading from a CDN would still result in a potentially wasted request ?
dynamicPolyfill( 'IntersectionObserver', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js', 'quicklink();' );
dynamicPolyfill( ["IntersectionObserver", "Object.assign"], ['https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js', 'https://other.cdn.net/script.js'], ['quicklink();', 'otherFunction();'] );
Note: You need to ensure that before you call the dynamicPolyfill() function that the actual script itself has loaded. If you're going to host the script yourself (rather than calling out to a CDN), make sure you include the script code first, then call the function. You can do this in the same manner as above, but replace the CDN URL with the path to your own JS file, if you're not going to call it from the same file.
<script
src='https://cdn.jsdelivr.net/gh/willstocks-tech/dynamically-polyfill-features-for-a-script@master/dynamicpolyfill.min.js'
onload='dynamicPolyfill( ["IntersectionObserver", "Object.assign"], 'https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js', 'quicklink();')'>
</script>
<script
src='https://cdn.jsdelivr.net/gh/willstocks-tech/dynamically-polyfill-features-for-a-script@master/dynamicpolyfill.min.js'
onload='dynamicPolyfill(["IntersectionObserver", "Object.assign"], ['https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js', 'https://other.cdn.net/script.js'], ['quicklink();', 'otherFunction();'])'>
</script>
For the versions available, check out the repo release history. I'd recommend sticking to the "latest release" though!
See also the list of contributors who have participated in this little nugget!
Please read CONTRIBUTING.md for details on the code of conduct, and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE.md file for details