Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,11 @@ function diffElementNodes(
);

// Remove children that are not part of any vnode.
if (excessDomChildren != NULL) {
if (
excessDomChildren != NULL &&
newVNode.type != 'body' &&
newVNode.type != 'head'
) {
for (i = excessDomChildren.length; i--; ) {
removeNode(excessDomChildren[i]);
}
Expand Down
27 changes: 27 additions & 0 deletions test/browser/hydrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,33 @@ describe('hydrate()', () => {
expect(getLog()).to.deep.equal(['Comment.remove()', 'Comment.remove()']);
});

it('should preserve existing head children when hydrating document', () => {
document.textContent = '';
document.head.innerHTML =
'<title>Test</title><meta name="viewport" content="width=device-width">';
document.body.innerHTML = '<p>Test</p>';
clearLog();

const App = () => (
<Fragment>
<head>
<title>Test</title>
</head>
<body>
<p>Test</p>
</body>
</Fragment>
);

hydrate(<App />, document);

expect(document.head.querySelector('meta[name="viewport"]')).to.not.equal(
null
);
expect(document.head.querySelector('title').textContent).to.equal('Test');
expect(document.body.innerHTML).to.equal('<p>Test</p>');
});

it('should work with error boundaries', () => {
scratch.innerHTML = '<div>Hello, World!</div>';
class Root extends Component {
Expand Down
6 changes: 3 additions & 3 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1969,6 +1969,7 @@ describe('render()', () => {

it('should work with document', () => {
document.textContent = '';
document.body.textContent = '';
const App = () => (
<Fragment>
<head>
Expand All @@ -1980,9 +1981,8 @@ describe('render()', () => {
</Fragment>
);
render(<App />, document);
expect(document.documentElement.innerHTML.trim()).to.equal(
'<head><title>Test</title></head><body><p>Test</p></body>'
);
expect(document.head.querySelector('title').textContent).to.equal('Test');
expect(document.body.innerHTML.trim()).to.equal('<p>Test</p>');
});

it('should not remount components when replacing a component with a falsy value in-between', () => {
Expand Down
Loading