soc221b / pinia-plugin-persistedstate-2

Persist and rehydrate your Pinia state between page reloads.
https://www.npmjs.com/package/pinia-plugin-persistedstate-2
MIT License
91 stars 8 forks source link

Store not being persisted at all #23

Closed ssinno28 closed 2 years ago

ssinno28 commented 2 years ago

Hello, I'm currently using this plugin to persist some of my store to cookies in Nuxt/Vue 3. Here is my implementation:

import Cookies from 'js-cookie';
import cookie from 'cookie';
import { defineNuxtPlugin } from 'nuxt/app';
import { createPersistedStatePlugin } from 'pinia-plugin-persistedstate-2';

export default defineNuxtPlugin(({ $pinia, ssrContext }) => {
  $pinia.use(
    createPersistedStatePlugin({
      includePaths: [
        ['tracking.functional'],
        'auth.accessToken',
        'auth.expiresAt',
        'auth.user',
        'auth.permissions',
        'search.searchValues',
        'search.miles',
        'search.zipCode',
        'search.currentPosition',
        'player',
      ],
      storage: {
        getItem: (key) => {
          // See https://nuxtjs.org/guide/plugins/#using-process-flags
          if (process.server) {
            if (ssrContext.req.headers.cookie) {
              const parsedCookies = cookie.parse(ssrContext.req.headers.cookie);
              return parsedCookies[key];
            }

            return null;
          } else {
            return Cookies.get(key);
          }
        },
        // Please see https://github.com/js-cookie/js-cookie#json, on how to handle JSON.
        setItem: (key, value) => {
          // Remove expires to make cookies into session cookies
          Cookies.set(key, value, {
            expires: 365,
            // secure: !isDev,
            sameSite: true,
          });
        },
        removeItem: (key) => Cookies.remove(key),
      },
    })
  );
});

For some reason includePaths seems to be completely ignored (it is still persisting other stores that are not included in includePaths) and on top of that the tracking and player store are not stored in cookies at all. Here is what my tracking store looks like:

 import { defineStore } from 'pinia';

export const useTrackingStore = defineStore({
  id: 'tracking',
  state: () => ({
    hasResponded: false,
    strictlyNecessary: true,
    analytical: true,
    functional: true,
    targeting: true,
  }),
  actions: {
    setAcceptAll() {
      this.hasResponded = true;
      this.strictlyNecessary = true;
      this.analytical = true;
      this.functional = true;
      this.targeting = true;
    },
    setDeclineAll() {
      this.hasResponded = true;
      this.strictlyNecessary = true;
      this.analytical = false;
      this.functional = false;
      this.targeting = false;
    },
    setAnalytical(value) {
      this.analytical = value;
    },
    setTargeting(value) {
      this.targeting = value;
    },
    setFunctional(value) {
      this.functional = value;
    },
    setHasResponded(value) {
      this.hasResponded = value;
    },
  },
});

Is there a reason why this store would be completely ignored?

soc221b commented 2 years ago

hi, @ssinno28 👋 , thanks for the feedback,

For some reason includePaths seems to be completely ignored ...

This problem is due to the includePaths is not a plugin option, but a store option: https://github.com/iendeavor/pinia-plugin-persistedstate-2#store-options

... on top of that the tracking and player store are not stored in cookies at all ...

It should works 🤔. Can you provide a reproducible demo using stackblitz or code sandbox? thanks

soc221b commented 2 years ago

Close for now, feel free to reopen if the problem still exists. Thank you.