[vite-ssr] How can I fix localStorage is not defined
See original GitHub issueDescribe the bug
Hi! so i am having several issues regarding implementing the pinia-persisted-state.
Environment: vue3.2.39+pinia2.0.22+pinia-plugin-persistedstate2.2.0+vite3.0.9+vite-plugin-ssr0.4.38
code
import { reactive } from "vue";
import { defineStore } from "pinia";
const user = reactive({
authKey: "",
});
export const useUserStore = defineStore({
id: "user",
state: () => user,
actions: {
setAuthKey(newAuthKey: string) {
this.authKey = newAuthKey;
// console.log("haha");
},
},
persist: true,
});
first issue:
I can’t really seem to import persist:true
, this is the issue that i’m having
Object literal may only specify known properties, and 'persist' does not exist in type 'DefineStoreOptions<"user", { authKey: string; }, {}, { setAuthKey(newAuthKey: string): void; }>'
second issue: Upon compiling the app, this is the error that occurs:
ReferenceError: localStorage is not defined
at /Users/clarissaaudrey/Documents/GitHub/dashboard-2.0.0/node_modules/pinia-plugin-persistedstate/dist/index.js:160:19
at Array.map (<anonymous>)
at /Users/clarissaaudrey/Documents/GitHub/dashboard-2.0.0/node_modules/pinia-plugin-persistedstate/dist/index.js:158:155
at /Users/clarissaaudrey/Documents/GitHub/dashboard-2.0.0/node_modules/pinia/dist/pinia.cjs:1644:43
at EffectScope.run (/Users/clarissaaudrey/Documents/GitHub/dashboard-2.0.0/node_modules/@vue/reactivity/dist/reactivity.cjs.js:37:24)
at /Users/clarissaaudrey/Documents/GitHub/dashboard-2.0.0/node_modules/pinia/dist/pinia.cjs:1644:33
at Array.forEach (<anonymous>)
at createSetupStore (/Users/clarissaaudrey/Documents/GitHub/dashboard-2.0.0/node_modules/pinia/dist/pinia.cjs:1631:14)
at createOptionsStore (/Users/clarissaaudrey/Documents/GitHub/dashboard-2.0.0/node_modules/pinia/dist/pinia.cjs:1222:13)
at Module.useStore (/Users/clarissaaudrey/Documents/GitHub/dashboard-2.0.0/node_modules/pinia/dist/pinia.cjs:1708:17)
can you help me fix this issue?
Thanks beforehand!
Reproduction
https://github.com/ClarissaAudrey/pinia-persisted-issue
System Info
npm install
npm run dev
Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guide.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- The provided reproduction is a minimal reproducible of the bug.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
React - “localStorage is not defined” error showing
After this, I am getting the following error: ReferenceError: localStorage is not defined. Please help how to resolve it. My package.json :
Read more >How to Fix "localStorage is not defined" in Next.js
How to fix this issue. This will ensure that localStorage is defined before you attempt to access it. useEffect will only run on...
Read more >should use storage.local over window.localStorage #49 - GitHub
Problem This template currently uses localStorage(1, 2). MDN advises against using local storage because of persistence issues: Although ...
Read more >Window.localStorage - Web APIs | MDN
localStorage is similar to sessionStorage , except that while ... a web server) the requirements for localStorage behavior are undefined and ...
Read more >Server Error: ReferenceError: (localstorage) is not defined
You need to move the declaration of selectedCategoryName out of the if block. ... Try retrieving the local storage items with getstaticprops at...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @hamedg68 , I have came up with a solution, not sure if it is applicable to you, so what i did is i render the
localStorage
only on the client side. Or you can try the cookie option that @prazdevs suggested.@ClarissaAudrey No problem hopefully that solves most of your problems or gives you hints to solve them. Feel free to close the issue if you’re fine with the answer 👍
@hamedg68
localStorage
is a client API, it can never be used/accessed server side. I recommend you use some other things such as cookies. But I have no clue howvite-plugin-ssr
works, and the docs are really putting me off it 😕