remount icon indicating copy to clipboard operation
remount copied to clipboard

MutationObserver strategy fails when script included in <head>

Open edsrzf opened this issue 4 years ago • 1 comments

The MutationObserver strategy fails when the script defining elements is included inside the <head> tag, without using defer or async on the script tag. The error is:

Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'.

The site of the error is here: https://github.com/rstacruz/remount/blob/8bd440e5115161a292ba44c4f8befc39d1cba535/src/strategies/mutation_observer.js#L76-L79

The cause is that, inside <head> where the script is being evaluated, document.body does not yet exist.

Here is a repl.it link demonstrating the issue: https://repl.it/@EvanShaw2/Remount-MutationObserver-strategy-issue (This code forces all browsers to use the MutationObserver strategy, even if they support custom elements.)

It would be possible to get around this by calling Remount.define inside a jQuery $().ready event handler or equivalent native JS, which would ensure that document.body exists.

It would be nice if this limitation were documented. Even better if it were handled by remount itself.

edsrzf avatar Jun 22 '20 01:06 edsrzf

Interesting - maybe it can also observe document.body || document.documentElement instead 🤔

rstacruz avatar Apr 09 '22 14:04 rstacruz