Not getting HMR to work with Vite, Vue3 and Pinia #1290
Replies: 8 comments 8 replies
-
Anybody has the idea? |
Beta Was this translation helpful? Give feedback.
-
I am experiencing the same issue. All parts of my Vue/vite app get reloaded automatically on save, only when i change something in a Pinia store, it is not picked up. |
Beta Was this translation helpful? Give feedback.
-
Just adding on to this - I'm getting this issue when trying to use a store in a component and I've isolated this down to using any Vue 3.2.41 |
Beta Was this translation helpful? Give feedback.
-
I've noticed this behavior with Pinia on a few projects using Vue 3, Typescript, and Quasar 2 (2.10.2 right now). I also tried this solution from Pinia and have not had Vite HMR picking up state changes made in code. Definitely frustrating, and I'll be watching for any discussion on this. After looking into this a bit further, this answer from StackOverflow cross-references #843 which states in the comment by @posva on May 13th:
In short, it appears to be intended functionality that updating the state data in the code does not update state in the browser, but for Vite projects you'll still want to add acceptHMRUpdate as per the docs. |
Beta Was this translation helpful? Give feedback.
-
I had to solve this in a project I was working on, and I'll admit finding the answer required me to deduce the root cause in the code rather than finding it somewhere tucked in comments on a thread. To keep someone else from having this issue I'll give as extensive an answer as possible here. If you're having this issue I hope this finds you! I'm going to break down the example repo provided by the OP: https://github.com/christianhengst/vue-vite-pinia-hmr. The entry point for the application is the import { createApp } from 'vue';
import { createPinia } from 'pinia';
import App from './App.vue';
const app = createApp(App);
app.use(createPinia());
app.mount('#app'); In this file the App.vue component is imported, and it is used to create the application. Within the App it imports a Pinia store. import { useStore } from './stores/counter'; Because this store, in the context of the component, requires Pinia the import needs to occur after the application has been designed to use pinia import { defineStore, acceptHMRUpdate } from 'pinia'
export const useStore = defineStore('counter', {}) That Sometimes, and even in this thread you'll see a recommendation to lazy load the component. This works, but why? Because the file is not imported directly, so it does not execute it's own imports right away. That's really all this is about. It's just timing of when you use what dependency, and once In more elaborate occurrences of this issue you'll see it when you try and use stores outside of components, like in route guards, so a good rule of thumb you can follow is to always async import your routes. This generally solves the issue by enforcing a code execution sequence that will work harmoniously with Pinia. You could also use wrapper functions and create reference to the store that gets defined later. Honestly, I won't go into how to do this as I think the best solution really is to async load your route components. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Yup! Lazy loading the routes is exactly what I’m referring to.Regards,WilliamOn Jun 16, 2023, at 6:16 AM, Gianpaolo Pascasi ***@***.***> wrote:
What do you mean by "async import your routes"? Do you mean importing the components lazily in the createRouter function or something else?
Thanks 😄
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Pour ceux et celle qui ont le problème des magasins qui ne se mettent pas à jour, j'ai la solution !! Voici le lien de la documentation officiel Pinia qui répond à ce problème : https://pinia.vuejs.org/cookbook/hot-module-replacement.html |
Beta Was this translation helpful? Give feedback.
-
Any solution for this? |
Beta Was this translation helpful? Give feedback.
-
Hi there!
I'm switching from Vuex to Pinia but having issues with "Hot Module Replacement". When changing the state in a store it is not hot updated in the component that is using the store.
I'm using single file components with the "Options API" and tried different approaches with
...mapState()
andsetup()
with no effect.I've created a basic repository that demonstrates the issue: https://github.com/christianhengst/vue-vite-pinia-hmr
How to reproduce
npm i
npm run dev
counterFromComponentData
inApp.vue
counter
instores/counter.js
Do I get something basically wrong or is there an issue with either Vite or Pinia?
Thanks for your help!
Beta Was this translation helpful? Give feedback.
All reactions