vuex-persist-indexeddb
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

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 to vuex.

  • 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 to undefined.

  • 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).

  • filter <Function>: A function that will be called to filter any mutations which will trigger setState on storage eventually. Defaults to () => true.

  • overwrite <Boolean>: When rehydrating, whether to overwrite the existing state with the output from getState directly, instead of merging the two objects with deepmerge. Defaults to false.

  • arrayMerger <Function>: A function for merging arrays when rehydrating state. Defaults to function (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 to store => {}

  • fetchBeforeUse <Boolean>: A boolean indicating if the state should be fetched from storage before the plugin is used. Defaults to false.

⚠️ 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):


Robin van der Vleuten

💻 📖 🚇 ⚠️

Sebastian

💻 📖

Boris Graeff

💻

Cícero Pablo

📖

Gurpreet Atwal

⚠️

Jakub Koralewski

💻

Jankees van Woezik

📖

Jofferson Ramirez Tiquez

📖

Jordan Deprez

📖

Juan Villegas

📖

Jürg Rast

💻

Kartashov Alexey

💻

Leonard Pauli

💻 📖

Nelson Liu

💻 📖 ⚠️

Nico

💻 ⚠️

Quentin Dreyer

💻

Raphael Saunier

💻

Rodney Rehm

💻 ⚠️

Ryan Wang

💻 📖 ⚠️

Sébastien Chopin

📖

jeffjing

💻

macarthuror

📖

Paul Melero

📖 💻 ⚠️

Guillaume da Silva

💻

Jonathan Santerre

💻

Fábio Santos

📖

robertgr991

💻

JurijsKolesnikovs

📖

David Bond

📖

Freek van Rijt

📖

Ilyes Hermellin

💻

Peter Siska

📖

Dmitry Filippov

📖

Thomas Meitz

📖 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

License

The MIT License (MIT). Please see License File for more information.

Readme

Keywords

Package Sidebar

Install

npm i vuex-persist-indexeddb

Weekly Downloads

344

Version

0.1.3

License

MIT

Unpacked Size

1.39 MB

Total Files

14

Last publish

Collaborators

  • sunhuaixing