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

The localstorage is reset to the value by default only on dev mode #339

Closed mathias22osterhagen22 closed 4 years ago

mathias22osterhagen22 commented 4 years ago

Relevant code or config

import Vue from 'vue'
import Vuex from 'vuex'
import presistedstate from 'vuex-persistedstate'

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        user: {
            token: ''
        },
        nav: {
            tab: 'RCS',
            subtab: '',
        }
    },
    mutations: {
        setUserToken(state, token) {
            console.log('SETTOKENBRO');
            state.user.token = token;
        },
        setNavTab(state, tab)
        {
            console.log('SET nav tab');
            state.nav.tab = tab;
        },
        setNavSubTab(state, subtab)
        {
            console.log('SET sub tab');
            state.nav.subtab = subtab;
        },
        logout(state) {

            console.log('Logout');
            state.user.token = '';
            state.nav.tab = 'RCS';
            state.nav.subtab = '';
        }
    },
    plugins: [presistedstate({
        reducer: (state) => ({
            user: {
                token: state.user.token,
            },
            nav:{
                tab: state.nav.tab,
                subtab: state.nav.subtab
            }
        }),
    })],
    getters:
    {
        getUserToken: state => {
            console.log('get token');
            return state.user.token;
        }
        ,
        getNavTab: state => {
            console.log('get nav');
            return state.nav.tab;
        }
        ,
        getNavSubTab: state => {
            console.log('get subnav');
            return state.nav.subtab;
        }
    }
});

export default store;

What you did: After I logged in I went to check if the token is well stored in the local storage.

What happened:

The token is stored, then he gets erased, whatever I put in the field get erased after x time. sh34eqVZEK

As you can see no log in the console even if I add one in every call of the store's function.

Problem description: The local storage gets "set" again randomly, this doesn't appear on firefox or chrome on the production version and I don't know at all from what this comes. I check the log of the console no commit is made, I checked twice that the store state is not used without getters or commit.

And I put in comment all "setTimeoutInterval" of my code (it was only one anyway).

How can I solve this magic?

mathias22osterhagen22 commented 4 years ago

Ok to the peoples who pass by here with the same problem.

I just restarted Chrome 😒 (be sure to kill all .exe relative to it). I suppose it was some weird caching.