Search code examples
javascriptvue.jsnuxt.jsvuex

localStorage in Nuxt js is not working in SSR mode


I'm trying to add items in a cart using vuex but getting some error in the console and the product in the page is not showing as well. Please tell me how to solve this issue. here is the error in console

client.js?06a0:103 SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at Store.initializeStore (index.js?9101:14)
    at wrappedMutationHandler (vuex.esm.js?2f62:844)
    at commitIterator (vuex.esm.js?2f62:466)
    at Array.forEach (<anonymous>)
    at eval (vuex.esm.js?2f62:465)
    at Store._withCommit (vuex.esm.js?2f62:624)
    at Store.commit (vuex.esm.js?2f62:464)
    at Store.boundCommit [as commit] (vuex.esm.js?2f62:409)
    at VueComponent.mounted (default.vue?ec86:82)

Here is the store.js where I'm trying to use localStorage

export const state = () => ({
    cart:{
        items:[],
    },
    isAuthenticated: false,
    token: '',
    isLoading: false,

})
  
export const mutations = {
    initializeStore(state) {
        if (localStorage.getItem('cart')) {
          state.cart = JSON.parse(localStorage.getItem('cart'))
        } else {
          localStorage.setItem('cart', JSON.stringify(state.cart))
        }
    },
    addToCart(state, item) {
        const exists = state.cart.items.filter(i => i.product.id === item.product.id)
        if (exists.length) {
          exists[0].quantity = parseInt(exists[0].quantity) + parseInt(item.quantity)
        } else {
          state.cart.items.push(item)
        }
        localStorage.setItem('cart', JSON.stringify(state.cart))
    },

}

export const actions = {

}

Here is the default.vue where I'm trying to access localStorage from vuex

export default {
  data(){
    return{
      title:'QL Gadgets',
      cart:{
        items:[]
      },
    }
  },
  mounted(){
    this.$store.commit('initializeStore')
  },
  computed:{
    cartTotalLenght(){
      let totalLenght = 0
      for(let i=0; i < this.cart.items.length; i++){
        totalLenght += this.cart.items[i].quantity
      }
      return totalLenght
    }
  },
  methods:{
    handleClick(){
        if(this.$refs.menu.classList.contains('hidden')){
            this.$refs.menu.classList.remove('hidden')
        }
        else{
            this.$refs.menu.classList.add('hidden')
        }
    }
  },
}

Solution

  • The issue here is likely that at some point you set an string in local storage that is not JSON-parseable or there is nothing in the response (undefined starts with u). Check your local storage with your chosen browser (e.g. Chrome or Firefox) to see if this is the case.

    In any event, you should handle errors reading from local storage or parsing the result. For example, the first time a user visits the page there won't be anything there and JSON.parse will fail. For example:

    export const mutations = {
      initializeStore(state) {
        try {
          state.cart = JSON.parse(localStorage.getItem('cart'))
        } catch {
          localStorage.setItem('cart', JSON.stringify(state.cart))
        }
      }
    }