Repository: facebook/react
Pull request: [fizz][external-runtime] Fix: process mutation records before disconnecting

## Summary

> All notifications of mutations that have already been detected, but not yet reported to the observer, are discarded. To hold on to and handle the detected but unreported mutations, use the takeRecords() method.
>    -- ([Mozilla docs for disconnect](
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/disconnect))

Fizz external runtime needs to process mutation records (representing potential Fizz instructions) before calling `disconnect()`. We currently do not do this (and might drop some instructions).

<!--
  Thanks for submitting a pull request!
  We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory.

  Before submitting a pull request, please make sure the following is done:

  1. Fork [the repository](https://github.com/facebook/react) and create your branch from `main`.
  2. Run `yarn` in the repository root.
  3. If you've fixed a bug or added code that should be tested, add tests!
  4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch TestName` is helpful in development.
  5. Run `yarn test --prod` to test in the production environment. It supports the same options as `yarn test`.
  6. If you need a debugger, run `yarn debug-test --watch TestName`, open `chrome://inspect`, and press "Inspect".
  7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`).
  8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files.
  9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`).
  10. If you haven't already, complete the CLA.

  Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html
-->
<!--
 Explain the **motivation** for making this change. What existing problem does the pull request solve?
-->

## How did you test this change?

Not sure how to repro in a unit test.

Changed files:
filename: packages/react-dom-bindings/src/server/ReactDOMServerExternalRuntime.js
patch: @@ -35,6 +35,8 @@ if (document.readyState === 'loading') {
           installFizzInstrObserver(document.body);
         }
         handleExistingNodes();
+        // We can call disconnect without takeRecord here,
+        // since we only expect a single document.body
         domBodyObserver.disconnect();
       }
     });
@@ -54,7 +56,7 @@ function handleExistingNodes() {
 }
 
 function installFizzInstrObserver(target /*: Node */) {
-  const fizzInstrObserver = new MutationObserver(mutations => {
+  const handleMutations = (mutations /*: Array<MutationRecord> */) => {
     for (let i = 0; i < mutations.length; i++) {
       const addedNodes = mutations[i].addedNodes;
       for (let j = 0; j < addedNodes.length; j++) {
@@ -63,13 +65,16 @@ function installFizzInstrObserver(target /*: Node */) {
         }
       }
     }
-  });
+  };
+
+  const fizzInstrObserver = new MutationObserver(handleMutations);
   // We assume that instruction data nodes are eventually appended to the
   // body, even if Fizz is streaming to a shell / subtree.
   fizzInstrObserver.observe(target, {
     childList: true,
   });
   window.addEventListener('DOMContentLoaded', () => {
+    handleMutations(fizzInstrObserver.takeRecords());
     fizzInstrObserver.disconnect();
   });
 }