From 75a8cbf1cf59ad3e2ce4724b0dda7e1d4762d1d9 Mon Sep 17 00:00:00 2001
From: Josh Story
Date: Fri, 24 Jan 2025 09:20:23 -0800
Subject: [PATCH] [Fiber] support hydration when rendering Suspense anywhere
stacked on https://github.com/facebook/react/pull/32163
This continues the work of making Suspense workable anywhere in a react-dom tree. See the prior PRs for how we handle server rendering and client rendering. In this change we update the hydration implementation to be able to locate expected nodes. In particular this means hydration understands now that the default hydration context is the document body when the container is above the body.
One case that is unique to hydration is clearing Suspense boundaries. When hydration fails or when the server instructs the client to recover an errored boundary it's possible that the html, head, and body tags in the initial document were written from a fallback or a different primary content on the server and need to be replaced by the client render. However these tags (and in the case of head, their content) won't be inside the comment nodes that identify the bounds of the Suspense boundary. And when client rendering you may not even render the same singletons that were server rendered. So when server rendering a boudnary which contributes to the preamble (the html, head, and body tag openings plus the head contents) we emit a special marker comment just before closing the boundary out. This marker encodes which parts of the preamble this boundary owned. If we need to clear the suspense boundary on the client we read this marker and use it to reset the appropriate singleton state.
---
.../src/client/ReactFiberConfigDOM.js | 108 +++-
.../src/server/ReactFizzConfigDOM.js | 112 +++-
.../src/server/ReactFizzConfigDOMLegacy.js | 14 +-
.../src/__tests__/ReactDOMFizzServer-test.js | 588 +++++++++++++++++-
...ctDOMServerIntegrationUntrustedURL-test.js | 6 +
.../react-markup/src/ReactFizzConfigMarkup.js | 2 +
.../src/ReactFiberCommitHostEffects.js | 1 +
.../src/ReactFiberConfigWithNoHydration.js | 2 +
.../src/ReactFiberHydrationContext.js | 59 +-
.../src/forks/ReactFiberConfig.custom.js | 4 +
packages/react-server/src/ReactFizzServer.js | 7 +-
11 files changed, 863 insertions(+), 40 deletions(-)
diff --git a/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js b/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
index 042d6897c6aae..26f1a2d0ef076 100644
--- a/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
+++ b/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
@@ -206,6 +206,11 @@ const SUSPENSE_START_DATA = '$';
const SUSPENSE_END_DATA = '/$';
const SUSPENSE_PENDING_START_DATA = '$?';
const SUSPENSE_FALLBACK_START_DATA = '$!';
+const PREAMBLE_CONTRIBUTION_MARKER = 'P';
+const PREAMBLE_CONTRIBUTION_INDICATOR = '|';
+const HTML_CONTRIBUTION_INDEX = 1;
+const BODY_CONTRIBUTION_INDEX = 2;
+const HEAD_CONTRIBUTION_INDEX = 3;
const FORM_STATE_IS_MATCHING = 'F!';
const FORM_STATE_IS_NOT_MATCHING = 'F';
@@ -986,6 +991,37 @@ export function clearSuspenseBoundary(
data === SUSPENSE_FALLBACK_START_DATA
) {
depth++;
+ } else if (data[0] === PREAMBLE_CONTRIBUTION_MARKER) {
+ const ownerDocument = parentInstance.ownerDocument;
+
+ // If this comment is too short or if this slot is an empty space (the indicator)
+ // Then this boundary DID contribute the singleton component. It is constructed this way
+ // to minimize the contribution data size for common contributions such as None (no comment)
+ // and All (). Less common contributions such as Body only would be ()
+ if (
+ (data[HTML_CONTRIBUTION_INDEX] || PREAMBLE_CONTRIBUTION_INDICATOR) ===
+ PREAMBLE_CONTRIBUTION_INDICATOR
+ ) {
+ const documentElement: Element = (ownerDocument.documentElement: any);
+ releaseSingletonInstance(documentElement);
+ }
+ if (
+ (data[BODY_CONTRIBUTION_INDEX] || PREAMBLE_CONTRIBUTION_INDICATOR) ===
+ PREAMBLE_CONTRIBUTION_INDICATOR
+ ) {
+ const body: Element = (ownerDocument.body: any);
+ releaseSingletonInstance(body);
+ }
+ if (
+ (data[HEAD_CONTRIBUTION_INDEX] || PREAMBLE_CONTRIBUTION_INDICATOR) ===
+ PREAMBLE_CONTRIBUTION_INDICATOR
+ ) {
+ const head: Element = (ownerDocument.head: any);
+ releaseSingletonInstance(head);
+ // We need to clear the head because this is the only singleton that can have children that
+ // were part of this boundary but are not inside this boundary.
+ clearHead(head);
+ }
}
}
// $FlowFixMe[incompatible-type] we bail out when we get a null
@@ -1499,7 +1535,7 @@ function clearContainerSparingly(container: Node) {
case 'STYLE': {
continue;
}
- // Stylesheet tags are retained because tehy may likely come from 3rd party scripts and extensions
+ // Stylesheet tags are retained because they may likely come from 3rd party scripts and extensions
case 'LINK': {
if (((node: any): HTMLLinkElement).rel.toLowerCase() === 'stylesheet') {
continue;
@@ -1511,6 +1547,27 @@ function clearContainerSparingly(container: Node) {
return;
}
+function clearHead(head: Element): void {
+ let node = head.firstChild;
+ while (node) {
+ const nextNode = node.nextSibling;
+ const nodeName = node.nodeName;
+ if (
+ isMarkedHoistable(node) ||
+ nodeName === 'SCRIPT' ||
+ nodeName === 'STYLE' ||
+ (nodeName === 'LINK' &&
+ ((node: any): HTMLLinkElement).rel.toLowerCase() === 'stylesheet')
+ ) {
+ // retain these nodes
+ } else {
+ head.removeChild(node);
+ }
+ node = nextNode;
+ }
+ return;
+}
+
// Making this so we can eventually move all of the instance caching to the commit phase.
// Currently this is only used to associate fiber and props to instances for hydrating
// HostSingletons. The reason we need it here is we only want to make this binding on commit
@@ -1872,7 +1929,20 @@ export function getFirstHydratableChild(
export function getFirstHydratableChildWithinContainer(
parentContainer: Container,
): null | HydratableInstance {
- return getNextHydratable(parentContainer.firstChild);
+ let parentElement: Element;
+ switch (parentContainer.nodeType) {
+ case DOCUMENT_NODE:
+ parentElement = (parentContainer: any).body;
+ break;
+ default: {
+ if (parentContainer.nodeName === 'HTML') {
+ parentElement = (parentContainer: any).ownerDocument.body;
+ } else {
+ parentElement = (parentContainer: any);
+ }
+ }
+ }
+ return getNextHydratable(parentElement.firstChild);
}
export function getFirstHydratableChildWithinSuspenseInstance(
@@ -1881,6 +1951,40 @@ export function getFirstHydratableChildWithinSuspenseInstance(
return getNextHydratable(parentInstance.nextSibling);
}
+// If it were possible to have more than one scope singleton in a DOM tree
+// we would need to model this as a stack but since you can only have one
+// and head is the only singleton that is a scope in DOM we can get away with
+// tracking this as a single value.
+let previousHydratableOnEnteringScopedSingleton: null | HydratableInstance =
+ null;
+
+export function getFirstHydratableChildWithinSingleton(
+ type: string,
+ singletonInstance: Instance,
+ currentHydratableInstance: null | HydratableInstance,
+): null | HydratableInstance {
+ if (isSingletonScope(type)) {
+ previousHydratableOnEnteringScopedSingleton = currentHydratableInstance;
+ return getNextHydratable(singletonInstance.firstChild);
+ } else {
+ return currentHydratableInstance;
+ }
+}
+
+export function getNextHydratableSiblingAfterSingleton(
+ type: string,
+ currentHydratableInstance: null | HydratableInstance,
+): null | HydratableInstance {
+ if (isSingletonScope(type)) {
+ const previousHydratableInstance =
+ previousHydratableOnEnteringScopedSingleton;
+ previousHydratableOnEnteringScopedSingleton = null;
+ return previousHydratableInstance;
+ } else {
+ return currentHydratableInstance;
+ }
+}
+
export function describeHydratableInstanceForDevWarnings(
instance: HydratableInstance,
): string | {type: string, props: $ReadOnly} {
diff --git a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
index 4b8841a06e67b..28c0a498ff4ba 100644
--- a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
+++ b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
@@ -684,16 +684,25 @@ export function completeResumableState(resumableState: ResumableState): void {
resumableState.bootstrapModules = undefined;
}
+const NoContribution /* */ = 0b000;
+const HTMLContribution /* */ = 0b001;
+const BodyContribution /* */ = 0b010;
+const HeadContribution /* */ = 0b100;
+const TotalContribution =
+ HTMLContribution | HeadContribution | BodyContribution;
+
export type PreambleState = {
htmlChunks: null | Array,
headChunks: null | Array,
bodyChunks: null | Array,
+ contribution: number,
};
export function createPreambleState(): PreambleState {
return {
htmlChunks: null,
headChunks: null,
bodyChunks: null,
+ contribution: NoContribution,
};
}
@@ -3227,7 +3236,7 @@ function pushStartHead(
throw new Error(`The ${'``'} tag may only be rendered once.`);
}
preamble.headChunks = [];
- return pushStartGenericElement(preamble.headChunks, props, 'head');
+ return pushStartSingletonElement(preamble.headChunks, props, 'head');
} else {
// This is deep and is likely just an error. we emit it inline though.
// Validation should warn that this tag is the the wrong spot.
@@ -3251,7 +3260,7 @@ function pushStartBody(
}
preamble.bodyChunks = [];
- return pushStartGenericElement(preamble.bodyChunks, props, 'body');
+ return pushStartSingletonElement(preamble.bodyChunks, props, 'body');
} else {
// This is deep and is likely just an error. we emit it inline though.
// Validation should warn that this tag is the the wrong spot.
@@ -3275,7 +3284,7 @@ function pushStartHtml(
}
preamble.htmlChunks = [DOCTYPE];
- return pushStartGenericElement(preamble.htmlChunks, props, 'html');
+ return pushStartSingletonElement(preamble.htmlChunks, props, 'html');
} else {
// This is deep and is likely just an error. we emit it inline though.
// Validation should warn that this tag is the the wrong spot.
@@ -3416,6 +3425,43 @@ function pushScriptImpl(
return null;
}
+// This is a fork of pushStartGenericElement because we don't ever want to do
+// the children as strign optimization on that path when rendering singletons.
+// When we eliminate that special path we can delete this fork and unify it again
+function pushStartSingletonElement(
+ target: Array,
+ props: Object,
+ tag: string,
+): ReactNodeList {
+ target.push(startChunkForTag(tag));
+
+ let children = null;
+ let innerHTML = null;
+ for (const propKey in props) {
+ if (hasOwnProperty.call(props, propKey)) {
+ const propValue = props[propKey];
+ if (propValue == null) {
+ continue;
+ }
+ switch (propKey) {
+ case 'children':
+ children = propValue;
+ break;
+ case 'dangerouslySetInnerHTML':
+ innerHTML = propValue;
+ break;
+ default:
+ pushAttribute(target, propKey, propValue);
+ break;
+ }
+ }
+ }
+
+ target.push(endOfStartTag);
+ pushInnerHTML(target, innerHTML, children);
+ return children;
+}
+
function pushStartGenericElement(
target: Array,
props: Object,
@@ -3907,14 +3953,17 @@ export function hoistPreambleState(
preambleState: PreambleState,
) {
const rootPreamble = renderState.preamble;
- if (rootPreamble.htmlChunks === null) {
+ if (rootPreamble.htmlChunks === null && preambleState.htmlChunks) {
rootPreamble.htmlChunks = preambleState.htmlChunks;
+ preambleState.contribution |= HTMLContribution;
}
- if (rootPreamble.headChunks === null) {
+ if (rootPreamble.headChunks === null && preambleState.headChunks) {
rootPreamble.headChunks = preambleState.headChunks;
+ preambleState.contribution |= HeadContribution;
}
- if (rootPreamble.bodyChunks === null) {
+ if (rootPreamble.bodyChunks === null && preambleState.bodyChunks) {
rootPreamble.bodyChunks = preambleState.bodyChunks;
+ preambleState.contribution |= BodyContribution;
}
}
@@ -4005,6 +4054,21 @@ const clientRenderedSuspenseBoundaryError1D =
const clientRenderedSuspenseBoundaryError2 =
stringToPrecomputedChunk('>');
+const boundaryPreambleContributionChunkTotal =
+ stringToPrecomputedChunk('');
+const boundaryPreambleContributionChunkHTMLOnly =
+ stringToPrecomputedChunk('');
+const boundaryPreambleContributionChunkBodyOnly =
+ stringToPrecomputedChunk('');
+const boundaryPreambleContributionChunkHeadOnly =
+ stringToPrecomputedChunk('');
+const boundaryPreambleContributionChunkHTMLAndBody =
+ stringToPrecomputedChunk('');
+const boundaryPreambleContributionChunkHTMLAndHead =
+ stringToPrecomputedChunk('');
+const boundaryPreambleContributionChunkHeadAndBody =
+ stringToPrecomputedChunk('');
+
export function writeStartCompletedSuspenseBoundary(
destination: Destination,
renderState: RenderState,
@@ -4091,7 +4155,11 @@ export function writeStartClientRenderedSuspenseBoundary(
export function writeEndCompletedSuspenseBoundary(
destination: Destination,
renderState: RenderState,
+ preambleState: null | PreambleState,
): boolean {
+ if (preambleState) {
+ writePreambleContribution(destination, preambleState);
+ }
return writeChunkAndReturn(destination, endSuspenseBoundary);
}
export function writeEndPendingSuspenseBoundary(
@@ -4103,9 +4171,41 @@ export function writeEndPendingSuspenseBoundary(
export function writeEndClientRenderedSuspenseBoundary(
destination: Destination,
renderState: RenderState,
+ preambleState: null | PreambleState,
): boolean {
+ if (preambleState) {
+ writePreambleContribution(destination, preambleState);
+ }
return writeChunkAndReturn(destination, endSuspenseBoundary);
}
+function writePreambleContribution(
+ destination: Destination,
+ preambleState: PreambleState,
+) {
+ const contribution = preambleState.contribution;
+ switch (contribution) {
+ case TotalContribution:
+ writeChunk(destination, boundaryPreambleContributionChunkTotal);
+ break;
+ case HeadContribution | BodyContribution:
+ writeChunk(destination, boundaryPreambleContributionChunkHeadAndBody);
+ break;
+ case HTMLContribution | HeadContribution:
+ writeChunk(destination, boundaryPreambleContributionChunkHTMLAndHead);
+ break;
+ case HTMLContribution | BodyContribution:
+ writeChunk(destination, boundaryPreambleContributionChunkHTMLAndBody);
+ break;
+ case HeadContribution:
+ writeChunk(destination, boundaryPreambleContributionChunkHeadOnly);
+ break;
+ case BodyContribution:
+ writeChunk(destination, boundaryPreambleContributionChunkBodyOnly);
+ break;
+ case HTMLContribution:
+ writeChunk(destination, boundaryPreambleContributionChunkHTMLOnly);
+ }
+}
const startSegmentHTML = stringToPrecomputedChunk('
+
+
+ ,
+ );
+ if (gate(flags => flags.enableOwnerStacks)) {
+ assertConsoleErrorDev([
+ [
+ 'Cannot render a outside the main document if it has an `itemProp` prop. `itemProp` suggests the tag belongs to an `itemScope` which can appear anywhere in the DOM. If you were intending for React to hoist this remove the `itemProp` prop. Otherwise, try moving this tag into the or of the Document.',
+ {withoutStack: true},
+ ],
+ 'In HTML, cannot be a child of .\nThis will cause a hydration error.' +
+ '\n' +
+ '\n ' +
+ '\n> ' +
+ '\n ' +
+ '\n ' +
+ '\n> ' +
+ '\n ...' +
+ '\n' +
+ '\n in meta (at **)' +
+ '\n in App (at **)',
+ ' cannot contain a nested .\nSee this log for the ancestor stack trace.' +
+ '\n in html (at **)' +
+ '\n in App (at **)',
+ [
+ 'Cannot render a outside the main document if it has an `itemProp` prop. `itemProp` suggests the tag belongs to an `itemScope` which can appear anywhere in the DOM. If you were intending for React to hoist this remove the `itemProp` prop. Otherwise, try moving this tag into the or of the Document.',
+ {withoutStack: true},
+ ],
+ ]);
+ } else {
+ assertConsoleErrorDev([
+ 'Cannot render a outside the main document if it has an `itemProp` prop. `itemProp` suggests the tag belongs to an `itemScope` which can appear anywhere in the DOM. If you were intending for React to hoist this remove the `itemProp` prop. Otherwise, try moving this tag into the or of the Document.' +
+ '\n in Suspense (at **)' +
+ '\n in html (at **)' +
+ '\n in App (at **)',
+ 'In HTML, cannot be a child of .\nThis will cause a hydration error.' +
+ '\n' +
+ '\n ' +
+ '\n> ' +
+ '\n ' +
+ '\n ' +
+ '\n> ' +
+ '\n ...' +
+ '\n' +
+ '\n in meta (at **)' +
+ '\n in Suspense (at **)' +
+ '\n in html (at **)' +
+ '\n in App (at **)',
+ 'Cannot render a outside the main document if it has an `itemProp` prop. `itemProp` suggests the tag belongs to an `itemScope` which can appear anywhere in the DOM. If you were intending for React to hoist this remove the `itemProp` prop. Otherwise, try moving this tag into the or of the Document.' +
+ '\n in Suspense (at **)' +
+ '\n in html (at **)' +
+ '\n in App (at **)',
+ ]);
+ }
+
+ await root.unmount();
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+ ,
+ );
});
- it('can server render Suspense before, after, and around ', async () => {
+ it('can render Suspense before, after, and around ', async () => {
function BlockedOn({value, children}) {
readText(value);
return children;
@@ -9119,11 +9217,90 @@ describe('ReactDOMFizzServer', () => {
,
);
+
+ const root = ReactDOMClient.hydrateRoot(document, );
+ await waitForAll([]);
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+
+
+
+
+
+
+
hello world
+
+ ,
+ );
+ if (gate(flags => flags.enableOwnerStacks)) {
+ assertConsoleErrorDev([
+ [
+ 'Cannot render a outside the main document if it has an `itemProp` prop. `itemProp` suggests the tag belongs to an `itemScope` which can appear anywhere in the DOM. If you were intending for React to hoist this remove the `itemProp` prop. Otherwise, try moving this tag into the or of the Document.',
+ {withoutStack: true},
+ ],
+ 'In HTML, cannot be a child of .\nThis will cause a hydration error.' +
+ '\n' +
+ '\n ' +
+ '\n> ' +
+ '\n ' +
+ '\n ' +
+ '\n> ' +
+ '\n ...' +
+ '\n' +
+ '\n in meta (at **)' +
+ '\n in App (at **)',
+ ' cannot contain a nested .\nSee this log for the ancestor stack trace.' +
+ '\n in html (at **)' +
+ '\n in App (at **)',
+ [
+ 'Cannot render a outside the main document if it has an `itemProp` prop. `itemProp` suggests the tag belongs to an `itemScope` which can appear anywhere in the DOM. If you were intending for React to hoist this remove the `itemProp` prop. Otherwise, try moving this tag into the or of the Document.',
+ {withoutStack: true},
+ ],
+ ]);
+ } else {
+ assertConsoleErrorDev([
+ 'Cannot render a outside the main document if it has an `itemProp` prop. `itemProp` suggests the tag belongs to an `itemScope` which can appear anywhere in the DOM. If you were intending for React to hoist this remove the `itemProp` prop. Otherwise, try moving this tag into the or of the Document.' +
+ '\n in Suspense (at **)' +
+ '\n in html (at **)' +
+ '\n in App (at **)',
+ 'In HTML, cannot be a child of .\nThis will cause a hydration error.' +
+ '\n' +
+ '\n ' +
+ '\n> ' +
+ '\n ' +
+ '\n ' +
+ '\n> ' +
+ '\n ...' +
+ '\n' +
+ '\n in meta (at **)' +
+ '\n in Suspense (at **)' +
+ '\n in html (at **)' +
+ '\n in App (at **)',
+ 'Cannot render a outside the main document if it has an `itemProp` prop. `itemProp` suggests the tag belongs to an `itemScope` which can appear anywhere in the DOM. If you were intending for React to hoist this remove the `itemProp` prop. Otherwise, try moving this tag into the or of the Document.' +
+ '\n in Suspense (at **)' +
+ '\n in html (at **)' +
+ '\n in App (at **)',
+ ]);
+ }
+
+ await root.unmount();
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+ ,
+ );
});
- it('will render fallback Document when erroring a boundary above the body', async () => {
+ it('will render fallback Document when erroring a boundary above the body and recover on the client', async () => {
+ let serverRendering = true;
function Boom() {
- throw new Error('Boom!');
+ if (serverRendering) {
+ throw new Error('Boom!');
+ }
+ return null;
}
function App() {
@@ -9174,11 +9351,50 @@ describe('ReactDOMFizzServer', () => {
,
);
+
+ serverRendering = false;
+
+ const recoverableErrors = [];
+ const root = ReactDOMClient.hydrateRoot(document, , {
+ onRecoverableError(err) {
+ recoverableErrors.push(err);
+ },
+ });
+ await waitForAll([]);
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+ hello world
+
+ ,
+ );
+ expect(recoverableErrors).toEqual([
+ __DEV__
+ ? new Error(
+ 'Switched to client rendering because the server rendering errored:\n\nBoom!',
+ )
+ : new Error(
+ 'The server could not finish this Suspense boundary, likely due to an error during server rendering. Switched to client rendering.',
+ ),
+ ]);
+
+ root.unmount();
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+ ,
+ );
});
it('will hoist resources and hositables from a primary tree into the of a client rendered fallback', async () => {
+ let serverRendering = true;
function Boom() {
- throw new Error('Boom!');
+ if (serverRendering) {
+ throw new Error('Boom!');
+ }
+ return null;
}
function App() {
@@ -9255,6 +9471,65 @@ describe('ReactDOMFizzServer', () => {
,
);
+
+ serverRendering = false;
+
+ const recoverableErrors = [];
+ const root = ReactDOMClient.hydrateRoot(document, , {
+ onRecoverableError(err) {
+ recoverableErrors.push(err);
+ },
+ });
+ await waitForAll([]);
+ expect(recoverableErrors).toEqual([
+ __DEV__
+ ? new Error(
+ 'Switched to client rendering because the server rendering errored:\n\nBoom!',
+ )
+ : new Error(
+ 'The server could not finish this Suspense boundary, likely due to an error during server rendering. Switched to client rendering.',
+ ),
+ ]);
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+
+
+
+
+
+ hello world
+
+ ,
+ );
+
+ root.unmount();
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+
+
+
+ ,
+ );
});
it('Will wait to flush Document chunks until all boundaries which might contain a preamble are errored or resolved', async () => {
@@ -9353,8 +9628,12 @@ describe('ReactDOMFizzServer', () => {
});
it('Can render a fallback alongside a non-fallback body', async () => {
+ let serverRendering = true;
function Boom() {
- throw new Error('Boom!');
+ if (serverRendering) {
+ throw new Error('Boom!');
+ }
+ return null;
}
function App() {
@@ -9416,11 +9695,52 @@ describe('ReactDOMFizzServer', () => {
,
);
+
+ serverRendering = false;
+
+ const recoverableErrors = [];
+ const root = ReactDOMClient.hydrateRoot(document, , {
+ onRecoverableError(err) {
+ recoverableErrors.push(err);
+ },
+ });
+ await waitForAll([]);
+ expect(recoverableErrors).toEqual([
+ __DEV__
+ ? new Error(
+ 'Switched to client rendering because the server rendering errored:\n\nBoom!',
+ )
+ : new Error(
+ 'The server could not finish this Suspense boundary, likely due to an error during server rendering. Switched to client rendering.',
+ ),
+ ]);
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+
+
+
cannot be a child of <#document>']);
+
+ root.unmount();
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+ ,
+ );
});
it('will flush the preamble as soon as a complete preamble is available', async () => {
@@ -9740,5 +10140,177 @@ describe('ReactDOMFizzServer', () => {
,
);
+
+ const root = ReactDOMClient.hydrateRoot(document, );
+ await waitForAll([]);
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+
+
+ ,
+ );
+ });
+
+ it('will clean up the head when a hydration mismatch causes a boundary to recover on the client', async () => {
+ let content = 'server';
+
+ function ServerApp() {
+ return (
+
+
+
+
+
+
{content}
+
+
+ );
+ }
+
+ function ClientApp() {
+ return (
+
+
+
+
+
+ {content}
+
+
+ );
+ }
+
+ await act(() => {
+ const {pipe} = renderToPipeableStream();
+ pipe(writable);
+ });
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+
+ server
+ ,
+ );
+
+ content = 'client';
+
+ const recoverableErrors = [];
+ const root = ReactDOMClient.hydrateRoot(document, , {
+ onRecoverableError(err) {
+ recoverableErrors.push(err.message);
+ },
+ });
+ await waitForAll([]);
+ if (gate(flags => flags.favorSafetyOverHydrationPerf)) {
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+
+ client
+ ,
+ );
+ expect(recoverableErrors).toEqual([
+ expect.stringContaining(
+ "Hydration failed because the server rendered HTML didn't match the client.",
+ ),
+ ]);
+ } else {
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+
+ server
+ ,
+ );
+ expect(recoverableErrors).toEqual([]);
+ assertConsoleErrorDev([
+ "A tree hydrated but some attributes of the server rendered HTML didn't match the client properties. This won't be patched up. This can happen if a SSR-ed Client Component used:" +
+ '\n' +
+ "\n- A server/client branch `if (typeof window !== 'undefined')`." +
+ "\n- Variable input such as `Date.now()` or `Math.random()` which changes each time it's called." +
+ "\n- Date formatting in a user's locale which doesn't match the server." +
+ '\n- External changing data without sending a snapshot of it along with the HTML.' +
+ '\n- Invalid HTML tag nesting.' +
+ '\n' +
+ '\nIt can also happen if the client has a browser extension installed which messes with the HTML before React loaded.' +
+ '\n' +
+ '\nhttps://react.dev/link/hydration-mismatch' +
+ '\n' +
+ '\n ' +
+ '\n ' +
+ '\n ' +
+ '\n ' +
+ '\n ' +
+ '\n ' +
+ '\n+ client' +
+ '\n- server' +
+ '\n+ client' +
+ '\n- server' +
+ '\n' +
+ '\n in Suspense (at **)' +
+ '\n in ClientApp (at **)',
+ ]);
+ }
+
+ root.unmount();
+ expect(getVisibleChildren(document)).toEqual(
+
+
+
+ ,
+ );
});
});
diff --git a/packages/react-dom/src/__tests__/ReactDOMServerIntegrationUntrustedURL-test.js b/packages/react-dom/src/__tests__/ReactDOMServerIntegrationUntrustedURL-test.js
index 384e8beb0b214..8664904130c11 100644
--- a/packages/react-dom/src/__tests__/ReactDOMServerIntegrationUntrustedURL-test.js
+++ b/packages/react-dom/src/__tests__/ReactDOMServerIntegrationUntrustedURL-test.js
@@ -41,6 +41,7 @@ describe('ReactDOMServerIntegration - Untrusted URLs', () => {
const {
resetModules,
itRenders,
+ clientCleanRender,
clientRenderOnBadMarkup,
clientRenderOnServerString,
} = ReactDOMServerIntegrationUtils(initModules);
@@ -141,6 +142,11 @@ describe('ReactDOMServerIntegration - Untrusted URLs', () => {
});
itRenders('a javascript protocol frame src', async render => {
+ if (render === clientCleanRender || render === clientRenderOnServerString) {
+ // React does not hydrate framesets properly because the default hydration scope
+ // is the body
+ return;
+ }
const e = await render(
diff --git a/packages/react-markup/src/ReactFizzConfigMarkup.js b/packages/react-markup/src/ReactFizzConfigMarkup.js
index 99e9921c8190a..358a08e7c54b8 100644
--- a/packages/react-markup/src/ReactFizzConfigMarkup.js
+++ b/packages/react-markup/src/ReactFizzConfigMarkup.js
@@ -174,6 +174,7 @@ export function writeStartClientRenderedSuspenseBoundary(
export function writeEndCompletedSuspenseBoundary(
destination: Destination,
renderState: RenderState,
+ preambleState: null | PreambleState,
): boolean {
// Markup doesn't have any instructions.
return true;
@@ -181,6 +182,7 @@ export function writeEndCompletedSuspenseBoundary(
export function writeEndClientRenderedSuspenseBoundary(
destination: Destination,
renderState: RenderState,
+ preambleState: null | PreambleState,
): boolean {
// Markup doesn't have any instructions.
return true;
diff --git a/packages/react-reconciler/src/ReactFiberCommitHostEffects.js b/packages/react-reconciler/src/ReactFiberCommitHostEffects.js
index 159c12bd4bcf2..c104c2a8464b5 100644
--- a/packages/react-reconciler/src/ReactFiberCommitHostEffects.js
+++ b/packages/react-reconciler/src/ReactFiberCommitHostEffects.js
@@ -314,6 +314,7 @@ function insertOrAppendPlacementNodeIntoContainer(
// This singleton is the parent of deeper nodes and needs to become
// the parent for child insertions and appends
parent = node.stateNode;
+ before = null;
}
const child = node.child;
diff --git a/packages/react-reconciler/src/ReactFiberConfigWithNoHydration.js b/packages/react-reconciler/src/ReactFiberConfigWithNoHydration.js
index 3707f99f488fb..0bb85246dfe24 100644
--- a/packages/react-reconciler/src/ReactFiberConfigWithNoHydration.js
+++ b/packages/react-reconciler/src/ReactFiberConfigWithNoHydration.js
@@ -28,9 +28,11 @@ export const registerSuspenseInstanceRetry = shim;
export const canHydrateFormStateMarker = shim;
export const isFormStateMarkerMatching = shim;
export const getNextHydratableSibling = shim;
+export const getNextHydratableSiblingAfterSingleton = shim;
export const getFirstHydratableChild = shim;
export const getFirstHydratableChildWithinContainer = shim;
export const getFirstHydratableChildWithinSuspenseInstance = shim;
+export const getFirstHydratableChildWithinSingleton = shim;
export const canHydrateInstance = shim;
export const canHydrateTextInstance = shim;
export const canHydrateSuspenseInstance = shim;
diff --git a/packages/react-reconciler/src/ReactFiberHydrationContext.js b/packages/react-reconciler/src/ReactFiberHydrationContext.js
index b4d948e735276..c0df597b5ac50 100644
--- a/packages/react-reconciler/src/ReactFiberHydrationContext.js
+++ b/packages/react-reconciler/src/ReactFiberHydrationContext.js
@@ -37,9 +37,11 @@ import {
supportsHydration,
supportsSingletons,
getNextHydratableSibling,
+ getNextHydratableSiblingAfterSingleton,
getFirstHydratableChild,
getFirstHydratableChildWithinContainer,
getFirstHydratableChildWithinSuspenseInstance,
+ getFirstHydratableChildWithinSingleton,
hydrateInstance,
diffHydratedPropsForDevWarnings,
describeHydratableInstanceForDevWarnings,
@@ -366,7 +368,11 @@ function claimHydratableSingleton(fiber: Fiber): void {
hydrationParentFiber = fiber;
rootOrSingletonContext = true;
- nextHydratableInstance = getFirstHydratableChild(instance);
+ nextHydratableInstance = getFirstHydratableChildWithinSingleton(
+ fiber.type,
+ instance,
+ nextHydratableInstance,
+ );
}
}
@@ -593,14 +599,14 @@ function popToNextHostParent(fiber: Fiber): void {
hydrationParentFiber = fiber.return;
while (hydrationParentFiber) {
switch (hydrationParentFiber.tag) {
- case HostRoot:
- case HostSingleton:
- rootOrSingletonContext = true;
- return;
case HostComponent:
case SuspenseComponent:
rootOrSingletonContext = false;
return;
+ case HostSingleton:
+ case HostRoot:
+ rootOrSingletonContext = true;
+ return;
default:
hydrationParentFiber = hydrationParentFiber.return;
}
@@ -625,7 +631,6 @@ function popHydrationState(fiber: Fiber): boolean {
return false;
}
- let shouldClear = false;
if (supportsSingletons) {
// With float we never clear the Root, or Singleton instances. We also do not clear Instances
// that have singleton text content
@@ -638,7 +643,11 @@ function popHydrationState(fiber: Fiber): boolean {
shouldSetTextContent(fiber.type, fiber.memoizedProps))
)
) {
- shouldClear = true;
+ const nextInstance = nextHydratableInstance;
+ if (nextInstance) {
+ warnIfUnhydratedTailNodes(fiber);
+ throwOnHydrationMismatch(fiber);
+ }
}
} else {
// If we have any remaining hydratable nodes, we need to delete them now.
@@ -651,23 +660,31 @@ function popHydrationState(fiber: Fiber): boolean {
(shouldDeleteUnhydratedTailInstances(fiber.type) &&
!shouldSetTextContent(fiber.type, fiber.memoizedProps)))
) {
- shouldClear = true;
- }
- }
- if (shouldClear) {
- const nextInstance = nextHydratableInstance;
- if (nextInstance) {
- warnIfUnhydratedTailNodes(fiber);
- throwOnHydrationMismatch(fiber);
+ const nextInstance = nextHydratableInstance;
+ if (nextInstance) {
+ warnIfUnhydratedTailNodes(fiber);
+ throwOnHydrationMismatch(fiber);
+ }
}
}
popToNextHostParent(fiber);
- if (fiber.tag === SuspenseComponent) {
- nextHydratableInstance = skipPastDehydratedSuspenseInstance(fiber);
- } else {
- nextHydratableInstance = hydrationParentFiber
- ? getNextHydratableSibling(fiber.stateNode)
- : null;
+ switch (fiber.tag) {
+ case SuspenseComponent:
+ nextHydratableInstance = skipPastDehydratedSuspenseInstance(fiber);
+ break;
+ case HostSingleton:
+ if (supportsSingletons) {
+ nextHydratableInstance = getNextHydratableSiblingAfterSingleton(
+ fiber.type,
+ nextHydratableInstance,
+ );
+ break;
+ }
+ // Fallthrough
+ default:
+ nextHydratableInstance = hydrationParentFiber
+ ? getNextHydratableSibling(fiber.stateNode)
+ : null;
}
return true;
}
diff --git a/packages/react-reconciler/src/forks/ReactFiberConfig.custom.js b/packages/react-reconciler/src/forks/ReactFiberConfig.custom.js
index 91420ca88cd95..ee5f40ad829ad 100644
--- a/packages/react-reconciler/src/forks/ReactFiberConfig.custom.js
+++ b/packages/react-reconciler/src/forks/ReactFiberConfig.custom.js
@@ -174,11 +174,15 @@ export const registerSuspenseInstanceRetry =
export const canHydrateFormStateMarker = $$$config.canHydrateFormStateMarker;
export const isFormStateMarkerMatching = $$$config.isFormStateMarkerMatching;
export const getNextHydratableSibling = $$$config.getNextHydratableSibling;
+export const getNextHydratableSiblingAfterSingleton =
+ $$$config.getNextHydratableSiblingAfterSingleton;
export const getFirstHydratableChild = $$$config.getFirstHydratableChild;
export const getFirstHydratableChildWithinContainer =
$$$config.getFirstHydratableChildWithinContainer;
export const getFirstHydratableChildWithinSuspenseInstance =
$$$config.getFirstHydratableChildWithinSuspenseInstance;
+export const getFirstHydratableChildWithinSingleton =
+ $$$config.getFirstHydratableChildWithinSingleton;
export const canHydrateInstance = $$$config.canHydrateInstance;
export const canHydrateTextInstance = $$$config.canHydrateTextInstance;
export const canHydrateSuspenseInstance = $$$config.canHydrateSuspenseInstance;
diff --git a/packages/react-server/src/ReactFizzServer.js b/packages/react-server/src/ReactFizzServer.js
index 4c02d06e600a9..041ef31e2bd3d 100644
--- a/packages/react-server/src/ReactFizzServer.js
+++ b/packages/react-server/src/ReactFizzServer.js
@@ -4865,6 +4865,7 @@ function flushSegment(
return writeEndClientRenderedSuspenseBoundary(
destination,
request.renderState,
+ boundary.fallbackPreamble,
);
} else if (boundary.status !== COMPLETED) {
if (boundary.status === PENDING) {
@@ -4935,7 +4936,11 @@ function flushSegment(
const contentSegment = completedSegments[0];
flushSegment(request, destination, contentSegment, hoistableState);
- return writeEndCompletedSuspenseBoundary(destination, request.renderState);
+ return writeEndCompletedSuspenseBoundary(
+ destination,
+ request.renderState,
+ boundary.contentPreamble,
+ );
}
}