diff options
Diffstat (limited to 'ui/app/helpers/utils/fetch-with-cache.js')
-rw-r--r-- | ui/app/helpers/utils/fetch-with-cache.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/ui/app/helpers/utils/fetch-with-cache.js b/ui/app/helpers/utils/fetch-with-cache.js new file mode 100644 index 000000000..ac641c3c4 --- /dev/null +++ b/ui/app/helpers/utils/fetch-with-cache.js @@ -0,0 +1,28 @@ +import { + loadLocalStorageData, + saveLocalStorageData, +} from '../../../lib/local-storage-helpers' +import http from './fetch' + +const fetch = http({ + timeout: 30000, +}) + +export default function fetchWithCache (url, opts, cacheRefreshTime = 360000) { + const currentTime = Date.now() + const cachedFetch = loadLocalStorageData('cachedFetch') || {} + const { cachedUrl, cachedTime } = cachedFetch[url] || {} + if (cachedUrl && currentTime - cachedTime < cacheRefreshTime) { + return cachedFetch[url] + } else { + cachedFetch[url] = { cachedUrl: url, cachedTime: currentTime } + saveLocalStorageData(cachedFetch, 'cachedFetch') + return fetch(url, { + referrerPolicy: 'no-referrer-when-downgrade', + body: null, + method: 'GET', + mode: 'cors', + ...opts, + }) + } +} |