Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pinia-plugin-persistedstate does not update cookies and localStorage in Nuxt 3 (Prod, SSR) #386

Open
5 tasks done
YuliyaMinsk opened this issue Feb 27, 2025 · 0 comments
Open
5 tasks done
Labels
🔍️ pending triage This issue needs to be looked into

Comments

@YuliyaMinsk
Copy link

Are you using Nuxt?

  • Check this box if you encountered the issue using Nuxt and the included module.

Describe the bug

I am using pinia-plugin-persistedstate in a Nuxt 3 project to persist state in cookies. In dev mode, everything works fine, but in production (SSR), the cookies do not update, even though the Pinia state changes.

❗ Additionally:
If I change storage: 'localStorage', data does not get stored in localStorage in production, even though console.log shows the updated state.

Expected behavior

After updating the Pinia state:

In DevTools → Application → Storage → Cookies, the cookie value should be updated.
In DevTools → Application → Storage → LocalStorage, the stored data should be updated.

Steps to reproduce

Install pinia-plugin-persistedstate in Nuxt 3 (npm i pinia-plugin-persistedstate).

Configure persist in nuxt.config.ts:

export default defineNuxtConfig({
  modules: ['@pinia/nuxt', 'pinia-plugin-persistedstate/nuxt'],
  piniaPluginPersistedstate: {
    storage: 'cookies', // Or 'localStorage'
    cookieOptions: {
      sameSite: 'lax',
      secure: false,
      path: '/',
      maxAge: 60 * 60 * 24 * 7, // 7 days
    },
  },
});

Create a Pinia store:

import { defineStore } from 'pinia';

export const useMainStore = defineStore('main', {
  state: () => ({
    currentCompany: null,
  }),
  actions: {
    setCurrentCompany(payload) {
      console.log('🔄 Updating currentCompany:', payload);
      this.currentCompany = payload;
    },
  },
  persist: true,
});

Run the app in prod mode:

npm run build && npm run start

Change the state (setCurrentCompany) and check Cookies/LocalStorage.

Actual behavior

In dev mode: Everything works → document.cookie and localStorage update correctly.
In prod mode: console.log confirms state changes, but Cookies and LocalStorage do not update.

What I have tried

✅ Manually updating cookies using useCookie() – works.
✅ Clearing old cookies before updating – does not help.
✅ Forcing $patch() in Pinia – does not help.
✅ Disabling persist in SSR (persist: process.client ? true : false) – does not help.

Versions

nuxt: 3.15.4
@pinia/nuxt: 0.10.1
pinia-plugin-persistedstate: 4.2.0
node.js: 20.15.1

Additional Information

SSR is enabled (ssr: true).
Cookies only update if manually set using useCookie().
No console errors, but cookies and localStorage do not update in production.

Question

🔹 Is this a bug in pinia-plugin-persistedstate, or am I missing something?
🔹 Why does useCookie() work, but persistedstate does not?

Any help would be greatly appreciated!

Reproduction

The issue only occurs after running npm run build && npm run start, making it impossible to reproduce in an online playground like StackBlitz

System Info

System:
    OS: macOS 15.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 313.69 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.15.1 - ~/.nvm/versions/node/v20.15.1/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.9.0 - ~/.nvm/versions/node/v20.15.1/bin/npm
  Browsers:
    Chrome: 133.0.6943.141
    Safari: 18.1

Used Package Manager

yarn

Validations

@YuliyaMinsk YuliyaMinsk added the 🔍️ pending triage This issue needs to be looked into label Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔍️ pending triage This issue needs to be looked into
Projects
None yet
Development

No branches or pull requests

1 participant