Storage adapter to use IndexedDB via idb v3 with redux-persist ripped from idb v3 docs > Examples section
Using npm:
npm install --save @piotr-cz/redux-persist-idb-storage
or Yarn:
yarn add @piotr-cz/redux-persist-idb-storage
- Promise support/ polyfill
Import the storage and include it in persistConfig
when creating Redux store:
// configureStore.js
import createIdbStorage from '@piotr-cz/redux-persist-idb-storage'
const persistConfig = {
key: 'root',
storage: createIdbStorage({name: 'myApp', storeName: 'keyval'}),
serialize: false, // Data serialization is not required and disabling it allows you to inspect storage value in DevTools; Available since [email protected]
deserialize: false, // Required to bear same value as `serialize` since [email protected]
}
// ...
When using Server-Side Rendering (SSR), indexedDB won't be available in the environment.
In this case you may use feature detection with a fallback to use default redux-persist storage (which resolves to noop functions):
// configureStore.js
// Redux Persist storage
import defaultStorage from 'redux-persist/lib/storage'
// IndexedDB storage
import createIdbStorage from '@piotr-cz/redux-persist-idb-storage/src'
const persistConfig = {
key: 'root',
storage: globalThis.indexedDB ? createIdbStorage({name: 'myApp', storeName: 'keyval'}) : defaultStorage,
serialize: false,
}
See idb API
name
(optional): IndexedDB Database name. Defaults to'keyval-store'
.storeName
(optional): IndexedDB Store name. Defaults to'keyval'
.version
(optional): Schema version. Defaults to1
.upgradeCallback
(optional): Defaults toupgradeDb => upgradeDb.createObjectStore(options.storeName)
.