-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Don't delete trailing mismatches during hydration at the root #21021
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
Changes from all commits
0451e99
64b4cf1
2ea0f18
7234ff4
c035eec
618232e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -368,12 +368,31 @@ describe('rendering React components at document', () => { | |
expect(testDocument.body.innerHTML).toBe('Hello world'); | ||
}); | ||
|
||
it('renders over an existing text child without throwing', () => { | ||
it('cannot render over an existing text child at the root', () => { | ||
const container = document.createElement('div'); | ||
container.textContent = 'potato'; | ||
expect(() => ReactDOM.hydrate(<div>parsnip</div>, container)).toErrorDev( | ||
'Expected server HTML to contain a matching <div> in <div>.', | ||
); | ||
// This creates an unfortunate double text case. | ||
expect(container.textContent).toBe('potatoparsnip'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Looks like this test verifies broken behavior. I wonder if there's a more explicit way we could write it (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea I'm not sure what would be best way to encode the subtleties here. This is not a "bug" as in it's not an issue with the implementation details that we could fix. It's an intentional behavior that we want to keep whatever else was in there and changing that behavior could be a breaking change. |
||
|
||
it('renders over an existing nested text child without throwing', () => { | ||
const container = document.createElement('div'); | ||
const wrapper = document.createElement('div'); | ||
wrapper.textContent = 'potato'; | ||
container.appendChild(wrapper); | ||
expect(() => | ||
ReactDOM.hydrate( | ||
<div> | ||
<div>parsnip</div> | ||
</div>, | ||
container, | ||
), | ||
).toErrorDev( | ||
'Expected server HTML to contain a matching <div> in <div>.', | ||
); | ||
expect(container.textContent).toBe('parsnip'); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -510,26 +510,47 @@ describe('ReactDOMServerHydration', () => { | |
|
||
it('Suspense + hydration in legacy mode', () => { | ||
const element = document.createElement('div'); | ||
element.innerHTML = '<div>Hello World</div>'; | ||
const div = element.firstChild; | ||
element.innerHTML = '<div><div>Hello World</div></div>'; | ||
const div = element.firstChild.firstChild; | ||
const ref = React.createRef(); | ||
expect(() => | ||
ReactDOM.hydrate( | ||
<React.Suspense fallback={null}> | ||
<div ref={ref}>Hello World</div> | ||
</React.Suspense>, | ||
<div> | ||
<React.Suspense fallback={null}> | ||
<div ref={ref}>Hello World</div> | ||
</React.Suspense> | ||
</div>, | ||
element, | ||
), | ||
).toErrorDev( | ||
'Warning: Did not expect server HTML to contain a <div> in <div>.', | ||
{withoutStack: true}, | ||
); | ||
|
||
// The content should've been client rendered and replaced the | ||
// existing div. | ||
expect(ref.current).not.toBe(div); | ||
// The HTML should be the same though. | ||
expect(element.innerHTML).toBe('<div>Hello World</div>'); | ||
expect(element.innerHTML).toBe('<div><div>Hello World</div></div>'); | ||
}); | ||
|
||
it('Suspense + hydration in legacy mode (at root)', () => { | ||
const element = document.createElement('div'); | ||
element.innerHTML = '<div>Hello World</div>'; | ||
const div = element.firstChild; | ||
const ref = React.createRef(); | ||
ReactDOM.hydrate( | ||
<React.Suspense fallback={null}> | ||
<div ref={ref}>Hello World</div> | ||
</React.Suspense>, | ||
element, | ||
); | ||
|
||
// The content should've been client rendered. | ||
expect(ref.current).not.toBe(div); | ||
// Unfortunately, since we don't delete the tail at the root, a duplicate will remain. | ||
expect(element.innerHTML).toBe( | ||
'<div>Hello World</div><div>Hello World</div>', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand the difference in observable behavior between these two tests:
Seems like the significant difference here is that you have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is a bit weird. There's no suspense boundary comment in the HTML so it doesn't find any matching suspense boundary. If that mismatch is at the root, we just assume that we have to insert the DOM node. We assume that we want to keep what was already there - that didn't match. So we end up with two. If it's not at the root, we still mismatch and insert a new node but we also remove what was already there. |
||
); | ||
}); | ||
|
||
it('Suspense + hydration in legacy mode with no fallback', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ import { | |
hydrateTextInstance, | ||
hydrateSuspenseInstance, | ||
getNextHydratableInstanceAfterSuspenseInstance, | ||
shouldDeleteUnhydratedTailInstances, | ||
didNotMatchHydratedContainerTextInstance, | ||
didNotMatchHydratedTextInstance, | ||
didNotHydrateContainerInstance, | ||
|
@@ -438,18 +439,15 @@ function popHydrationState(fiber: Fiber): boolean { | |
return false; | ||
} | ||
|
||
const type = fiber.type; | ||
|
||
// If we have any remaining hydratable nodes, we need to delete them now. | ||
// We only do this deeper than head and body since they tend to have random | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit This comment is pretty DOM renderer specific |
||
// other nodes in them. We also ignore components with pure text content in | ||
// side of them. | ||
// TODO: Better heuristic. | ||
// side of them. We also don't delete anything inside the root container. | ||
if ( | ||
fiber.tag !== HostComponent || | ||
(type !== 'head' && | ||
type !== 'body' && | ||
!shouldSetTextContent(type, fiber.memoizedProps)) | ||
fiber.tag !== HostRoot && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the effective change. |
||
(fiber.tag !== HostComponent || | ||
(shouldDeleteUnhydratedTailInstances(fiber.type) && | ||
!shouldSetTextContent(fiber.type, fiber.memoizedProps))) | ||
) { | ||
let nextInstance = nextHydratableInstance; | ||
while (nextInstance) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.