Skip to content

Commit ef14280

Browse files
authored
use simpler insert and append functions when not compiling with hydration (#6525)
1 parent 33307a5 commit ef14280

File tree

5 files changed

+69
-23
lines changed

5 files changed

+69
-23
lines changed

src/compiler/compile/Component.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,14 @@ export default class Component {
272272
} else {
273273
let name = node.name.slice(1);
274274

275+
if (compile_options.hydratable) {
276+
if (internal_exports.has(`${name}_hydration`)) {
277+
name += '_hydration';
278+
} else if (internal_exports.has(`${name}Hydration`)) {
279+
name += 'Hydration';
280+
}
281+
}
282+
275283
if (compile_options.dev) {
276284
if (internal_exports.has(`${name}_dev`)) {
277285
name += '_dev';

src/runtime/internal/dev.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { custom_event, append, insert, detach, listen, attr } from './dom';
1+
import { custom_event, append, append_hydration, insert, insert_hydration, detach, listen, attr } from './dom';
22
import { SvelteComponent } from './Component';
33

44
export function dispatch_dev<T=any>(type: string, detail?: T) {
@@ -10,11 +10,21 @@ export function append_dev(target: Node, node: Node) {
1010
append(target, node);
1111
}
1212

13+
export function append_hydration_dev(target: Node, node: Node) {
14+
dispatch_dev('SvelteDOMInsert', { target, node });
15+
append_hydration(target, node);
16+
}
17+
1318
export function insert_dev(target: Node, node: Node, anchor?: Node) {
1419
dispatch_dev('SvelteDOMInsert', { target, node, anchor });
1520
insert(target, node, anchor);
1621
}
1722

23+
export function insert_hydration_dev(target: Node, node: Node, anchor?: Node) {
24+
dispatch_dev('SvelteDOMInsert', { target, node, anchor });
25+
insert_hydration(target, node, anchor);
26+
}
27+
1828
export function detach_dev(node: Node) {
1929
dispatch_dev('SvelteDOMRemove', { node });
2030
detach(node);

src/runtime/internal/dom.ts

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ function init_hydrate(target: NodeEx) {
125125
}
126126
}
127127

128+
export function append(target: Node, node: Node) {
129+
target.appendChild(node);
130+
}
131+
128132
export function append_styles(
129133
target: Node,
130134
style_sheet_id: string,
@@ -161,7 +165,7 @@ function append_stylesheet(node: ShadowRoot | Document, style: HTMLStyleElement)
161165
append((node as Document).head || node, style);
162166
}
163167

164-
export function append(target: NodeEx, node: NodeEx) {
168+
export function append_hydration(target: NodeEx, node: NodeEx) {
165169
if (is_hydrating) {
166170
init_hydrate(target);
167171

@@ -187,9 +191,13 @@ export function append(target: NodeEx, node: NodeEx) {
187191
}
188192
}
189193

190-
export function insert(target: NodeEx, node: NodeEx, anchor?: NodeEx) {
194+
export function insert(target: Node, node: Node, anchor?: Node) {
195+
target.insertBefore(node, anchor || null);
196+
}
197+
198+
export function insert_hydration(target: NodeEx, node: NodeEx, anchor?: NodeEx) {
191199
if (is_hydrating && !anchor) {
192-
append(target, node);
200+
append_hydration(target, node);
193201
} else if (node.parentNode !== target || node.nextSibling != anchor) {
194202
target.insertBefore(node, anchor || null);
195203
}
@@ -482,7 +490,7 @@ export function claim_html_tag(nodes) {
482490
const start_index = find_comment(nodes, 'HTML_TAG_START', 0);
483491
const end_index = find_comment(nodes, 'HTML_TAG_END', start_index);
484492
if (start_index === end_index) {
485-
return new HtmlTag();
493+
return new HtmlTagHydration();
486494
}
487495

488496
init_claim_info(nodes);
@@ -494,7 +502,7 @@ export function claim_html_tag(nodes) {
494502
n.claim_order = nodes.claim_info.total_claimed;
495503
nodes.claim_info.total_claimed += 1;
496504
}
497-
return new HtmlTag(claimed_nodes);
505+
return new HtmlTagHydration(claimed_nodes);
498506
}
499507

500508
export function set_data(text, data) {
@@ -628,27 +636,24 @@ export class HtmlTag {
628636
e: HTMLElement;
629637
// html tag nodes
630638
n: ChildNode[];
631-
// hydration claimed nodes
632-
l: ChildNode[] | void;
633639
// target
634640
t: HTMLElement;
635641
// anchor
636642
a: HTMLElement;
637643

638-
constructor(claimed_nodes?: ChildNode[]) {
644+
constructor() {
639645
this.e = this.n = null;
640-
this.l = claimed_nodes;
646+
}
647+
648+
c(html: string) {
649+
this.h(html);
641650
}
642651

643652
m(html: string, target: HTMLElement, anchor: HTMLElement = null) {
644653
if (!this.e) {
645654
this.e = element(target.nodeName as keyof HTMLElementTagNameMap);
646655
this.t = target;
647-
if (this.l) {
648-
this.n = this.l;
649-
} else {
650-
this.h(html);
651-
}
656+
this.c(html);
652657
}
653658

654659
this.i(anchor);
@@ -676,6 +681,29 @@ export class HtmlTag {
676681
}
677682
}
678683

684+
export class HtmlTagHydration extends HtmlTag {
685+
// hydration claimed nodes
686+
l: ChildNode[] | void;
687+
688+
constructor(claimed_nodes?: ChildNode[]) {
689+
super();
690+
this.e = this.n = null;
691+
this.l = claimed_nodes;
692+
}
693+
c(html: string) {
694+
if (this.l) {
695+
this.n = this.l;
696+
} else {
697+
super.c(html);
698+
}
699+
}
700+
i(anchor) {
701+
for (let i = 0; i < this.n.length; i += 1) {
702+
insert_hydration(this.t, this.n[i], anchor);
703+
}
704+
}
705+
}
706+
679707
export function attribute_to_object(attributes: NamedNodeMap) {
680708
const result = {};
681709
for (const attribute of attributes) {

test/js/samples/hydrated-void-element/expected.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
detach,
99
element,
1010
init,
11-
insert,
11+
insert_hydration,
1212
noop,
1313
safe_not_equal,
1414
space,
@@ -40,9 +40,9 @@ function create_fragment(ctx) {
4040
attr(img, "alt", "donuts");
4141
},
4242
m(target, anchor) {
43-
insert(target, img, anchor);
44-
insert(target, t, anchor);
45-
insert(target, div, anchor);
43+
insert_hydration(target, img, anchor);
44+
insert_hydration(target, t, anchor);
45+
insert_hydration(target, div, anchor);
4646
},
4747
p: noop,
4848
i: noop,

test/js/samples/src-attribute-check/expected.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
detach,
88
element,
99
init,
10-
insert,
10+
insert_hydration,
1111
noop,
1212
safe_not_equal,
1313
space,
@@ -41,9 +41,9 @@ function create_fragment(ctx) {
4141
if (!src_url_equal(img1.src, img1_src_value = "" + (/*slug*/ ctx[1] + ".jpg"))) attr(img1, "src", img1_src_value);
4242
},
4343
m(target, anchor) {
44-
insert(target, img0, anchor);
45-
insert(target, t, anchor);
46-
insert(target, img1, anchor);
44+
insert_hydration(target, img0, anchor);
45+
insert_hydration(target, t, anchor);
46+
insert_hydration(target, img1, anchor);
4747
},
4848
p(ctx, [dirty]) {
4949
if (dirty & /*url*/ 1 && !src_url_equal(img0.src, img0_src_value = /*url*/ ctx[0])) {

0 commit comments

Comments
 (0)