liveBook · Manning

WebComponentsReady

As explained in Web Components in Action

WebComponentsReady is a browser event signaling that Web Components polyfills and component definitions have finished loading.

WebComponentsReady is an event-like readiness signal for Web Components on a page. It indicates that polyfills and component definitions can be applied to matching elements, making it useful when setup timing matters. section 14.2

Despite its browser-style usage, WebComponentsReady is generated by the Web Components polyfill rather than by the browser itself. Natively supported Web Components do not require this polyfill-specific signal. section 14.2

Listen for page-wide readiness

Load the polyfill, then register a listener: document.addEventListener('WebComponentsReady', function () { console.log('components ready'); }); This callback runs after the polyfill has made Web Component definitions ready for page elements. section 14.2

Wait for one component instead

For a specific custom element, use customElements.whenDefined('sample-component').then(() => { console.log('defined now!'); }); This targets one definition rather than waiting for Web Components across the page. section 14.2

Listing 14.1. WebComponentsReady event
<html>
<head>
   <title>Web Components Ready</title>
   <script src="https://unpkg.com/@webcomponents/webcomponentsjs@2.0.0/   #1
                webcomponents-loader.js"></script>
   <script>
       document.addEventListener(                                         #2
               'WebComponentsReady', function(e) {
           console.log('components ready');
       });

   </script>
</head>
</html>

The listing shows the polyfill script and the minimal event listener needed to observe WebComponentsReady. section 14.2

How Web Components in Action covers it

How deep it goes
Explains page-wide readiness, polyfill origin, and a component-specific alternative, but does not detail polyfill internals.
Examples
Uses a document event listener, a polyfill loader, a delayed custom-element definition, and a customElements.whenDefined promise.
What it assumes
Assumes familiarity with HTML, document event listeners, custom elements, and promises while introducing page readiness from the event model.
What you can do after
Apply WebComponentsReady in page code or component tests, and choose customElements.whenDefined for one component.
How it connects
Connects Web Components polyfills, custom elements, the Custom Element API, DOM events, and customElements.whenDefined.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.