mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-07 22:28:48 +00:00
25 lines
571 B
JavaScript
25 lines
571 B
JavaScript
import cache from "memory-cache";
|
|
|
|
const defaultDuration = 5;
|
|
|
|
export default async function cachedFetch(url, duration, ua) {
|
|
const cached = cache.get(url);
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
duration = duration || defaultDuration;
|
|
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
|
|
// wrapping text in JSON.parse to handle utf-8 issues
|
|
const options = {};
|
|
if (ua) {
|
|
options.headers = {
|
|
"User-Agent": ua,
|
|
};
|
|
}
|
|
const data = await fetch(url, options).then((res) => res.json());
|
|
cache.put(url, data, duration * 1000 * 60);
|
|
return data;
|
|
}
|