robinvdvleuten / vuex-persistedstate

💾 Persist and rehydrate your Vuex state between page reloads.
https://npm.im/vuex-persistedstate
MIT License
5.77k stars 378 forks source link

Exemple using cookie-universal-nuxt #444

Open mrleblanc101 opened 2 years ago

mrleblanc101 commented 2 years ago

Would it be possible to add an exemple using cookie-universal-nuxt instead of cookie and js-cookies. It combine both feature into a single plugin and is already a Nuxt module.

mrleblanc101 commented 2 years ago

I think we can just do this, but I'm not sure it will handle every edge case:

// ~/plugins/persistedState.js

import createPersistedState from 'vuex-persistedstate';

export default ({ store, $cookies }) => {
    createPersistedState({
        paths: [...],
        storage: {
            getItem: (key) => $cookies.get(key),
            setItem: (key, value) => $cookies.set(key, value, { expires: 365, secure: false }),
            removeItem: key => $cookies.remove(key)
        }
    })(store);
};