HTMLImports.whenReady is a callback API from the HTML Imports polyfill. It runs after imported resources are fully ready, allowing a Web Component to query its imported template, clone it, and append the clone. section 7.1
Populate an imported component
Inside connectedCallback(), pass a function to HTMLImports.whenReady. Query the template from the import's owner document, clone template.content with cloneNode(true), and append the clone to the component. section 7.1
Listing 7.3. Populating Web Component markup from an HTML Import template
class SampleComponent extends HTMLElement { #1
connectedCallback() {
HTMLImports.whenReady( () => {
const template = #2
ownerDoc.querySelector('template');
const clone = #3
template.content.cloneNode(true);
this.appendChild(clone); #4
});
}
}
const ownerDoc = #5
HTMLImports.importForElement(document.currentScript);
if (!customElements.get('sample-component')) {
customElements.define('sample-component', SampleComponent);
}
Inspect the complete callback flow, including owner-document lookup, template cloning, component definition, and insertion. section 7.1
