The polyfills loader makes it easy to manage loading polyfills and/or serving different versions of your application based on browser support. It generates a script that loads the necessary polyfills and the appropriate version of the application through on runtime feature detection.
Part of Open Web Components: guides, tools and libraries for modern web development and web components
A simplified version of the loader:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
The primary reason for this project is to make it easier to build performant web apps. The web ecosystem is evolving fast, it’s easy to end up with many unnecessary polyfills in your application because all supported browsers already implement the feature you are using.
By loading polyfills conditionally, you make sure you only load what’s necessary and you don’t need to include them in your main bundle. Polyfills are hashed based on content, so they can be cached indefinitely by a web server or service worker.
Serving different versions of your application means you don’t need to serve the lowest common denominator to all of your users. This is often achieved used <script type="module">
and <script nomodule>
. The polyfills loader uses a variation of this, where the feature detection happens in javascript because we need to ensure polyfills are loaded before any of your application code is run.
Browser optimize loading webpages by scanning ahead for script tags and fetching them right away. Because the polyfills loader moves the loading of scripts into javascript, we lose out on this optimization. For the polyfills, this is intentional, because we don’t want to load them all the time and since they will be cached this optimization does not do much anyway.
For your application code, we recommend using preload
(or modulepreload
when it is widely supported) links to ensure your application’s modern build is fetched from the start.
1 2 3 4 5 |
|
You will most likely use the polyfills loader as part of another tool, but it can be used directly as well.
A typical web app which loads as modules on modern browsers, and system js on older browsers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
The files to load on modern browsers. Loaded if no legacy entry points match for the current browser, or if none are configured. Each file specifies how it should be loaded.
1 2 3 4 5 6 7 8 9 |
|
Sets of files to load on legacy browsers, and the runtime feature detection to execute to determine whether it should be loaded.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
The polyfills config controls which polyills are injected onto the page. These are the possible polyfills:
They can be turned on using booleans. When using the polyfills loader directly, these are default false. Other tools may turn on different defaults.
1 2 3 4 5 6 7 |
|
In order to define css variables outside of a web component in IE11, you need to wrap the <style>
tag inside of a <shady-css-scoped>
tag. This tag is provided by the shady-css-scoped-element package, and will be included if you use both the webcomponents and shadyCssCustomStyle polyfills.
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 |
|
With the hash
option, polyfill filenames can be hashed based on their content, this allows them to be cached indefinitely.
1 2 3 4 5 6 7 8 |
|
If you need a polyfill that isn’t available in the default list, you can add a custom polyfill. These consist of at least a unique name, a path where to find the polyfill and a bit of javascript executed at runtime to test whether the polyfill should be loaded.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
The polyfills loader delays loading any scripts until polyfills are loaded. This can create problems when you rely on specific loading behavior. You can exclude certain scripts with the exclude
option.
1 2 3 4 5 6 7 8 |
|
Which directory to load polyfills from. By default, this is ./polyfills
1 2 3 |
|
Whether the code output should be minified.
1 2 3 |
|
The createPolyfillsLoader
function takes configuration and returns the javascript code for the polyfills loader. It also returns information about the generated polyfill files, these will need to be made available at runtime so that they can be imported by the loader code.
1 2 3 4 5 6 7 8 |
|
The injectPolyfillsLoader
function injects a polyfills loader into an existing HTML page. It also injects polyfills for any import maps it finds.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|