Skip to content

Commit 4a473e3

Browse files
committed
do not reuse claimed nodes if the html does not match
1 parent 9e8592e commit 4a473e3

File tree

10 files changed

+89
-3
lines changed

10 files changed

+89
-3
lines changed

src/compiler/compile/render_dom/wrappers/RawMustacheTag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default class RawMustacheTagWrapper extends Tag {
5858
block.chunks.create.push(b`${html_tag} = new @HtmlTag(${is_svg ? 'true' : 'false'});`);
5959

6060
if (this.renderer.options.hydratable) {
61-
block.chunks.claim.push(b`${html_tag} = @claim_html_tag(${_parent_nodes}, ${is_svg ? 'true' : 'false'});`);
61+
block.chunks.claim.push(b`${html_tag} = @claim_html_tag(${_parent_nodes}, ${init}, ${is_svg ? 'true' : 'false'});`);
6262
}
6363
block.chunks.hydrate.push(b`${html_tag}.a = ${update_anchor};`);
6464
block.chunks.mount.push(b`${html_tag}.m(${init}, ${parent_node || '#target'}, ${parent_node ? null : '#anchor'});`);

src/runtime/internal/dom.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,7 @@ function find_comment(nodes, text, start) {
492492
return nodes.length;
493493
}
494494

495-
496-
export function claim_html_tag(nodes, is_svg: boolean) {
495+
export function claim_html_tag(nodes, html: string, is_svg: boolean) {
497496
// find html opening tag
498497
const start_index = find_comment(nodes, 'HTML_TAG_START', 0);
499498
const end_index = find_comment(nodes, 'HTML_TAG_END', start_index);
@@ -506,6 +505,17 @@ export function claim_html_tag(nodes, is_svg: boolean) {
506505
detach(html_tag_nodes[0]);
507506
detach(html_tag_nodes[html_tag_nodes.length - 1]);
508507
const claimed_nodes = html_tag_nodes.slice(1, html_tag_nodes.length - 1);
508+
509+
let ssr_html = '';
510+
for (const n of claimed_nodes) {
511+
ssr_html += n.outerHTML;
512+
}
513+
if (ssr_html !== html) {
514+
for (const n of claimed_nodes) {
515+
detach(n);
516+
}
517+
return new HtmlTagHydration(undefined, is_svg);
518+
}
509519
for (const n of claimed_nodes) {
510520
n.claim_order = nodes.claim_info.total_claimed;
511521
nodes.claim_info.total_claimed += 1;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h2>Hello World</h2>
2+
<div>xxxxxxx</div><span>yyyy<b>yyyy</b>yyyy</span><p>zzzzzz</p>
3+
<p id="p">Bye World</p>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<h1>Hello World</h1>
2+
<!-- HTML_TAG_START -->
3+
<div>aaaaaaa</div>
4+
<div>bbbbbbb</div>
5+
<p>cccccc</p>
6+
<span>eeee<b>eee</b>eeee</span>
7+
<!-- HTML_TAG_END -->
8+
<p id="p">Bye World</p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default {
2+
snapshot(target) {
3+
const p = target.querySelector('#p');
4+
5+
return {
6+
p
7+
};
8+
},
9+
10+
test(assert, target, snapshot) {
11+
const p = target.querySelector('#p');
12+
13+
assert.equal(p, snapshot.p);
14+
}
15+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
const content = `<div>xxxxxxx</div>
3+
<span>yyyy<b>yyyy</b>yyyy</span>
4+
<p>zzzzzz</p>
5+
`;
6+
</script>
7+
8+
<h2>Hello World</h2>
9+
{@html content}
10+
<p id="p">Bye World</p>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>Hello World</h1>
2+
<div>xxxxxxx</div><span>yyyy<b>yyyy</b>yyyy</span><p>zzzzzz</p>
3+
<p id="p">Bye World</p>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<h1>Hello World</h1>
2+
<!-- HTML_TAG_START -->
3+
<div>aaaaaaa</div>
4+
<div>bbbbbbb</div>
5+
<p>cccccc</p>
6+
<span>eeee<b>eee</b>eeee</span>
7+
<!-- HTML_TAG_END -->
8+
<p id="p">Bye World</p>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default {
2+
snapshot(target) {
3+
const p = target.querySelector('#p');
4+
const h1 = target.querySelector('h1');
5+
6+
return {
7+
p,
8+
h1
9+
};
10+
},
11+
12+
test(assert, target, snapshot) {
13+
const p = target.querySelector('#p');
14+
const h1 = target.querySelector('h1');
15+
16+
assert.equal(p, snapshot.p);
17+
assert.equal(h1, snapshot.h1);
18+
}
19+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
const content = `<div>xxxxxxx</div>
3+
<span>yyyy<b>yyyy</b>yyyy</span>
4+
<p>zzzzzz</p>
5+
`;
6+
</script>
7+
8+
<h1>Hello World</h1>
9+
{@html content}
10+
<p id="p">Bye World</p>

0 commit comments

Comments
 (0)