I'm not into web-components, but I would say... not at all.
Your component is being defined by your script, but before it is, the browser will still parse the markup and execute all the synchronous scripts as usual, and fire the DOMContentLoaded when it's done.
So if you do define your CustomElements synchronously before the DOMContentLoaded event fired, then your elements connectedCallback
will have fired (because it's not an Event, it's a callback, and is called synchronously).
if (window.customElements) {
addEventListener('DOMContentLoaded', e => console.log('DOM loaded'));
class MyCustom extends HTMLElement {
connectedCallback() {
console.log('Custom element added to page.');
}
}
customElements.define('my-custom', MyCustom);
console.log('Just defined my custom element')
} else {
console.log("your browser doesn't have native support");
}
<my-custom></my-custom>
But if you do wait for the DOMContentLoaded event, then... the callbacks will fire after.
if (window.customElements) {
addEventListener('DOMContentLoaded', e => console.log('DOM loaded'));
class MyCustom extends HTMLElement {
connectedCallback() {
console.log('Custom element added to page.');
}
}
setTimeout(()=> customElements.define('my-custom', MyCustom), 2000);
} else {
console.log("your browser doesn't have native support");
}
<my-custom></my-custom>
But in no way is DOMContentLoaded waiting for anything else than the end of the synchronous execution of all the scripts, just like if you didn't had any CustomElement in there.
As to your last question, as said in the docs about the defer
attribute, the scripts with such an attribute will get parsed before the DOMContentLoaded fires.
defer
attribute, when set, is known to not work flawless cross browsers. – Crescent