redux-persist-middleware
Generates Redux middleware that will trigger an asynchronous write to cache on a requestIdleCallback
.
- You supply a map of action types to reducers that should be persisted as a result of a given action.
- You supply the function to be called for persisting (must return a Promise).
That's it!
Works really well with the tiny cache library money-clip for versioned, async, IndexedDB backed caching for Redux apps.
Why?
- I think caching should to be a seamless asynchronous side effect in Redux, done when the browser is not busy with other things (hence the use of
requestIdleCallback
). - Lets you bring your own persistance library. I use money-clip because it's tiny, async, IndexedDB-powered (not sync and blocking like
localStorage
), and supports versioning and max age. - I want to dispatching special persistance related actions to trigger writing to cache because such actions are likely to trigger unnecessary renders. The work of persisting data has no direct impact on the UI and in my opinion, should be done lazily to keep app performing smoothly.
- I don't want to write on every action, I want to pick what reducers get persisted on what actions in an opt-in sort of way.
- inert if running in node.
install
npm install redux-persist-middleware
Example
// The relevant stuff // Here we use the money-clip library to// creates an object of cache functions with// these options pre-appliedconst cache = // A mapping of actions to reducers we should// persist after those actions occurconst actionMap = FETCH_USERS_SUCCESS: 'users' FETCH_TOKEN_SUCCESS: 'auth' // Configure our middlewareconst persistMiddleware = // Load everything from cache when the app// boots up.cache
Tests
$ npm i && npm test
Change log
1.0.1
: bugfix to make it work in a web worker1.0.0
: initial release
credits
If you like this follow @HenrikJoreteg on twitter.