Skip to content

Commit fcd784b

Browse files
committed
use a simpler insert and append function when not compile with hydratable
1 parent fdd3d4b commit fcd784b

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
@@ -267,6 +267,14 @@ export default class Component {
267267
} else {
268268
let name = node.name.slice(1);
269269

270+
if (compile_options.hydratable) {
271+
if (internal_exports.has(`${name}_hydration`)) {
272+
name += '_hydration';
273+
} else if (internal_exports.has(`${name}Hydration`)) {
274+
name += 'Hydration';
275+
}
276+
}
277+
270278
if (compile_options.dev) {
271279
if (internal_exports.has(`${name}_dev`)) {
272280
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
@@ -112,7 +112,11 @@ function init_hydrate(target: NodeEx) {
112112
}
113113
}
114114

115-
export function append(target: NodeEx, node: NodeEx) {
115+
export function append(target: Node, node: Node) {
116+
target.appendChild(node);
117+
}
118+
119+
export function append_hydration(target: NodeEx, node: NodeEx) {
116120
if (is_hydrating) {
117121
init_hydrate(target);
118122

@@ -129,9 +133,13 @@ export function append(target: NodeEx, node: NodeEx) {
129133
}
130134
}
131135

132-
export function insert(target: NodeEx, node: NodeEx, anchor?: NodeEx) {
136+
export function insert(target: Node, node: Node, anchor?: Node) {
137+
target.insertBefore(node, anchor || null);
138+
}
139+
140+
export function insert_hydration(target: NodeEx, node: NodeEx, anchor?: NodeEx) {
133141
if (is_hydrating && !anchor) {
134-
append(target, node);
142+
append_hydration(target, node);
135143
} else if (node.parentNode !== target || node.nextSibling != anchor) {
136144
target.insertBefore(node, anchor || null);
137145
}
@@ -404,12 +412,12 @@ export function claim_html_tag(nodes) {
404412
const start_index = find_comment(nodes, 'HTML_TAG_START', 0);
405413
const end_index = find_comment(nodes, 'HTML_TAG_END', start_index);
406414
if (start_index === end_index) {
407-
return new HtmlTag();
415+
return new HtmlTagHydration();
408416
}
409417
const html_tag_nodes = nodes.splice(start_index, end_index + 1);
410418
detach(html_tag_nodes[0]);
411419
detach(html_tag_nodes[html_tag_nodes.length - 1]);
412-
return new HtmlTag(html_tag_nodes.slice(1, html_tag_nodes.length - 1));
420+
return new HtmlTagHydration(html_tag_nodes.slice(1, html_tag_nodes.length - 1));
413421
}
414422

415423
export function set_data(text, data) {
@@ -543,27 +551,24 @@ export class HtmlTag {
543551
e: HTMLElement;
544552
// html tag nodes
545553
n: ChildNode[];
546-
// hydration claimed nodes
547-
l: ChildNode[] | void;
548554
// target
549555
t: HTMLElement;
550556
// anchor
551557
a: HTMLElement;
552558

553-
constructor(claimed_nodes?: ChildNode[]) {
559+
constructor() {
554560
this.e = this.n = null;
555-
this.l = claimed_nodes;
561+
}
562+
563+
c(html: string) {
564+
this.h(html);
556565
}
557566

558567
m(html: string, target: HTMLElement, anchor: HTMLElement = null) {
559568
if (!this.e) {
560569
this.e = element(target.nodeName as keyof HTMLElementTagNameMap);
561570
this.t = target;
562-
if (this.l) {
563-
this.n = this.l;
564-
} else {
565-
this.h(html);
566-
}
571+
this.c(html);
567572
}
568573

569574
this.i(anchor);
@@ -591,6 +596,29 @@ export class HtmlTag {
591596
}
592597
}
593598

599+
export class HtmlTagHydration extends HtmlTag {
600+
// hydration claimed nodes
601+
l: ChildNode[] | void;
602+
603+
constructor(claimed_nodes?: ChildNode[]) {
604+
super();
605+
this.e = this.n = null;
606+
this.l = claimed_nodes;
607+
}
608+
c(html: string) {
609+
if (this.l) {
610+
this.n = this.l;
611+
} else {
612+
super.c(html);
613+
}
614+
}
615+
i(anchor) {
616+
for (let i = 0; i < this.n.length; i += 1) {
617+
insert_hydration(this.t, this.n[i], anchor);
618+
}
619+
}
620+
}
621+
594622
export function attribute_to_object(attributes: NamedNodeMap) {
595623
const result = {};
596624
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
@@ -39,9 +39,9 @@ function create_fragment(ctx) {
3939
attr(img, "alt", "donuts");
4040
},
4141
m(target, anchor) {
42-
insert(target, img, anchor);
43-
insert(target, t, anchor);
44-
insert(target, div, anchor);
42+
insert_hydration(target, img, anchor);
43+
insert_hydration(target, t, anchor);
44+
insert_hydration(target, div, anchor);
4545
},
4646
p: noop,
4747
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
@@ -40,9 +40,9 @@ function create_fragment(ctx) {
4040
if (img1.src !== (img1_src_value = "" + (/*slug*/ ctx[1] + ".jpg"))) attr(img1, "src", img1_src_value);
4141
},
4242
m(target, anchor) {
43-
insert(target, img0, anchor);
44-
insert(target, t, anchor);
45-
insert(target, img1, anchor);
43+
insert_hydration(target, img0, anchor);
44+
insert_hydration(target, t, anchor);
45+
insert_hydration(target, img1, anchor);
4646
},
4747
p(ctx, [dirty]) {
4848
if (dirty & /*url*/ 1 && img0.src !== (img0_src_value = /*url*/ ctx[0])) {

0 commit comments

Comments
 (0)