pinia-plugin-persistedstate
pinia-plugin-persistedstate copied to clipboard
Persistence doesn't work?
Describe the bug
Hello, I wanted to try this out. Below is minimum reproducible code. Using Nuxt 3.
The store loses its state after seemingly refreshing or going to another page. It also doesn't save it to cookies as it should. calling store.$persist()
throws an error saying it doesn't exist.
What have I done wrong? Used this for reference: https://prazdevs.github.io/pinia-plugin-persistedstate/frameworks/nuxt-3.html
nuxt.config.ts:
export default defineNuxtConfig({
devtools: { enabled: true },
modules: [
"@nuxt/ui",
"@pinia/nuxt", // needed
"@pinia-plugin-persistedstate/nuxt",
],
ui: {
icons: ["tdesign"],
},
piniaPersistedstate: {
cookieOptions: {
expires: new Date(new Date().getTime() + 400 * 24 * 60 * 60 * 1000),
},
storage: "cookies",
debug: true,
},
build: {
transpile: ["pinia-plugin-persistedstate"],
},
});
stores/account.ts
import { defineStore } from "pinia";
export const useAccountStore = defineStore({
id: "accountStore",
state: () => {
const email = ref("");
const password = ref("");
const role = ref("");
return { email, password, role };
},
actions: {},
});
login.vue
<script setup lang="ts">
const store = useAccountStore();
// fetch request somewhere
const res = await response.json();
store.$patch({ // updated store inside vue edge browser extension but not in the cookie
email: res.email,
password: res.password,
role: res.userrole
})
</script>
Reproduction
https://stackblitz.com/edit/nuxt-starter-gpnkto?file=app.vue
System Info
System:
OS: Windows 11 10.0.22621
CPU: (24) x64 13th Gen Intel(R) Core(TM) i7-13700K
Memory: 12.25 GB / 31.78 GB
Binaries:
Node: 21.5.0 - C:\Program Files\nodejs\node.EXE
npm: 10.2.1 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Chromium (121.0.2277.83)
Internet Explorer: 11.0.22621.1
Used Package Manager
npm
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guide.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] The provided reproduction is a minimal reproducible of the bug.
Facing the same issue even after switching to localstorage on nuxt global config.
Facing the same issue even after switching to localstorage on nuxt global config.
Hey, turns out global config can be completely ignored. I added my persistence config to my store directly and it works now.
You must add the persist property to your store with a boolean value and by default it will save the value in the cookies