[plugin-svelte] Fast Refresh doesn't recognize variable value changes via HMR #1567
Replies: 13 comments 11 replies
-
This might actually be fast refresh not working, not HMR, curious to see since the terminology can be confusing haha. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Thanks - I have a repro repo here: I did not know there was a fast refresh distinct from hmr. |
Beta Was this translation helpful? Give feedback.
-
@melissamcewen @FredKSchott @stramel @UARTman - just changing the text Hello to Hi does work as desired. Does this determine whether the problem is fast refresh versus hmr? |
Beta Was this translation helpful? Give feedback.
-
Since I don't see this brought up, I will reiterate what I said in discussion threads.
I am not sure how much help this provides but this is all I have now. |
Beta Was this translation helpful? Give feedback.
-
paging HMR Guru @rixo :) I think that this is a limitation of https://github.com/rixo/svelte-hmr, the HMR library that we use internally. @rixo anything that we can do here to help? If this isn't possible, but you can detect it, then you could call |
Beta Was this translation helpful? Give feedback.
-
It isn't variable name changes, it's variable value changes. |
Beta Was this translation helpful? Give feedback.
-
Updated title! thanks for the correction |
Beta Was this translation helpful? Give feedback.
-
It's not a bug, it's a feature! This is preservation of local state in action. It's a 2-edged sword. You can turn it off with an option. plugins: [
[
'@snowpack/plugin-svelte',
{
hmrOptions: {
noPreserveState: true,
},
},
],
'@snowpack/plugin-dotenv',
], Without it, though, you wouldn't have the state of the counter in the default Svelte create-snowpack-app example preserved across updates (it would reset to 0 each time): <script>
import {onMount} from 'svelte';
let count = 0;
onMount(() => {
const interval = setInterval(() => count++, 1000);
return () => {
clearInterval(interval);
};
});
</script>
<p>Page has been open for <code>{count}</code> seconds.</p> If this counter was a flag to have a dialog open, for example, on each HMR update the flag would reset and you would have to re-click the open button to see your dialog. With this option, the "in-memory" state is preserved, and the modal stays open. On the other hand, like this issue shows, this can be confusing and it can also get in the way sometimes. Without it, you still benefit from having the state of all the rest of the app -- everything above and around the impacted component(s) -- preserved / unaffected. This is really a question of taste and there is no clear winner, so I'm never satisfied with what should be the default for this option. Some other plugins have defaulted it to There is normally a way to disable it temporarily by putting a Also, in the discussion leading to this issue, the event handler should probably be a <script>
let counter = 5;
const handle = () => {
counter = counter + 1;
};
</script>
Counter:
{counter}
<button on:click={handle}>+1</button> One shouldn't use |
Beta Was this translation helpful? Give feedback.
-
Fixed |
Beta Was this translation helpful? Give feedback.
-
oh interesting! TIL! So just to be clear, this behavior only exists for Thinking out loud: I wonder if there would be some way to keep track of the "original" value of each of these, and then update if a new value was set. Something like:
|
Beta Was this translation helpful? Give feedback.
-
(Moved to discussion, since this is no longer considered a bug.) |
Beta Was this translation helpful? Give feedback.
-
@nous- Oh, yes you're right... The option name is actually ['@snowpack/plugin-svelte', {
hmrOptions: {
preserveLocalState: true
}
}], There is a typo in I think the longer option name is better, because HMR always preserves a lot of app state "around" the impacted component, independently of what it does with local variables. Also, there is some internal state of the components (event listeners, bound variables...) that needs to be transferred in very case. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
As mentioned in discussion #1536 . To replicate use the latest CSA app-template-svelte. Then add this code to your
app.svelte
Start up the server, then try changing the value of
name
. The changes do not show up unless you refresh.Beta Was this translation helpful? Give feedback.
All reactions