Playing around with streaming #HTMLFragments in #Firefox and discovered my whole demo doesn't work because of one small Firefox bug: It doesn't trigger `MutationObserver` on `document.write()` for an inner document. 😭

https://bugzilla.mozilla.org/show_bug.cgi?id=1811782

Hopefully it's something which can be fixed relative easily, but I'm worried it'll fall through the cracks as one of those "not that important because why would you ever do that" bugs.

1811782 - `document.write()` does not trigger `MutationObserver` of `document.implementation.createHTMLDocument()`

UNCONFIRMED (nobody) in Firefox - Untriaged. Last updated 2023-01-22.

So apparently #Firefox is at least partially right here. `document.write()` implicitly resets the document meaning `document.body` gets reset to `null` and recreated when #HTML is parsed.

My mistake was observing the `document.body` _before_ the reset, so content streams into a _new_ `<body />` element I'm not observing.

The solution is to `document.open()` explicitly to reset the document early, _and then_ observe `document.body`. Subsequent `document.write()` calls will append to that `<body />` tag and trigger mutations.

There's still some weird divergences between Firefox and #Chrome / #Safari which could probably be addressed, but I was able to make #HTMLFragments work at least.

https://github.com/dgp1130/html-fragments-demo/commit/97901e3d0ddaf38b4ef170d81719eddb52cab090

Shout out to Olli Pettay for identifying the issue so quickly!

Add Firefox support to streaming use case. · dgp1130/html-fragments-demo@97901e3

Firefox didn't work for streaming use cases and would never trigger the `MutationObserver`. This appears to be because `doc.write()` implicitly calls `doc.open()` if it wasn't explicitly ca...

GitHub