Skip to content

Commit

Permalink
Allow hydrating empty string as null.
Browse files Browse the repository at this point in the history
  • Loading branch information
rickhanlonii committed Dec 15, 2024
1 parent 3012d8b commit a42f0d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
6 changes: 4 additions & 2 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,9 @@ export function canHydrateInstance(
} else if (
rel !== anyProps.rel ||
element.getAttribute('href') !==
(anyProps.href == null ? null : anyProps.href) ||
(anyProps.href == null || anyProps.href === ''
? null
: anyProps.href) ||
element.getAttribute('crossorigin') !==
(anyProps.crossOrigin == null ? null : anyProps.crossOrigin) ||
element.getAttribute('title') !==
Expand Down Expand Up @@ -2984,7 +2986,7 @@ export function hydrateHoistable(
const node = nodes[i];
if (
node.getAttribute('href') !==
(props.href == null ? null : props.href) ||
(props.href == null || props.href === '' ? null : props.href) ||
node.getAttribute('rel') !==
(props.rel == null ? null : props.rel) ||
node.getAttribute('title') !==
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,24 @@ describe('ReactDOMServerIntegration', () => {
expect(e.getAttribute('href')).toBe('');
});

itRenders('empty href on other tags', async render => {
const e = await render(<link rel="stylesheet" href="" />, 1);
expect(e.getAttribute('href')).toBe(null);
});

itRenders('empty href on other tags', async render => {
const e = await render(<base href="" />, 1);
expect(e.getAttribute('href')).toBe(null);
});

itRenders('empty href on other tags', async render => {
const e = await render(
// <link href="" /> would be more sensible.
// However, that results in a hydration warning as well.
// Our test helpers do not support different error counts for initial
// server render and hydration.
// The number of errors on the server need to be equal to the number of
// errors during hydration.
// So we use a <div> instead.
<div href="" />,
<map>
<area alt="" href="" />
</map>,
1,
);
expect(e.getAttribute('href')).toBe(null);
expect(e.firstChild.getAttribute('href')).toBe(null);
});

itRenders('no string prop with true value', async render => {
Expand Down

0 comments on commit a42f0d3

Please sign in to comment.