Re: [WICG/webcomponents] Preventing re-renders prior to first call to `connectedCallback()` (Issue #1081)

it's not just re-rendering though ... `connectedCallback` doesn't guarantee you can set `innerHTML` in the element because the element is still parsing and not necessarily closed, unless is one of those elements that has no content, still ...

```js
this.innerHTML = renderFunction.render(this.attributes)
```

this fails if your custom element declaration happens *before* the element is discovered/streamed in the DOM.

this works if your element is already live, meaning your declaration happened after the DOM was already parsed.

The latter point means you are using modules to define custom elements and modules play (as side-effect) better because they exec on `DOMContentLoaded` state of the dom, but if your definition is in a synchronous script tag before the rest of the DOM is parsed you'll have surprises there ... `innerHTML` can't be used.

This is the reason we've been asking a lot a way to have the possibility to operate on Custom Elements once their end tag is either reached or implicitly enforced by the parser, but the issue here is pretty normality from a parser point of view:

```html
<custom-element attr="1">
  Maybe content
</custom-element>
```

A parser would find the node, which is already connected, and it will start parsing `<custom-element attr="1">` which happens before the content of such node is even known.

Then it passes to its childNodes, if any, and right before that, it will trigger `connectedCallback` already, but the node is in a weird state:

  * it was already live, everything is fine, still the parser is making sense of it
  * it's just discovered, the parser at that point doesn't even know what's the node content, so `textContent` or `innerHTML` operations will be forbidden until these can operate

These are the basics behind Custom Elements to know though, otherwise much more issues could be found in the "shipping".

TL;DR is any callback except for `disconnectedCallback` reliable on current Custom Elements specification when it comes to infer the state of the node that is in the *discovery* phase rather than already *live*? **No**.



-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/1081#issuecomment-2419850303
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/issues/1081/2419850303@github.com>

Received on Thursday, 17 October 2024 15:23:33 UTC