Here are some common recipes and advice for constructing polyfill requests. This isn't a substitute for the full API reference, and is not intended to show all possible permutations.
The simplest use: consider default polyfills, bundle, minify and and return those which are required by the current browser:
This example is minified by including .min
in the URL. All the other examples on this page are not minified, so you can see the metadata included at the top of the response, but any response can be minified by adding .min
in the same way as shown above.
Consider only the Array.prototype.map
and modernizr:es5array
polyfills, the first only if needed by the browser, the second regardless of the user-agent. No .min
here so the result is not minified.
If you don't specify any features
, 'default' is assumed, but you can also specify default explicitly if you want to combine it with something else. This example uses the default set plus fetch:
Use the es6
alias to include all features that are part of ECMAScript 6 (for which we currently have polyfills):
Consider default polyfills, but for an IE7 user-agent:
It's often a good idea to use the gated
flag to wrap polyfills in a feature-detect. Some people prefer not to do this and instead just trust the browser targeting, but to guard against mistargeting, incorrect user agent interpretation, and conflicts with other polyfills that may be on the same page, you can apply feature-detect gating to all features in the bundle at once:
Use the always
flag to force a polyfill to be returned regardless of whether the user-agent requires it:
This example uses a useragent override of chrome/37 to demonstrate one of the problems of using always
: in Chrome, there is no polyfill for Document that makes sense, because even in those browsers that do need the polyfill, no two browsers use the same one. As a result, even if you specify always
on this feature, you'll get no polyfill in chrome.
With both gated
and always
, you can apply them either at the feature-level using the pipe delimiter or to the whole request using the flags
parameter. This example applies both flags to one feature but neither to the other:
And this example applies both flags to the whole request:
To help out the project with collecting compatibility data, please consider enabling the `rum` option:
There are many ways you can incorporate the polyfill service into your site's resource loading strategy, to suit different standards and rules.
We recommend the use of the async
and defer
attributes on <script>
tags that load from the polyfill service, but loading from us in a non-blocking way means you can't know for certain whether your own code will execute before or after the polyfills are done loading.
To make sure the polyfills are present before you try to run your own code, you can attach an onload
handler to the polyfill-service-798d858f74-m29sv script tag, use a more sophisticated script loader or simply use our callback
argument to evaluate a global callback when the polyfills are loaded:
<script src="polyfill-service-798d858f74-m29sv/v2/polyfill.min.js?callback=polyfillsAreLoaded" defer async></script>
<script>
function polyfillsAreLoaded() {
console.log('Now to do the cool stuff...');
}
</script>
The polyfill service uses (and advocates) server side feature detection based on User-Agent
identification, but you can also use the service with feature detection in the browser if you like. Just run the tests you want and construct a polyfill-service-798d858f74-m29sv URL, with an always
flag to disable our user-agent feature-detection, and a ua
identifier set to a supported browser:
// Create a list of the features this browser needs.
// Beware of overly simplistic detects!
var features = [];
('Promise' in window) || features.push('Promise');
('IntersectionObserver' in window) || features.push('IntersectionObserver');
('after' in Element.prototype) || features.push('Element.prototype.after');
// If any features need to be polyfilled, construct
// a script tag to load the polyfills and append it
// to the document
if (features.length) {
var s = document.createElement('script');
// Include a `ua` argument set to a supported browser to skip UA identification
// (improves response time) and avoid being treated as unknown UA (which would
// otherwise result in no polyfills, even with `always`, if UA is unknown)
s.src = 'polyfill-service-798d858f74-m29sv/v2/polyfill.min.js?features='+features.join(',')+'&flags=gated,always&ua=chrome/50&callback=main';
s.async = true;
document.head.appendChild(s);
} else {
// If no polyfills are required, invoke the app
// without delay
main();
}
function main() {
console.log('Now to do the cool stuff...');
}
You can view the above example in a JSBin demo. Doing live feature detection in the browser is one of our oldest feature requests, and we hope to support this out of the box at some point. Follow issue 84 for updates.