How to implement vuex-persistedstate in vue/ssr

1.2k views Asked by At

I need to persist state in vue ssr app, but can't understand how to implement this.

As storage, I want to use cookies.

When I install the plugin as written in readme nothing happens, that it's strange: I expect an error because js-cookies calls "window".

The question is: how to implement vuex-persistedstate in vue/ssr?

I can access cookies in req.cookies, but I can't set cookies in the browser, and this expected because my store fill on server side, and js-cookies calls on server side.

1

There are 1 answers

0
waghcwb On

Until the they fix this in source code, I managed this way:

storage.js

import CreatePersistedState from 'vuex-persistedstate'

let PersistedState

if (process.browser) {
  PersistedState = CreatePersistedState(yourOptions)
}

export default PersistedState

store.js

import CreatePersistedState from 'src/util/plugins/storage'
...

const plugins = []

if (process.browser) {
  plugins.push(CreatePersistedState)
}

export default function createStore() {
  return new Vuex.Store({
    state,
    modules,
    actions,
    mutations,
    getters,
    strict: false,
    plugins: plugins
  })
}