Skip to content

[WIP] Diffs for hydration errors #27808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 228 additions & 13 deletions packages/react-dom-bindings/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

import type {HostContext, HostContextDev} from './ReactFiberConfigDOM';

import {HostContextNamespaceNone} from './ReactFiberConfigDOM';
import {
HostContextNamespaceNone,
HydratableInstance
} from './ReactFiberConfigDOM'

import {
registrationNameDependencies,
Expand Down Expand Up @@ -2892,6 +2895,216 @@ export function diffHydratedText(
return isDifferent;
}

function formatAttributes(element: Element): string {
let str = '';
const attributes = element.attributes;
for (let i = 0; i < attributes.length; i++) {
if (i > 30) {
str += ' ...';
break;
}
const attributeName = attributes[i].name;
const value = attributes[i].value;
if (value != null) {
let trimmedValue = value;
if (value.length > 30) {
trimmedValue = value.slice(0, 30) + '...';
}
const escapedValue = trimmedValue.replace(/"/g, '&quot;');
str += ' ' + attributeName + '="' + escapedValue + '"';
}
}
return str;
}

function formatProps(props): string {
let str = '';
let i = 0;
for (const prop in props) {
if (!props.hasOwnProperty(prop)) {
continue;
}
if (i > 30) {
str += ' ...';
break;
}
const attributeName = prop;
const value = props[prop];
if (
value != null &&
typeof value !== 'function' &&
typeof value !== 'symbol'
) {
let trimmedValue = JSON.stringify(value);
if (value.length > 30) {
trimmedValue = value.slice(0, 30) + '...';
}
if (typeof value === 'string') {
let insideQuotes = false
if (value.startsWith('&quot;') && value.endsWith('&quot;')) {
insideQuotes = true
} else if (value.startsWith('&apos;') && value.endsWith('&apos;')) {
insideQuotes = true
}
if (insideQuotes) {
// eslint-disable-next-line react-internal/safe-string-coercion
str += ' ' + attributeName + '={' + trimmedValue + '}';
} else {
// eslint-disable-next-line react-internal/safe-string-coercion
str += ' ' + attributeName + '=' + trimmedValue + '';
}
} else {
str += ' ' + attributeName + '={' + trimmedValue + '}';
}
}
i++;
}
return str;
}

function formatElement(element, indentation, formattedChildren) {
let str = indentation + '<' + element.nodeName.toLowerCase();
str += formatAttributes(element);
if (formattedChildren === null) {
if (element.innerHTML !== '') {
str += '>...';
str += '</' + element.nodeName.toLowerCase() + '>';
} else {
str += ' />';
}
} else {
str += '>\n' + formattedChildren;
str += '\n' + indentation;
str += '</' + element.nodeName.toLowerCase() + '>';
}
return str;
}

// todo: haven't tested yet
function formatDocumentNode(node) {
const children = [];
let formattedChildren = null;
let child = node.firstChild;
if (child !== null) {
while (true) {
children.push(formatNode(child, ''));
child = child.nextSibling;
if (child === null) {
break;
}
}
formattedChildren = children.join('\n');
}
return formatElement(node, '', formattedChildren);
}

function formatNode(node, indentation) {
switch (node.nodeType) {
// TODO: use constants
case Node.DOCUMENT_NODE:
case Node.DOCUMENT_FRAGMENT_NODE: {
return formatDocumentNode(node);
}
case Node.ELEMENT_NODE:
return formatElement(node, indentation, null);
case Node.TEXT_NODE:
// TODO: trim
return indentation + '"' + node.nodeValue + '"';
default:
return '';
}
}

function shouldIncludeInDiff(node) {
return (
// TODO: use constants
node.nodeType === Node.ELEMENT_NODE || node.nodeType === Node.TEXT_NODE
);
}

function findPreviousSiblingForDiff(node) {
let sibling = node.previousSibling;
while (sibling !== null) {
if (shouldIncludeInDiff(sibling)) {
return sibling;
}
sibling = sibling.previousSibling;
}
return null;
}

function findNextSiblingForDiff(node) {
let sibling = node.nextSibling;
while (sibling !== null) {
if (shouldIncludeInDiff(sibling)) {
return sibling;
}
sibling = sibling.nextSibling;
}
return null;
}

function formatDiffForExtraServerNode(parentNode, child) {
let formattedSibling = null;
const prevSibling = findPreviousSiblingForDiff(child);
if (prevSibling !== null) {
formattedSibling = formatNode(prevSibling, ' ');
}
let formattedChildren = '';
if (formattedSibling !== null) {
if (findPreviousSiblingForDiff(prevSibling) !== null) {
formattedChildren += ' ...\n';
}
formattedChildren += formattedSibling + '\n';
}
formattedChildren += formatNode(child, '- ');
formattedChildren += ' <-- server';
if (findNextSiblingForDiff(child) !== null) {
formattedChildren += '\n ...';
}
return formatElement(parentNode, ' ', formattedChildren);
}

function formatDiffForExtraClientNode(
parentNode,
text,
mismatchNode?: HydratableInstance
) {
let formattedSibling = null;
let prevSibling = null;
if (mismatchNode != null) {
prevSibling = findPreviousSiblingForDiff(mismatchNode);
} else if (parentNode.lastChild !== null) {
prevSibling = parentNode.lastChild;
}
let formattedChildren = '';
if (prevSibling !== null) {
formattedSibling = formatNode(prevSibling, ' ');
if (formattedSibling !== null) {
if (findPreviousSiblingForDiff(prevSibling) !== null) {
formattedChildren += ' ...\n';
}
formattedChildren += formattedSibling + '\n';
}
}
if (mismatchNode != null) {
formattedChildren += formatNode(mismatchNode, '- ') + ' <-- server\n';
}
formattedChildren += text + ' <-- client';
formattedChildren += ' <-- client\n';
if (mismatchNode != null) {
formattedChildren += ' ...';
}
return formatElement(parentNode, ' ', formattedChildren);
}

function formatTagWithProps(tag, props) {
let str = '<' + tag.toLowerCase();
str += formatProps(props);
str += ' />';
return str;
}

export function warnForDeletedHydratableElement(
parentNode: Element | Document | DocumentFragment,
child: Element,
Expand All @@ -2902,9 +3115,9 @@ export function warnForDeletedHydratableElement(
}
didWarnInvalidHydration = true;
console.error(
'Did not expect server HTML to contain a <%s> in <%s>.',
child.nodeName.toLowerCase(),
parentNode.nodeName.toLowerCase(),
'The server has rendered an extra text node. ' +
'The mismatch occurred inside of this parent:\n\n%s',
formatDiffForExtraServerNode(parentNode, child),
);
}
}
Expand All @@ -2919,9 +3132,9 @@ export function warnForDeletedHydratableText(
}
didWarnInvalidHydration = true;
console.error(
'Did not expect server HTML to contain the text node "%s" in <%s>.',
child.nodeValue,
parentNode.nodeName.toLowerCase(),
'The server has rendered an extra text node. ' +
'The mismatch occurred inside of this parent:\n\n%s',
formatDiffForExtraServerNode(parentNode, child),
);
}
}
Expand All @@ -2930,23 +3143,25 @@ export function warnForInsertedHydratedElement(
parentNode: Element | Document | DocumentFragment,
tag: string,
props: Object,
lastHydratedChild: HydratableInstance,
) {
if (__DEV__) {
if (didWarnInvalidHydration) {
return;
}
didWarnInvalidHydration = true;
console.error(
'Expected server HTML to contain a matching <%s> in <%s>.',
tag,
parentNode.nodeName.toLowerCase(),
'The server has rendered an extra text node. ' +
'The mismatch occurred inside of this parent:\n\n%s',
formatDiffForExtraClientNode(parentNode, formatTagWithProps(tag, props), lastHydratedChild),
);
}
}

export function warnForInsertedHydratedText(
parentNode: Element | Document | DocumentFragment,
text: string,
lastHydratedChild: HydratableInstance,
) {
if (__DEV__) {
if (text === '') {
Expand All @@ -2961,9 +3176,9 @@ export function warnForInsertedHydratedText(
}
didWarnInvalidHydration = true;
console.error(
'Expected server HTML to contain a matching text node for "%s" in <%s>.',
text,
parentNode.nodeName.toLowerCase(),
'The server has rendered an extra text node. ' +
'The mismatch occurred inside of this parent:\n\n%s',
formatDiffForExtraClientNode(parentNode, text, lastHydratedChild),
);
}
}
Expand Down
24 changes: 19 additions & 5 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -1582,18 +1582,29 @@ export function didNotFindHydratableInstanceWithinContainer(
parentContainer: Container,
type: string,
props: Props,
lastHydratedChild,
) {
if (__DEV__) {
warnForInsertedHydratedElement(parentContainer, type, props);
warnForInsertedHydratedElement(
parentContainer,
type,
props,
lastHydratedChild,
);
}
}

export function didNotFindHydratableTextInstanceWithinContainer(
parentContainer: Container,
text: string,
lastHydratedChild,
) {
if (__DEV__) {
warnForInsertedHydratedText(parentContainer, text);
warnForInsertedHydratedText(
parentContainer,
text,
lastHydratedChild,
);
}
}

Expand All @@ -1609,23 +1620,25 @@ export function didNotFindHydratableInstanceWithinSuspenseInstance(
parentInstance: SuspenseInstance,
type: string,
props: Props,
lastHydratedChild,
) {
if (__DEV__) {
// $FlowFixMe[incompatible-type]: Only Element or Document can be parent nodes.
const parentNode: Element | Document | null = parentInstance.parentNode;
if (parentNode !== null)
warnForInsertedHydratedElement(parentNode, type, props);
warnForInsertedHydratedElement(parentNode, type, props, lastHydratedChild);
}
}

export function didNotFindHydratableTextInstanceWithinSuspenseInstance(
parentInstance: SuspenseInstance,
text: string,
lastHydratedChild,
) {
if (__DEV__) {
// $FlowFixMe[incompatible-type]: Only Element or Document can be parent nodes.
const parentNode: Element | Document | null = parentInstance.parentNode;
if (parentNode !== null) warnForInsertedHydratedText(parentNode, text);
if (parentNode !== null) warnForInsertedHydratedText(parentNode, text, lastHydratedChild);
}
}

Expand All @@ -1645,10 +1658,11 @@ export function didNotFindHydratableInstance(
type: string,
props: Props,
isConcurrentMode: boolean,
lastHydratedChild,
) {
if (__DEV__) {
if (isConcurrentMode || parentProps[SUPPRESS_HYDRATION_WARNING] !== true) {
warnForInsertedHydratedElement(parentInstance, type, props);
warnForInsertedHydratedElement(parentInstance, type, props, lastHydratedChild);
}
}
}
Expand Down
Loading