Right now it's easy to assume that connectedCallback will call once the element has been fully populated, as this is how it'll work over a fast/local connection.
This means failures are likely to be seen intermittently in real-world conditions.
I'm not sure what the best answer is. Maybe connectedCallback could be called sync by the parser if the JS stack is empty. That means it'd consistently call before children have been added, regardless of timing. This means the no-children issue will show up in dev, and not be racey.
If you're setting innerHTML or building the DOM with JS, connectedCallback could continue to use a microtask.
This means we'd only be changing the behaviour of cases that are already potentially broken. We'd just be breaking them more consistently.
I'm wrong about the microtask thing. Closing this until I actually figure out what I'm on about.
We shouldn't modify the definition of connectedCallback because people are using it for the wrong purpose. (For the right purpose: see the various uses of "becomes connected" in the HTML Standard.)
The general problem here is people assuming there is ever any static time at which your list of children is "complete" or canonical. That is never true. Children can change dynamically. If you want to make decisions based on your children, you need to use a MutationObserver to observe them.
Correct @domenic. Was a "I don't have to like it to respect it" moment for me after reading the HTML Standard a little while back under similar situations.
Also @jakearchibald bringing up some (not so fond) memories of "wait what's going on?" with connectedCallback related to custom element child composition. Indeed Had to implement the stame strategy with a MutationObserver dependency. Things didn't start making since until I read the "becomes connected" portion of the standard. FWIW However what helped me was coming up with an automated suite (JSDOM helped here) to check as the different combinations of what we need satisfied. Manually testing etc. and my lack of understanding became a little like chasing kittens. More a personal problem for me than a real-world problem tho. YMMV.
Godspeed.
P.S. :heart: your HTTP/2 blog post. It's #SadButTrue 馃槃
Most helpful comment
We shouldn't modify the definition of connectedCallback because people are using it for the wrong purpose. (For the right purpose: see the various uses of "becomes connected" in the HTML Standard.)
The general problem here is people assuming there is ever any static time at which your list of children is "complete" or canonical. That is never true. Children can change dynamically. If you want to make decisions based on your children, you need to use a MutationObserver to observe them.