Skip to content

Instantly share code, notes, and snippets.

@jurosh
Last active April 24, 2018 07:31
Show Gist options
  • Select an option

  • Save jurosh/30f85da26f4177bb0b7c220ea74444e6 to your computer and use it in GitHub Desktop.

Select an option

Save jurosh/30f85da26f4177bb0b7c220ea74444e6 to your computer and use it in GitHub Desktop.
Redux Middleware: Simple local storage single item cache
const cacheMiddleware = store => next => action => {
if(action.type !== 'GET_SOMETHING') {
return next(action);
}
const data = getFromLocalstorage()
if(!data) {
// Fetch and put to localstorate for later use
const data = fetchFromServer()
return next({ type: 'SERVER_RESULT', data })
}
return next({ type: 'CACHED_RESULT', data });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment