Replies: 1 comment
-
Ah right, I guess Vite is adding a random querystring to the file to bust the cache so it just downloads a new version of the In any case, there's a deeper problem here and it's that this counter will be shared across different requests in your server. In order to avoid that, you need to create a new counter for every request. You can do this from Vite SSR main hook and save it in initialState.myState = import.meta.env.SSR
? reactive({ counter: 0 })
: reactive(initialState.myState) Then, get that request-scoped counter with import { useContext } from 'vite-ssr'
export function useCounter() {
const {
initialState: { myState },
} = useContext()
return {
decrement() {
myState.counter--
},
increment() {
myState.counter++
},
get value() {
myState state.counter
},
}
} That should be enough to fix the issue I was talking about but if you save the file that is setting |
Beta Was this translation helpful? Give feedback.
-
When I create a composable and HMR is triggered the state just resets.
The first thing that comes to mind is to sync the counter state with initialState in viteSSR method as Pinia does. Is there a clean way to sync composables state when HMR is triggered?
For example:
Beta Was this translation helpful? Give feedback.
All reactions