Skip to content

Commit

Permalink
Support document rendering (#24523)
Browse files Browse the repository at this point in the history
* 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.

* Allow Document as container for createRoot

previously rendering into Document was broken and only hydration worked because React did not properly deal with the documentElement and would error in a broken state if used that way. With the previous commit addressing this limitation this change re-adds Document as a valid container for createRoot.

It should be noted that if you use document with createRoot it will drop anything a 3rd party scripts adds the page before rendering for the first time.
  • Loading branch information
gnoff committed May 10, 2022
1 parent d20c3af commit 8197c73
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/react-dom/client.js
Expand Up @@ -23,7 +23,7 @@ import {
} from './';

export function createRoot(
container: Element | DocumentFragment,
container: Element | Document | DocumentFragment,
options?: CreateRootOptions,
): RootType {
if (__DEV__) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/client/ReactDOM.js
Expand Up @@ -148,7 +148,7 @@ const Internals = {
};

function createRoot(
container: Element | DocumentFragment,
container: Element | Document | DocumentFragment,
options?: CreateRootOptions,
): RootType {
if (__DEV__) {
Expand Down
5 changes: 2 additions & 3 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Expand Up @@ -672,9 +672,8 @@ export function clearContainer(container: Container): void {
if (container.nodeType === ELEMENT_NODE) {
((container: any): Element).textContent = '';
} else if (container.nodeType === DOCUMENT_NODE) {
const body = ((container: any): Document).body;
if (body != null) {
body.textContent = '';
if (container.documentElement) {
container.removeChild(container.documentElement);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/client/ReactDOMRoot.js
Expand Up @@ -164,7 +164,7 @@ ReactDOMHydrationRoot.prototype.unmount = ReactDOMRoot.prototype.unmount = funct
};

export function createRoot(
container: Element | DocumentFragment,
container: Element | Document | DocumentFragment,
options?: CreateRootOptions,
): RootType {
if (!isValidContainer(container)) {
Expand Down

0 comments on commit 8197c73

Please sign in to comment.