Polymer 3.x has been developed alongside and tested with a new suite of v1-spec compatible polyfills
for custom elements and shadow DOM. These versions no longer include the HTML imports polyfill,
and have been developed to work with ES6 modules. You can test Polymer 3.x using the latest 2.0 version of
webcomponentsjs
v2.0.0 or later.
(Polyfill versions v1.x.x include the HTML imports polyfill, and are compatible with Polymer 2.x. Versions prior to v1.0 support the older, v0 specifications for custom elements and shadow DOM.)
There are two main ways to load the polyfills:
-
webcomponents-bundle.js
includes all of the polyfills necessary to run on any of the supported browsers. Because all browsers receive all polyfills, this results in extra bytes being sent to browsers that support one or more feature. This replaces the v1.xwebcomponents-lite.js
bundle. -
webcomponents-loader.js
performs client-side feature-detection and loads just the required polyfills. This requires an extra round-trip to the server, but saves bandwidth for browsers that support one or more features.
In both cases, the polyfills should be loaded just once. The polyfills are generally loaded from the
application entrypoint (index.html
or similar).
There are a couple of other related polyfill files that you may need:
custom-elements-es5-adapter.js
. This small polyfill allows you to run compiled, ES5 elements on browsers that support native custom elements. This is useful in static serving environments where you need to serve a single app version to all browsers. The adapter is discussed in more detail in ES6 and in Build for production. Important note: the es5 adapter must come before the webcomponents polyfills, if any.apply-shim.js
. A polyfill for CSS mixins. Unlike the other polyfills, it should be imported by any component that defines or applies CSS mixins. For details, see Use custom CSS mixins.
References:
Polyfill settings
By default, the individual polyfill for a given feature is disabled on browsers that natively support that feature. For testing purposes, you can force the polyfills on for browsers that have native support. You can force the polyfills on by adding a JavaScript snippet before you import the polyfills:
<script>
// Force all polyfills on
if (window.customElements) window.customElements.forcePolyfill = true;
ShadyDOM = { force: true };
ShadyCSS = { shimcssproperties: true};
</script>
<script src="./node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
The webcomponents-bundle.js
file also supports forcing the polyfills on by adding query parameters to
the app's URL:
https://www.example.com/my-application/view1?wc-ce&wc-shadydom&wc-shimcssproperties
The following table lists the JavaScript snippets and query parameters for each polyfill.
Polyfill | Description |
Custom Elements |
JavaScript:
Query parameter:
|
Shadow DOM |
JavaScript:
Query parameter:
|
CSS custom properties |
JavaScript:
Query parameter:
|
Progress of native browser support
As of May 2018 the v1 versions of the Custom Elements and Shadow DOM APIs have been shipped in Chrome, Opera, and Safari, and implementations underway in other browsers.
See caniuse.com for more information on native browser support for web components.
Notes:
-
Chrome natively implements both the v0 APIs (used by Polymer 1.x) and the v1 APIs (used by Polymer 2.x & 3.x).
-
Safari includes working implementations of Shadow DOM v1 and Custom Elements v1.
-
Firefox includes experimental implementations of Custom Elements v1 and Shadow DOM v1. These implementations are currently disabled by default but are expected to be enabled by about version 63.
-
Edge has on its backlog to support Shadow DOM v1 and Custom Elements v1.