Skip to content

Commit fd06131

Browse files
committed
Support Document as a container for hydration and rendering
Previously Document was not handled effectively as a container. in particual when hydrating if there was a fallback to client rendering React would attempt to append a new <html> element into the document before clearing out the existing one which errored leaving the application in brokent state. The initial approach I took was to recycle the documentElement and never remove or append it, always just moving it to the right fiber and appending the right children (heady/body) as needed. However in testing a simple approach in modern browsers it seems like treating the documentElement like any other element works fine. This change modifies the clearContainer method to remove the documentElement if the container is a DOCUMENT_NODE. Once the container is cleared React can append a new documentElement via normal means.
1 parent d20c3af commit fd06131

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

packages/react-dom/src/client/ReactDOMHostConfig.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,8 @@ export function clearContainer(container: Container): void {
672672
if (container.nodeType === ELEMENT_NODE) {
673673
((container: any): Element).textContent = '';
674674
} else if (container.nodeType === DOCUMENT_NODE) {
675-
const body = ((container: any): Document).body;
676-
if (body != null) {
677-
body.textContent = '';
675+
if (container.documentElement) {
676+
container.removeChild(container.documentElement);
678677
}
679678
}
680679
}

0 commit comments

Comments
 (0)