question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[vite-ssr] How can I fix localStorage is not defined

See original GitHub issue

Describe 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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ClarissaAudreycommented, Sep 30, 2022

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.

0reactions
prazdevscommented, Sep 29, 2022

@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

I have the same problem with vite-plugin-ssr @ClarissaAudrey, can you find a way to use pinia-plugin-persistedstate with ite-plugin-ssr to store data in localstorage which is fetched in serverside?

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 how vite-plugin-ssr works, and the docs are really putting me off it 😕

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found