microcipcip / cookie-universal

Universal cookie plugin, perfect for SSR
525 stars 39 forks source link

Cookie get is not working after chanhing route in asynd fetch or asyncData #122

Open Venegrad opened 2 years ago

Venegrad commented 2 years ago

Im using nuxt $cookies for save some data. When some buttons clicked, im updating cookies:

updateTickets(ctx) {
    this.$cookies.set(`payTickets${ctx.getters.getCurGame}`, ctx.getters.getPayTickets, {
      secure: false
    });
    this.$cookies.set(`tickets${ctx.getters.getCurGame}`, ctx.getters.getTickets, {
      secure: false
    });
  },

Cookie reading action from store:

async cookieWatcher(ctx) {
    let tickets = this.$cookies.get(`tickets${ctx.getters.getCurGame}`);
    let payTickets = this.$cookies.get(`payTickets${ctx.getters.getCurGame}`);

    if(!tickets || !tickets.length) tickets = [[]];
    if(!payTickets || !payTickets.length) payTickets = [];

    ctx.commit("setTickets", tickets);
    ctx.commit("setPayTickets", payTickets);
  },

On page im using:

async fetch({store, $axios, route, $auth, error, params}) {
   store.dispatch("games/cookieWatcher");
}

But thats not working. When i leave page and enter anotherm new cookies is not reading, only after page reload by f5, cookies reading.

for fix that, i need to watch route

created() {
  this.$store.dispatch("games/cookieWatcher");
},
watch: {

    '$route.path'() {
      this.$store.dispatch("games/cookieWatcher");
    }
  },

Guess thats error, cuz cookie need correctly reading from async fetch or asyncData

Venegrad commented 2 years ago

error in client side route change, need to fix that, any idia?

microcipcip commented 2 years ago

I don't understand why it doesn't work. Did you check the browser storage to see if the cookie is actually set when clicking the button?

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Venegrad commented 2 years ago

I don't understand why it doesn't work. Did you check the browser storage to see if the cookie is actually set when clicking the button?

asyncData is calling every time, when u enter nuxt page. In asyncData im calling store action store.dispatch("games/cookieWatcher"); what reading cookie store this.$cookies.get(tickets${ctx.getters.getCurGame});