Skip to content

fix: reuse DOM nodes inside <template> during hydration #4803

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

Open
wants to merge 2 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
10 changes: 9 additions & 1 deletion src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
UNDEFINED,
NULL
} from '../constants';
import { isArray } from '../util';
import { isArray, slice } from '../util';
import { getDomSibling } from '../component';


/**
* @typedef {import('../internal').ComponentChildren} ComponentChildren
* @typedef {import('../internal').Component} Component
Expand Down Expand Up @@ -53,6 +54,13 @@ export function diffChildren(
isHydrating,
refQueue
) {
if (
newParentVNode.type === 'template' &&
excessDomChildren?.length === 0 &&
parentDom instanceof DocumentFragment
) {
Comment on lines +57 to +61
Copy link
Member

@JoviDeCroock JoviDeCroock Jun 18, 2025

Choose a reason for hiding this comment

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

Let's make this fast by adding the isHydrating check, we don't need to check excessDomChildren length as we expect this to be 0 as per your assumptions in this PR. I would assume that we are in a DocumentFragment when we are dealing with a template.

Suggested change
if (
newParentVNode.type === 'template' &&
excessDomChildren?.length === 0 &&
parentDom instanceof DocumentFragment
) {
if (isHydrating && newParentVNode.type === 'template') {

I assume you could save even more bytes by moving this to

preact/src/diff/index.js

Lines 596 to 611 in 1dae489

diffChildren(
// @ts-expect-error
newVNode.type == 'template' ? dom.content : dom,
isArray(newChildren) ? newChildren : [newChildren],
newVNode,
oldVNode,
globalContext,
nodeType == 'foreignObject' ? XHTML_NAMESPACE : namespace,
excessDomChildren,
commitQueue,
excessDomChildren
? excessDomChildren[0]
: oldVNode._children && getDomSibling(oldVNode, 0),
isHydrating,
refQueue
);
and changing this condition to use newVNode.type === 'template' as well as hoisting the duplicated check

excessDomChildren = slice.call(parentDom.childNodes);
}
let i,
/** @type {VNode} */
oldVNode,
Expand Down
7 changes: 7 additions & 0 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2013,4 +2013,11 @@ describe('render()', () => {
render(<App />, scratch);
expect(scratch.innerHTML).to.equal('hello world');
});
it('should hydrate <template> tags ', () => {
const App = () => <template><h1>it works</h1></template>;
scratch.innerHTML = `<template><h1>it works</h1></template>`;

render(<App />, scratch);
expect(scratch.innerHTML).to.equal(`<template><h1>it works</h1></template>`);
});
});
Loading