vuex-persist-indexeddb
v0.1.3
Published
Persist and rehydrate your Vuex state between page reloads.
Downloads
1,011
Readme
vuex-persist-indexeddb
Persist and rehydrate your Vuex state between page reloads.
Install
npm install --save vuex-persist-indexeddb
Usage
vuex-persist-indexeddb 3.x (for Vuex 3 and Vue 2)
import Vuex from "vuex";
import createPersistedState from "vuex-persist-indexeddb";
const store = new Vuex.Store({
// ...
plugins: [createPersistedState()],
});
vuex-persist-indexeddb 4.x (for Vuex 4 and Vue 3)
import { createStore } from "vuex";
import createPersistedState from "vuex-persist-indexeddb";
const store = createStore({
// ...
plugins: [createPersistedState()],
});
Example with Vuex modules
New plugin instances can be created in separate files, but must be imported and added to plugins object in the main Vuex file.
/* module.js */
export const dataStore = {
state: {
data: ['js']
}
}
/* store.js */
import { dataStore } from './module'
const dataState = createPersistedState({
paths: ['dataStore.data']
})
export new Vuex.Store({
modules: {
dataStore
},
plugins: [dataState]
})
Example use reducer
/* module.js */
export const dataStore = {
state: {
user: {
name: 'jason',
sex: 'male',
phone: '132****1325'
},
books: ['js', 'java', 'python']
}
}
/* store.js */
import { dataStore } from './module'
const dataState = createPersistedState({
reducer(state, paths) {
return {
user: {
name: state.dataStore.user.name,
sex: state.dataStore.user.sex
}
};
}
})
export new Vuex.Store({
modules: {
dataStore
},
plugins: [dataState]
})
Example with Nuxt.js
It is possible to use vuex-persist-indexeddb with Nuxt.js. It must be included as a NuxtJS plugin:
With local storage (client-side only)
// nuxt.config.js
...
/*
* Naming your plugin 'xxx.client.js' will make it execute only on the client-side.
* https://nuxtjs.org/guide/plugins/#name-conventional-plugin
*/
plugins: [{ src: '~/plugins/persistedState.client.js' }]
...
// ~/plugins/persistedState.client.js
import createPersistedState from 'vuex-persist-indexeddb'
export default ({store}) => {
createPersistedState({
key: 'yourkey',
paths: [...]
...
})(store)
}
API
createPersistedState([options])
Creates a new instance of the plugin with the given options. The following options can be provided to configure the plugin for your specific needs:
key <String>
: The key to store the persisted state under. Defaults tovuex
.paths <Array>
: An array of any paths to partially persist the state. If no paths are given, the complete state is persisted. If an empty array is given, no state is persisted. Paths must be specified using dot notation. If using modules, include the module name. eg: "auth.user" Defaults toundefined
.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 tostore => handler => store.subscribe(handler)
.filter <Function>
: A function that will be called to filter any mutations which will triggersetState
on storage eventually. Defaults to() => true
.overwrite <Boolean>
: When rehydrating, whether to overwrite the existing state with the output fromgetState
directly, instead of merging the two objects withdeepmerge
. Defaults tofalse
.arrayMerger <Function>
: A function for merging arrays when rehydrating state. Defaults tofunction (store, saved) { return saved }
(saved state replaces supplied state).rehydrated <Function>
: A function that will be called when the rehydration is finished. Useful when you are using Nuxt.js, which the rehydration of the persisted state happens asynchronously. Defaults tostore => {}
fetchBeforeUse <Boolean>
: A boolean indicating if the state should be fetched from storage before the plugin is used. Defaults tofalse
.
⚠️ LocalForage ⚠️
As it maybe seems at first sight, it's not possible to pass a LocalForage instance as storage
property. This is due the fact that all getters and setters must be synchronous and LocalForage's methods are asynchronous.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
License
The MIT License (MIT). Please see License File for more information.