0.0.4 • Published 3 years ago

luo-vuex-persistedstate v0.0.4

Weekly downloads
1
License
MIT
Repository
-
Last release
3 years ago

luo-vuex-persistedstate

Permanent storage vuex status

Install

npm install --save luo-vuex-persistedstate

Usage

import createPersistedState from 'luo-vuex-persistedstate'

const store = new Vuex.Store({
  // ...
  plugins: [createPersistedState()],
})

Nuxt.js

// nuxt.config.js

...
plugins: [{ src: '~/plugins/localStorage.js', ssr: false }]
...
// ~/plugins/localStorage.js

import createPersistedState from 'luo-vuex-persistedstate'

export default ({store}) => {
  window.onNuxtReady(() => {
    createPersistedState({
        key: 'yourkey',
        paths: [...]
        ...
    })(store)
  })
}

API

createPersistedState([options])

  • key <String>: The key to store the persisted state under. (default: vuex)
  • secondKey <String>: Keys to store persistent state (level 2). (default: vuexData)
  • paths <Array>: An array of any paths to partially persist the state. If no paths are given, the complete state is persisted. Paths must be specified using dot notation. If using modules, include the module name. eg: "auth.user" (default: [])
  • reducer <Function>: A function that will be called to reduce the state to persist based on the given paths. Defaults to include the values.
  • subscriber <Function>: A function called to setup mutation subscription. Defaults to store => handler => store.subscribe(handler)

  • storage <Object>: Instead for (or in combination with) getState and setState. Defaults to localStorage.

  • getState <Function>: A function that will be called to rehydrate a previously persisted state. Defaults to using storage.
  • setState <Function>: A function that will be called to persist the given state. Defaults to using storage.
  • filter <Function>: A function that will be called to filter any mutations which will trigger setState on storage eventually. Defaults to () => true
  • arrayMerger <Function>: A function for merging arrays when rehydrating state. Defaults to function (store, saved) { return saved } (saved state replaces supplied state).

Customize Storage

If it's not ideal to have the state of the Vuex store inside localstorage. One can easily implement the functionality to use cookies for that (or any other you can think of);

import { Store } from 'vuex'
import createPersistedState from 'luo-vuex-persistedstate'
import * as Cookies from 'js-cookie'

const store = new Store({
  // ...
  plugins: [
    createPersistedState({
      storage: {
        getItem: key => Cookies.get(key),
        // Please see https://github.com/js-cookie/js-cookie#json, on how to handle JSON.
        setItem: (key, value) =>
          Cookies.set(key, value, { expires: 3, secure: true }),
        removeItem: key => Cookies.remove(key),
      },
    }),
  ],
})

In fact, any object following the Storage protocol (getItem, setItem, removeItem, etc) could be passed:

createPersistedState({ storage: window.sessionStorage })

License

This content is released under the MIT License.