Skip to content

Commit

Permalink
fix: ensure HMR doesn't mess with anchor nodes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dummdidumm committed Jul 1, 2024
1 parent 434e1ad commit aa1720c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tough-buckets-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure HMR doesn't mess with anchor nodes
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/dev/hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function hmr(source) {
/** @type {import("#client").Effect} */
let effect;

block(anchor, 0, () => {
block(null, 0, () => {
const component = get(source);

if (effect) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
B
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
mode: ['client'],
compileOptions: {
hmr: true,
dev: true
},
test({ target, assert }) {
assert.htmlEqual(target.innerHTML, `<button>switch</button> A`);

target.querySelector('button')?.click();
flushSync();
assert.htmlEqual(target.innerHTML, `<button>switch</button> B`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
import A from './A.svelte';
import B from './B.svelte';
let component = $state(A);
</script>

<button onclick={() => (component = B)}>switch</button>
<svelte:component this={component} />

0 comments on commit aa1720c

Please sign in to comment.