Skip to content

Commit b5a3537

Browse files
committed
[Fizz] External runtime: fix bug in processing existing elements
1 parent e64a8f4 commit b5a3537

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

packages/react-dom-bindings/src/server/ReactDOMServerExternalRuntime.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,37 @@ if (!window.$RC) {
2121
window.$RM = new Map();
2222
}
2323

24-
if (document.readyState === 'loading') {
25-
if (document.body != null) {
24+
if (document.body != null) {
25+
if (document.readyState === 'loading') {
2626
installFizzInstrObserver(document.body);
27-
} else {
28-
// body may not exist yet if the fizz runtime is sent in <head>
29-
// (e.g. as a preinit resource)
30-
// $FlowFixMe[recursive-definition]
31-
const domBodyObserver = new MutationObserver(() => {
32-
// We expect the body node to be stable once parsed / created
33-
if (document.body) {
34-
if (document.readyState === 'loading') {
35-
installFizzInstrObserver(document.body);
36-
}
37-
handleExistingNodes();
38-
// We can call disconnect without takeRecord here,
39-
// since we only expect a single document.body
40-
domBodyObserver.disconnect();
41-
}
42-
});
43-
// documentElement must already exist at this point
44-
// $FlowFixMe[incompatible-call]
45-
domBodyObserver.observe(document.documentElement, {childList: true});
4627
}
47-
}
28+
// $FlowFixMe[incompatible-cast]
29+
handleExistingNodes((document.body /*: HTMLElement */));
30+
} else {
31+
// Document must be loading -- body may not exist yet if the fizz external
32+
// runtime is sent in <head> (e.g. as a preinit resource)
33+
// $FlowFixMe[recursive-definition]
34+
const domBodyObserver = new MutationObserver(() => {
35+
// We expect the body node to be stable once parsed / created
36+
if (document.body != null) {
37+
if (document.readyState === 'loading') {
38+
installFizzInstrObserver(document.body);
39+
}
40+
// $FlowFixMe[incompatible-cast]
41+
handleExistingNodes((document.body /*: HTMLElement */));
4842

49-
handleExistingNodes();
43+
// We can call disconnect without takeRecord here,
44+
// since we only expect a single document.body
45+
domBodyObserver.disconnect();
46+
}
47+
});
48+
// documentElement must already exist at this point
49+
// $FlowFixMe[incompatible-call]
50+
domBodyObserver.observe(document.documentElement, {childList: true});
51+
}
5052

51-
function handleExistingNodes() {
52-
const existingNodes = document.getElementsByTagName('template');
53+
function handleExistingNodes(target /*: HTMLElement */) {
54+
const existingNodes = Array.from(target.children);
5355
for (let i = 0; i < existingNodes.length; i++) {
5456
handleNode(existingNodes[i]);
5557
}

0 commit comments

Comments
 (0)