You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+66-14Lines changed: 66 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,32 +29,66 @@ By default, `svelte-hmr` will trigger a full browser reload when it detects an e
29
29
30
30
#### noPreserveState
31
31
32
+
**Deprecated: removed and default changed from version 0.12. Use `preserveState` instead.**
33
+
34
+
#### preserveState
35
+
32
36
Type: `bool`<br>
33
37
Default: `false`
34
38
35
-
Prevent preserving the state of the component (i.e. value of props and `let` variables) across HMR updates.
36
-
37
-
Note that, independently of this option, the state of child components of a component that is impacted by a HMR update will never be preserved beyond what is re-injected by props, and the state of parent / sibling components will always be preserved (more accurately: parent and siblings are not affected by HMR updates). Read the HMR explanation bellow if you want to understand why.
39
+
Enable [preservation of local state](#preservation-of-local-state) for all variables in all components.
Escape hatch from preservation of local state. There are some situation where preservation of state gets in the way, typically when you want to change the initial / default value of a prop or local variable. If this string appears anywhere in the component's code, then state won't be preserved for this update.
46
+
Force disable preservation of local statefor this component.
45
47
46
-
You'd generally use it with a quick comment right where you are currently editing the code, and remove it just after saving the file:
48
+
This flag has priority over all other settings of state preservation. If it is present, all the state of the component will be reset on the next update, regardless of the value of all the other state preservation settings.
47
49
48
-
```js
49
-
let answer =42// @!hmr
50
+
```svelte
51
+
<!-- @hmr:reset -->
52
+
53
+
<script>
54
+
'@hmr:reset'
55
+
56
+
// @hmr:reset
57
+
</script>
50
58
```
51
59
52
-
But you can also make it more permanent if you find that some of your components don't play with state preservation. Maybe use a noop string to clearly manifest your intention?
60
+
#### preserveAllLocalStateKey
61
+
62
+
Type: `string`<br>
63
+
Default: `'@hmr:keep-all'`
64
+
65
+
Force preservation of all local variables of this component.
53
66
54
67
```svelte
68
+
<!-- @hmr:keep-all -->
69
+
55
70
<script>
56
-
'@!hmr'
57
-
...
71
+
'@hmr:keep-all'
72
+
73
+
// @hmr:keep-all
74
+
</script>
75
+
```
76
+
77
+
#### preserveLocalStateKey
78
+
79
+
Type: `string`<br>
80
+
Default: `'@hmr:keep'`
81
+
82
+
Force preservation of a given local variable in this component.
83
+
84
+
```svelte
85
+
<script>
86
+
// @hmr:keep
87
+
let x = 0
88
+
89
+
let y = 0 // @hmr:keep
90
+
91
+
x = 1 // @hmr:keep
58
92
</script>
59
93
```
60
94
@@ -118,7 +152,9 @@ Now, the best way to see what it can do for you is probably to checkout the temp
118
152
119
153
### Preservation of local state
120
154
121
-
Local state is preserved by Svelte HMR, that is any state that Svelte itself tracks as reactive (basically any root scope `let` vars, exported or not).
155
+
**From version 0.12** this behaviour has been deemed too confusing and hard to anticipate, so preservation of state is now disabled by default, and some escape hatches to preserve the state of some given variables have been added.
156
+
157
+
Local state can be preserved by Svelte HMR, that is any state that Svelte itself tracks as reactive (basically any root scope `let` vars, exported or not).
122
158
123
159
This means that in code like this:
124
160
@@ -133,9 +169,25 @@ This means that in code like this:
133
169
134
170
If you replace `let x = 1` by `let x = 10` and save, the previous value of `x` will be preserved. That is, `x` will be 2 and not 10. The restoration of previous state happens _after_ the init code of the component has run, so the value will not be 11 either, despite the `x++` that is still here.
135
171
136
-
If you find this behaviour inconvenient, you can disable it by setting `noPreserveState` option of your HMR-enabled bundler plugin.
172
+
If you want this behaviour for all the state of all your components, you can enable it by setting the `preserveLocalState` option to `true`.
173
+
174
+
If you then want to disable it for just one particular file, or just temporarily, you can turn it off by adding a `// @hmr:reset` comment somewhere in your component.
137
175
138
-
If you want to disable it for just one particular file, or just temporarily, you can turn it off by adding a `// @!hmr` comment somewhere in your component.
176
+
On the contrary, if you keep the default `preserveLocalState` to `false`, you can enable preservation of all the local state of a given component by adding the following comment: `// @hmr:keep-all`. You can also preserve only the state of some specific variables, by annotating them with: `// @hmr:keep`.
0 commit comments