Skip to content
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

Single text node hot path #4523

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
19 changes: 17 additions & 2 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
RESET_MODE
} from '../constants';
import { BaseComponent, getDomSibling } from '../component';
import { Fragment } from '../create-element';
import { Fragment, createVNode } from '../create-element';
import { diffChildren } from './children';
import { setProperty } from './props';
import { assign, isArray, removeNode, slice } from '../util';
Expand Down Expand Up @@ -486,7 +486,6 @@ function diffElementNodes(
}
}

// If the new vnode didn't have dangerouslySetInnerHTML, diff its children
if (newHtml) {
// Avoid re-applying the same '__html' if it did not changed between re-render
if (
Expand All @@ -499,6 +498,22 @@ function diffElementNodes(
}

newVNode._children = [];
} else if (typeof newChildren === 'string') {
if (newChildren !== oldProps.children) {
while (oldVNode._children && (i = oldVNode._children.pop())) {
// Setting textContent on the dom element will unmount all DOM nodes
// of the previous children, so we don't need to remove DOM in this
// call to unmount
unmount(i, oldVNode, true);
}

newVNode._children = [
// @ts-expect-error
createVNode(null, (dom.textContent = newChildren), null, null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to consider using dom.appendChild(document.createTextNode(newChildren)) and manually removing previous children. Otherwise, rendering a single text child will blow away non-preact-rendered children from a parent element.

];
// @ts-expect-error
newVNode._children[0]._dom = dom.firstChild;
}
Comment on lines +501 to +516
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (typeof newChildren === 'string') {
if (newChildren !== oldProps.children) {
while (oldVNode._children && (i = oldVNode._children.pop())) {
// Setting textContent on the dom element will unmount all DOM nodes
// of the previous children, so we don't need to remove DOM in this
// call to unmount
unmount(i, oldVNode, true);
}
newVNode._children = [
// @ts-expect-error
createVNode(null, (dom.textContent = newChildren), null, null)
];
// @ts-expect-error
newVNode._children[0]._dom = dom.firstChild;
}
} else if (typeof newChildren === 'string' && typeof oldProps.children === 'string') {
if (newChildren !== oldProps.children) {
oldVNode._children[0]._dom.data = newChildren;
newVNode._children = oldVNode._children;
oldVNode._children = [];
}

} else {
if (oldHtml) dom.innerHTML = '';

Expand Down
13 changes: 12 additions & 1 deletion test/browser/components.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createElement, render, Component, Fragment } from 'preact';
import { createElement, render, Component, Fragment, createRef } from 'preact';
import { setupRerender } from 'preact/test-utils';
import {
setupScratch,
Expand Down Expand Up @@ -2078,6 +2078,17 @@ describe('Components', () => {
expect(parentDom1.base).to.equalNode(scratch.firstChild);
});

it('should set c.base for a single text-node child', () => {
let ref = createRef();
const SignalValue = props => {
return props.text;
};
render(<SignalValue ref={ref} text="hello world" />, scratch);
console.log(ref.current);
expect(ref.current.base.nodeType).to.equal(3);
expect(ref.current.base.data).to.equal('hello world');
});

it('should not update sibling c.base if child component changes DOM nodes', () => {
let s1 = {},
s2 = {},
Expand Down
Loading