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/).
- Run
npm i dynamic-polyfiller
- Import in your app (example below for React):
import 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();'
);
- Copy the contents of dynamicpolyfill.js
- Paste it into your existing JS file(s)
- Add a new line after it and call
dynamicPolyfill();
Note: Case-sensitive- Example:
dynamicPolyfill( ["IntersectionObserver", "Object.assign"], 'https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js', 'quicklink();' )
- Add an
onLoad
attribute to the tag calling thedynamicPolyfill()
function and passing your parameters- Note: the first parameter is the feature polyfills you want to pass. This is expected as an array.
- Note: the second paramter is the URL of the script you want to use. This is expected as a either a
string
or anarray
, but can be blank (''
) ornull
if you're not loading a third party script. - Note: the third parameter is the function that you would run once the script has loaded. This is expected as a
string
or anarray
. - Note: the 4th parameter has now been deprecated.
- Add a
<script></script>
tag linking to this script- Example:
<script src='https://cdn.jsdelivr.net/gh/willstocks-tech/dynamically-polyfill-features-for-a-script@master/dynamicpolyfill.min.js'> </script>
- Add an
onLoad
attribute to the tag calling thedynamicPolyfill()
function and passing your parameters- Note: the first parameter is the feature polyfills you want to pass. This is expected as an array.
- Note: the second paramter is the URL of the script you want to use. This is expected as a either a
string
or anarray
, but can be blank (''
) ornull
if you're not loading a third party script. - Note: the third parameter is the function that you would run once the script has loaded. This is expected as a
string
or anarray
. - Note: the 4th parameter has now been deprecated.
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>
- Vanilla Javascript - no framework dependencies whatsoever!
- Polyfill.io - for the actual polyfills!
For the versions available, check out the repo release history. I'd recommend sticking to the "latest release" though!
- Will Stocks - Initial work - willstocks-tech
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
- PurpleBooth for this awesome README template!
- Polyfill.io for the awesome polyfill service!