Skip to content

Commit

Permalink
Fix HTML comments breaking hydration
Browse files Browse the repository at this point in the history
This PR resolves a regression when react-dom/server was used to render
a Preact app. It was introduced in #2942
  • Loading branch information
marvinhagemeister committed Jan 23, 2021
1 parent 359ad4f commit 2f14050
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/diff/index.js
Expand Up @@ -318,7 +318,13 @@ function diffElementNodes(
//
// Note: This takes advantage of Text nodes having `.localName=undefined`,
// which is loosely equal to Text VNodes' `.type=null`. Elements use string equality.
if (child != null && (dom == child || child.localName == nodeType)) {
if (
child != null &&
((newVNode.type === null
? child.nodeType === 3
: child.localName === newVNode.type) ||
dom == child)
) {
dom = child;
excessDomChildren[i] = null;
break;
Expand Down
6 changes: 6 additions & 0 deletions test/browser/hydrate.test.js
Expand Up @@ -445,4 +445,10 @@ describe('hydrate()', () => {
'<p class="hi">hello baz</p><p class="hi">hello bar</p>'
);
});

it('should skip comment nodes', () => {
scratch.innerHTML = '<p>hello <!-- c -->foo</p>';
hydrate(<p class="hi">hello {'foo'}</p>, scratch);
expect(scratch.innerHTML).to.equal('<p>hello foo</p>');
});
});

0 comments on commit 2f14050

Please sign in to comment.