Skip to content

Commit cadc0dc

Browse files
authored
feat: only traverse trailing static nodes during hydration (#12935)
1 parent 75759db commit cadc0dc

File tree

6 files changed

+25
-5
lines changed

6 files changed

+25
-5
lines changed

.changeset/quick-paws-wash.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: only traverse trailing static nodes during hydration

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export function process_children(nodes, initial, is_element, { visit, state }) {
118118
// traverse to the last (n - 1) one when hydrating
119119
if (skipped > 1) {
120120
skipped -= 1;
121-
state.init.push(b.stmt(get_node(false)));
121+
state.init.push(b.stmt(b.call('$.next', skipped !== 1 && b.literal(skipped))));
122122
}
123123
}
124124

packages/svelte/src/internal/client/dom/hydration.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,16 @@ export function hydrate_template(template) {
6666
}
6767
}
6868

69-
export function next() {
69+
export function next(count = 1) {
7070
if (hydrating) {
71-
hydrate_next();
71+
var i = count;
72+
var node = hydrate_node;
73+
74+
while (i--) {
75+
node = /** @type {TemplateNode} */ (get_next_sibling(node));
76+
}
77+
78+
hydrate_node = node;
7279
}
7380
}
7481

packages/svelte/tests/snapshot/samples/skip-static-subtree/_expected/client/index.svelte.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "svelte/internal/disclose-version";
22
import * as $ from "svelte/internal/client";
33

4-
var root = $.template(`<header><nav><a href="/">Home</a> <a href="/away">Away</a></nav></header> <main><h1> </h1> <div class="static"><p>we don't need to traverse these nodes</p></div> <p>or</p> <p>these</p> <p>ones</p> <!></main> <cant-skip><custom-elements></custom-elements></cant-skip>`, 3);
4+
var root = $.template(`<header><nav><a href="/">Home</a> <a href="/away">Away</a></nav></header> <main><h1> </h1> <div class="static"><p>we don't need to traverse these nodes</p></div> <p>or</p> <p>these</p> <p>ones</p> <!> <p>these</p> <p>trailing</p> <p>nodes</p> <p>can</p> <p>be</p> <p>completely</p> <p>ignored</p></main> <cant-skip><custom-elements></custom-elements></cant-skip>`, 3);
55

66
export default function Skip_static_subtree($$anchor, $$props) {
77
var fragment = root();
@@ -14,6 +14,7 @@ export default function Skip_static_subtree($$anchor, $$props) {
1414
var node = $.sibling(h1, 10);
1515

1616
$.html(node, () => $$props.content, false, false);
17+
$.next(14);
1718
$.reset(main);
1819

1920
var cant_skip = $.sibling(main, 2);

packages/svelte/tests/snapshot/samples/skip-static-subtree/_expected/server/index.svelte.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import * as $ from "svelte/internal/server";
33
export default function Skip_static_subtree($$payload, $$props) {
44
let { title, content } = $$props;
55

6-
$$payload.out += `<header><nav><a href="/">Home</a> <a href="/away">Away</a></nav></header> <main><h1>${$.escape(title)}</h1> <div class="static"><p>we don't need to traverse these nodes</p></div> <p>or</p> <p>these</p> <p>ones</p> ${$.html(content)}</main> <cant-skip><custom-elements with="attributes"></custom-elements></cant-skip>`;
6+
$$payload.out += `<header><nav><a href="/">Home</a> <a href="/away">Away</a></nav></header> <main><h1>${$.escape(title)}</h1> <div class="static"><p>we don't need to traverse these nodes</p></div> <p>or</p> <p>these</p> <p>ones</p> ${$.html(content)} <p>these</p> <p>trailing</p> <p>nodes</p> <p>can</p> <p>be</p> <p>completely</p> <p>ignored</p></main> <cant-skip><custom-elements with="attributes"></custom-elements></cant-skip>`;
77
}

packages/svelte/tests/snapshot/samples/skip-static-subtree/index.svelte

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
<p>these</p>
1919
<p>ones</p>
2020
{@html content}
21+
<p>these</p>
22+
<p>trailing</p>
23+
<p>nodes</p>
24+
<p>can</p>
25+
<p>be</p>
26+
<p>completely</p>
27+
<p>ignored</p>
2128
</main>
2229

2330
<cant-skip>

0 commit comments

Comments
 (0)