-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
feat: preserve $state across HMR component swaps #17995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexkahndev
wants to merge
2
commits into
sveltejs:main
Choose a base branch
from
alexkahndev:feat/hmr-preserve-state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'svelte': patch | ||
| --- | ||
|
|
||
| Preserve $state values across HMR component swaps. When a component is hot-replaced, labeled signal values are captured from the old effect tree and restored during the new component's initialization via $.tag(). This matches the state preservation behavior of React Fast Refresh and Vue's rerender(). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/svelte/tests/runtime-runes/samples/hmr-preserve-state-collision/Counter.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <script> | ||
| let count = $state(0); | ||
| </script> | ||
|
|
||
| <button class="child" onclick={() => count += 1}> | ||
| child: {count} | ||
| </button> |
51 changes: 51 additions & 0 deletions
51
packages/svelte/tests/runtime-runes/samples/hmr-preserve-state-collision/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { flushSync } from 'svelte'; | ||
| import { HMR } from 'svelte/internal/client'; | ||
| import { test } from '../../test'; | ||
|
|
||
| export default test({ | ||
| compileOptions: { | ||
| dev: true, | ||
| hmr: true | ||
| }, | ||
|
|
||
| html: `<button class="parent">parent: 1</button><button class="child">child: 0</button>`, | ||
|
|
||
| test({ assert, target, mod }) { | ||
| const parent_btn = target.querySelector('button.parent'); | ||
| const child_btn = target.querySelector('button.child'); | ||
|
|
||
| // Click parent to increase count to 2 (adds another Counter) | ||
| flushSync(() => parent_btn?.click()); | ||
| assert.htmlEqual( | ||
| target.innerHTML, | ||
| `<button class="parent">parent: 2</button><button class="child">child: 0</button><button class="child">child: 0</button>` | ||
| ); | ||
|
|
||
| // Click the first child counter 5 times | ||
| for (let i = 0; i < 5; i++) { | ||
| flushSync(() => target.querySelector('button.child')?.click()); | ||
| } | ||
|
|
||
| assert.htmlEqual( | ||
| target.innerHTML, | ||
| `<button class="parent">parent: 2</button><button class="child">child: 5</button><button class="child">child: 0</button>` | ||
| ); | ||
|
|
||
| // Simulate HMR swap on the parent component. | ||
| // Without collision prevention, the parent's `count` could be | ||
| // overwritten by a child's `count` value, destroying children. | ||
| const hmr_data = mod.default[HMR]; | ||
| if (hmr_data && hmr_data.update) { | ||
| const fake_incoming = /** @type {any} */ ({ [HMR]: { fn: hmr_data.fn, current: null } }); | ||
| hmr_data.update(fake_incoming); | ||
| flushSync(); | ||
| } | ||
|
|
||
| // Parent count should still be 2, not corrupted by child's count | ||
| // Children should still be rendered (2 of them) | ||
| assert.htmlEqual( | ||
| target.innerHTML, | ||
| `<button class="parent">parent: 2</button><button class="child">child: 5</button><button class="child">child: 0</button>` | ||
| ); | ||
| } | ||
| }); |
11 changes: 11 additions & 0 deletions
11
packages/svelte/tests/runtime-runes/samples/hmr-preserve-state-collision/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <script> | ||
| import Counter from './Counter.svelte'; | ||
| let count = $state(1); | ||
| </script> | ||
|
|
||
| <button class="parent" onclick={() => count++}>parent: {count}</button> | ||
|
|
||
| {#each Array(count) as _} | ||
| <Counter /> | ||
| {/each} |
31 changes: 31 additions & 0 deletions
31
packages/svelte/tests/runtime-runes/samples/hmr-preserve-state-composable/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { flushSync } from 'svelte'; | ||
| import { HMR } from 'svelte/internal/client'; | ||
| import { test } from '../../test'; | ||
|
|
||
| export default test({ | ||
| compileOptions: { | ||
| dev: true, | ||
| hmr: true | ||
| }, | ||
|
|
||
| html: `<button>count: 0</button>`, | ||
|
|
||
| test({ assert, target, mod }) { | ||
| const button = target.querySelector('button'); | ||
|
|
||
| // Click 3 times | ||
| flushSync(() => button?.click()); | ||
| flushSync(() => button?.click()); | ||
| flushSync(() => button?.click()); | ||
| assert.htmlEqual(target.innerHTML, `<button>count: 3</button>`); | ||
|
|
||
| // HMR swap the parent component — composable state should be preserved | ||
| const hmr_data = mod.default[HMR]; | ||
| const fake_incoming = /** @type {any} */ ({ [HMR]: { fn: hmr_data.fn, current: null } }); | ||
| hmr_data.update(fake_incoming); | ||
| flushSync(); | ||
|
|
||
| // Composable's $state should be preserved across parent HMR | ||
| assert.htmlEqual(target.innerHTML, `<button>count: 3</button>`); | ||
| } | ||
| }); |
7 changes: 7 additions & 0 deletions
7
packages/svelte/tests/runtime-runes/samples/hmr-preserve-state-composable/counter.svelte.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export function createCounter() { | ||
| let count = $state(0); | ||
| return { | ||
| get count() { return count; }, | ||
| increment() { count++; } | ||
| }; | ||
| } |
6 changes: 6 additions & 0 deletions
6
packages/svelte/tests/runtime-runes/samples/hmr-preserve-state-composable/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <script> | ||
| import { createCounter } from './counter.svelte.js'; | ||
| const counter = createCounter(); | ||
| </script> | ||
|
|
||
| <button onclick={() => counter.increment()}>count: {counter.count}</button> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.