Skip to content

Commit 0d83307

Browse files
authored
fix: ensure HMR doesn't mess with anchor nodes (#12242)
The refactoring in #12215 didn't take HMR into account. As a result, the anchor was passed to the HMR block, which was subsequently cleaned up on destroy - but the anchor could be shared with other components and therefore needs to stay in the dom. Passing `null` instead solves the problem. Fixes #12228 Fixes #12230 Fixes #12233
1 parent 434e1ad commit 0d83307

File tree

6 files changed

+34
-1
lines changed

6 files changed

+34
-1
lines changed

.changeset/tough-buckets-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure HMR doesn't mess with anchor nodes

packages/svelte/src/internal/client/dev/hmr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function hmr(source) {
1818
/** @type {import("#client").Effect} */
1919
let effect;
2020

21-
block(anchor, 0, () => {
21+
block(null, 0, () => {
2222
const component = get(source);
2323

2424
if (effect) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
B
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
mode: ['client'],
6+
compileOptions: {
7+
hmr: true,
8+
dev: true
9+
},
10+
test({ target, assert }) {
11+
assert.htmlEqual(target.innerHTML, `<button>switch</button> A`);
12+
13+
target.querySelector('button')?.click();
14+
flushSync();
15+
assert.htmlEqual(target.innerHTML, `<button>switch</button> B`);
16+
}
17+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
import A from './A.svelte';
3+
import B from './B.svelte';
4+
5+
let component = $state(A);
6+
</script>
7+
8+
<button onclick={() => (component = B)}>switch</button>
9+
<svelte:component this={component} />

0 commit comments

Comments
 (0)