To answer my own question, I think it’s related to the DOM loading timing, as Matt hinted at here - ReferenceError: Can't find variable: simvar - #5 by mattnischan_xblms
So the solution is either to retry on a timer, or more nicely, use something like the document.readyState
and listen out for when it is ‘complete’ rather than just ‘interactive’ e.g. scripts are all loaded and ready to go (such as the togglebutton template etc). In a normal browser this would be the Windows.load event. Example:
connectedCallback() {
super.connectedCallback();
// will be 'interactive' initially as scripts etc loading
console.info("DOM loading?", document.readyState);
document.onreadystatechange = () => {
if (document.readyState === "complete") {
console.info("DOM loaded, complete", document.readyState);
// Do Stuff
}
};