prazdevs / pinia-plugin-persistedstate

💾 Configurable persistence and rehydration of Pinia stores.
https://prazdevs.github.io/pinia-plugin-persistedstate/
MIT License
2.12k stars 121 forks source link

Not persisting if store is used in plugin with Nuxt 3 #190

Closed ahmad-afiquddin closed 1 year ago

ahmad-afiquddin commented 1 year ago

Describe the bug

Trying to use store in a plugin but I found out that states do not persist when a store is used in a plugin with Nuxt 3. When removed from plugin, persist works. Followed nuxt 3 doc to use pinia in plugin nuxt 3 doc

// store/test.ts

export const useTestStore = defineStore('test-store', {
  state: () => ({
    count: 0,
  }),
  actions: {
    addCount() {
      this.count++;
    },
  },
  persist: {
    storage: persistedState.localStorage,
  },
});
// plugin/store.ts
export default defineNuxtPlugin(({ $pinia }) => {
  return {
    provide: {
      store: useTestStore($pinia),
    },
  };
});

Reproduction

https://stackblitz.com/edit/nuxt-starter-lvetcm?file=plugins%2Fstore.ts,nuxt.config.ts,pages%2Findex.vue,store%2Ftest.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm

Used Package Manager

npm

Validations

GaoNeng-wWw commented 1 year ago

same

prazdevs commented 1 year ago

Hi, I looked into it, and you should not declare your plugin in the nuxt.config.ts as specified in the nuxt docs https://nuxt.com/docs/guide/directory-structure/plugins

Just remove it, and Nuxt will find it and the plugin will also work with persistence

GaoNeng-wWw commented 1 year ago

Hi, I looked into it, and you should not declare your plugin in the nuxt.config.ts as specified in the nuxt docs https://nuxt.com/docs/guide/directory-structure/plugins

Just remove it, and Nuxt will find it and the plugin will also work with persistence

https://stackblitz.com/edit/nuxt-starter-5kqjqh?file=plugins%2Fstore.ts,nuxt.config.ts,pages%2Findex.vue,store%2Ftest.ts

look like it's solved❤

prazdevs commented 1 year ago

Nice ! Considering it closed ! 🎉

jeanmatthieud commented 1 year ago

I have the same issue with Nuxt2, do you have a workaround ?

jeanmatthieud commented 1 year ago

Ok I found the issue. The nuxt plugins are loaded in a alphabetical order.

I believe that your plugin had this information in the official documentation. I think it could be interesting to add it again for Nuxt2 users like myself.