UNPKG

4.48 kBMarkdownView Raw
1# vuex-persistedstate
2
3Persist [Vuex](http://vuex.vuejs.org/) state with [localStorage](https://developer.mozilla.org/nl/docs/Web/API/Window/localStorage).
4
5[![NPM version](https://img.shields.io/npm/v/vuex-persistedstate.svg)](https://www.npmjs.com/package/vuex-persistedstate)
6[![Build Status](https://img.shields.io/travis/robinvdvleuten/vuex-persistedstate.svg)](https://travis-ci.org/robinvdvleuten/vuex-persistedstate)
7
8## Requirements
9
10- [Vue.js](https://vuejs.org) (v2.0.0+)
11- [Vuex](http://vuex.vuejs.org) (v2.0.0+)
12
13## Installation
14
15```bash
16$ npm install vuex-persistedstate
17```
18
19## Usage
20
21[![Edit vuex-persistedstate](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/80k4m2598?autoresize=1)
22
23```js
24import createPersistedState from 'vuex-persistedstate'
25
26const store = new Vuex.Store({
27 // ...
28 plugins: [createPersistedState()]
29})
30```
31
32### Nuxt.js
33It is possible to use vuex-persistedstate with Nuxt.js. Due to the order code is loaded in, vuex-persistedstate must be included as a NuxtJS plugin:
34
35```javascript
36// nuxt.config.js
37
38...
39plugins: [{ src: '~/plugins/localStorage.js', ssr: false }]
40...
41
42```
43```javascript
44// ~/plugins/localStorage.js
45
46import createPersistedState from 'vuex-persistedstate'
47
48export default ({store}) => {
49 createPersistedState({
50 key: 'yourkey',
51 paths: [...]
52 ...
53 })(store)
54}
55
56```
57
58## API
59
60### `createPersistedState([options])`
61
62Creates a new instance of the plugin with the given options. The following options
63can be provided to configure the plugin for your specific needs:
64
65- `key <String>`: The key to store the persisted state under. (default: __vuex__)
66- `paths <Array>`: An array of any paths to partially persist the state. If no paths are given, the complete state is persisted. (default: __[]__)
67- `reducer <Function>`: A function that will be called to reduce the state to persist based on the given paths. Defaults to include the values.
68- `subscriber <Function>`: A function called to setup mutation subscription. Defaults to `store => handler => store.subscribe(handler)`
69
70- `storage <Object>`: Instead for (or in combination with) `getState` and `setState`. Defaults to localStorage.
71- `getState <Function>`: A function that will be called to rehydrate a previously persisted state. Defaults to using `storage`.
72- `setState <Function>`: A function that will be called to persist the given state. Defaults to using `storage`.
73- `filter <Function>`: A function that will be called to filter any mutations which will trigger `setState` on storage eventually. Defaults to `() => true`
74- `arrayMerger <Function>`: A function for merging arrays when rehydrating state. Defaults to `function (store, saved) { return saved }` (saved state replaces supplied state).
75
76## Customize Storage
77
78If it's not ideal to have the state of the Vuex store inside localstorage. One can easily implement the functionality to use [cookies](https://github.com/js-cookie/js-cookie) for that (or any other you can think of);
79
80[![Edit vuex-persistedstate with js-cookie](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/xl356qvvkz?autoresize=1)
81
82```js
83import { Store } from 'vuex'
84import createPersistedState from 'vuex-persistedstate'
85import * as Cookies from 'js-cookie'
86
87const store = new Store({
88 // ...
89 plugins: [
90 createPersistedState({
91 storage: {
92 getItem: key => Cookies.get(key),
93 // Please see https://github.com/js-cookie/js-cookie#json, on how to handle JSON.
94 setItem: (key, value) => Cookies.set(key, value, { expires: 3, secure: true }),
95 removeItem: key => Cookies.remove(key)
96 }
97 })
98 ]
99})
100```
101
102In fact, any object following the Storage protocol (getItem, setItem, removeItem, etc) could be passed:
103
104```js
105createPersistedState({ storage: window.sessionStorage })
106```
107
108This is especially useful when you are using this plugin in combination with server-side rendering, where one could pass an instance of [dom-storage](https://www.npmjs.com/package/dom-storage).
109
110### ⚠️ LocalForage ⚠️
111
112As it maybe seems at first sight, it's not possible to pass a [LocalForage](https://github.com/localForage/localForage) instance as `storage` property. This is due the fact that all getters and setters must be synchronous and [LocalForage's methods](https://github.com/localForage/localForage#callbacks-vs-promises) are asynchronous.
113
114## License
115
116MIT © [Robin van der Vleuten](https://www.robinvdvleuten.nl)