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

Conversation

frontman-git
Copy link
Contributor

fix 3732

When hydrating a <template> element, Preact fails to reuse existing DOM nodes inside template.content.
This is because <template> elements encapsulate their children within a DocumentFragment, but the diff/hydrate logic doesn't switch the context to template.content.

As a result, hydration skips the existing fragment content and instead appends new nodes — leading to duplicated content in the DOM.

Copy link

github-actions bot commented Jun 18, 2025

📊 Tachometer Benchmark Results

Summary

duration

  • create10k: unsure 🔍 -1% - +0% (-6.44ms - +0.82ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -0% - +0% (-0.02ms - +0.04ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -1% - +3% (-0.52ms - +2.03ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -0% - +1% (-0.05ms - +0.14ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -1% - +2% (-0.84ms - +0.98ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -4% - +3% (-0.09ms - +0.07ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -2% - +2% (-0.58ms - +0.56ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -5% - +2% (-1.44ms - +0.48ms)
    preact-local vs preact-main

usedJSHeapSize

  • create10k: unsure 🔍 +0% - +0% (+0.00ms - +0.00ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 +0% - +0% (+0.00ms - +0.01ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -5% - +7% (-0.32ms - +0.41ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 +0% - +0% (+0.00ms - +0.01ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -0% - +1% (-0.00ms - +0.02ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -2% - +3% (-0.02ms - +0.03ms)
    preact-local vs preact-main
  • todo: unsure 🔍 +0% - +0% (+0.00ms - +0.00ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -0% - +0% (-0.01ms - +0.01ms)
    preact-local vs preact-main

Results

create10k

duration

VersionAvg timevs preact-localvs preact-main
preact-local989.52ms - 994.42ms-unsure 🔍
-1% - +0%
-6.44ms - +0.82ms
preact-main992.10ms - 997.46msunsure 🔍
-0% - +1%
-0.82ms - +6.44ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local19.19ms - 19.19ms-unsure 🔍
+0% - +0%
+0.00ms - +0.00ms
preact-main19.19ms - 19.19msunsure 🔍
-0% - -0%
-0.00ms - -0.00ms
-
filter-list

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.50ms - 16.54ms-unsure 🔍
-0% - +0%
-0.02ms - +0.04ms
preact-main16.49ms - 16.53msunsure 🔍
-0% - +0%
-0.04ms - +0.02ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.54ms - 1.54ms-unsure 🔍
+0% - +0%
+0.00ms - +0.01ms
preact-main1.54ms - 1.54msunsure 🔍
-0% - -0%
-0.01ms - -0.00ms
-
hydrate1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local67.60ms - 69.18ms-unsure 🔍
-1% - +3%
-0.52ms - +2.03ms
preact-main66.64ms - 68.64msunsure 🔍
-3% - +1%
-2.03ms - +0.52ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local5.80ms - 6.34ms-unsure 🔍
-5% - +7%
-0.32ms - +0.41ms
preact-main5.78ms - 6.27msunsure 🔍
-7% - +5%
-0.41ms - +0.32ms
-
many-updates

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.51ms - 16.68ms-unsure 🔍
-0% - +1%
-0.05ms - +0.14ms
preact-main16.52ms - 16.58msunsure 🔍
-1% - +0%
-0.14ms - +0.05ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.78ms - 3.78ms-unsure 🔍
+0% - +0%
+0.00ms - +0.01ms
preact-main3.77ms - 3.78msunsure 🔍
-0% - -0%
-0.01ms - -0.00ms
-
replace1k
  • Browser: chrome-headless
  • Sample size: 100
  • Built by: CI #4800
  • Commit: e17ec02

duration

VersionAvg timevs preact-localvs preact-main
preact-local61.15ms - 62.34ms-unsure 🔍
-1% - +2%
-0.84ms - +0.98ms
preact-main60.99ms - 62.36msunsure 🔍
-2% - +1%
-0.98ms - +0.84ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local2.99ms - 3.01ms-unsure 🔍
-0% - +1%
-0.00ms - +0.02ms
preact-main2.99ms - 2.99msunsure 🔍
-1% - +0%
-0.02ms - +0.00ms
-

run-warmup-0

VersionAvg timevs preact-localvs preact-main
preact-local26.06ms - 26.92ms-slower ❌
1% - 5%
0.21ms - 1.32ms
preact-main25.38ms - 26.07msfaster ✔
1% - 5%
0.21ms - 1.32ms
-

run-warmup-1

VersionAvg timevs preact-localvs preact-main
preact-local32.02ms - 33.31ms-unsure 🔍
-3% - +3%
-1.05ms - +0.87ms
preact-main32.05ms - 33.46msunsure 🔍
-3% - +3%
-0.87ms - +1.05ms
-

run-warmup-2

VersionAvg timevs preact-localvs preact-main
preact-local28.44ms - 29.46ms-slower ❌
0% - 5%
0.07ms - 1.46ms
preact-main27.71ms - 28.66msfaster ✔
0% - 5%
0.07ms - 1.46ms
-

run-warmup-3

VersionAvg timevs preact-localvs preact-main
preact-local25.82ms - 26.85ms-unsure 🔍
-0% - +5%
-0.07ms - +1.28ms
preact-main25.28ms - 26.18msunsure 🔍
-5% - +0%
-1.28ms - +0.07ms
-

run-warmup-4

VersionAvg timevs preact-localvs preact-main
preact-local22.07ms - 23.33ms-unsure 🔍
-3% - +4%
-0.73ms - +0.98ms
preact-main22.00ms - 23.15msunsure 🔍
-4% - +3%
-0.98ms - +0.73ms
-

run-final

VersionAvg timevs preact-localvs preact-main
preact-local20.84ms - 21.76ms-unsure 🔍
-0% - +6%
-0.08ms - +1.14ms
preact-main20.37ms - 21.17msunsure 🔍
-5% - +0%
-1.14ms - +0.08ms
-
text-update

duration

VersionAvg timevs preact-localvs preact-main
preact-local2.00ms - 2.10ms-unsure 🔍
-4% - +3%
-0.09ms - +0.07ms
preact-main2.00ms - 2.13msunsure 🔍
-3% - +5%
-0.07ms - +0.09ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local0.97ms - 1.01ms-unsure 🔍
-2% - +3%
-0.02ms - +0.03ms
preact-main0.96ms - 1.00msunsure 🔍
-3% - +2%
-0.03ms - +0.02ms
-
todo

duration

VersionAvg timevs preact-localvs preact-main
preact-local32.45ms - 32.91ms-unsure 🔍
-2% - +2%
-0.58ms - +0.56ms
preact-main32.17ms - 33.21msunsure 🔍
-2% - +2%
-0.56ms - +0.58ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.25ms - 1.25ms-unsure 🔍
+0% - +0%
+0.00ms - +0.00ms
preact-main1.24ms - 1.25msunsure 🔍
-0% - -0%
-0.00ms - -0.00ms
-
update10th1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local28.84ms - 30.11ms-unsure 🔍
-5% - +2%
-1.44ms - +0.48ms
preact-main29.24ms - 30.67msunsure 🔍
-2% - +5%
-0.48ms - +1.44ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local2.94ms - 2.95ms-unsure 🔍
-0% - +0%
-0.01ms - +0.01ms
preact-main2.94ms - 2.95msunsure 🔍
-0% - +0%
-0.01ms - +0.01ms
-

tachometer-reporter-action v2 for CI

@rschristian
Copy link
Member

Size Change: +120 B (+0.26%)

Total Size: 46.8 kB

Filename Size Change
dist/preact.js 4.68 kB +37 B (+0.8%)
dist/preact.mjs 4.71 kB +43 B (+0.92%)
dist/preact.umd.js 4.76 kB +40 B (+0.85%)
ℹ️ View Unchanged
Filename Size
compat/dist/compat.js 3.78 kB
compat/dist/compat.mjs 3.69 kB
compat/dist/compat.umd.js 3.83 kB
debug/dist/debug.js 3.85 kB
debug/dist/debug.mjs 3.85 kB
debug/dist/debug.umd.js 3.94 kB
devtools/dist/devtools.js 260 B
devtools/dist/devtools.mjs 271 B
devtools/dist/devtools.umd.js 346 B
hooks/dist/hooks.js 1.52 kB
hooks/dist/hooks.mjs 1.54 kB
hooks/dist/hooks.umd.js 1.58 kB
jsx-runtime/dist/jsxRuntime.js 892 B
jsx-runtime/dist/jsxRuntime.mjs 861 B
jsx-runtime/dist/jsxRuntime.umd.js 966 B
test-utils/dist/testUtils.js 473 B
test-utils/dist/testUtils.mjs 473 B
test-utils/dist/testUtils.umd.js 555 B

compressed-size-action

fix: apply precommit hook

Co-authored-by: Ryan Christian <[email protected]>

refactor: reorder template tag check to avoid expensive instanceof first

Co-authored-by: Ryan Christian <[email protected]>

fix: import slice from util file
@frontman-git frontman-git force-pushed the fix/template-hydration-reuse-dom branch from 16b8b19 to 1dae489 Compare June 18, 2025 05:26
@frontman-git
Copy link
Contributor Author

Thanks for the review — commits have been squashed.

Comment on lines +57 to +61
if (
newParentVNode.type === 'template' &&
excessDomChildren?.length === 0 &&
parentDom instanceof DocumentFragment
) {
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: <template> tag content rendered twice
3 participants