Store fetch responses in localStorage with expire timers! And fetch when idle and update cache in the background
MIT License
Store fetch JSON responses in localStorage with expire timers! And fetch only if the timer has expired.
npm i --save fetch-unless-cached
Use the inbuilt cached fetch which caches response for 600 minutes.
import cachedFetch from "fetch-unless-cached";
Or create a custom cached fetch function with your own duration
import {createfetchUnlessCached} from "fetch-unless-cached"
/**
* Create a custom fetch function which caches response for 300 minutes
*/
const cachedFetch = createfetchUnlessCached(300)
function fetchMyData(){
/*
* cachedFetch is just isomorphic-fetch but coupled with cache
* Do not perform .then(res => res.json()) as this happens internally
*
*/
...
return cachedFetch('myapi.com').then(response => console.log(response))
}
Note: If you would like to fetch during an idle state you can set the second argument to true.
const cachedFetch = createfetchUnlessCached(300, true);