UNPKG

@indiekit/indiekit

Version:

The little server that connects your website to the independent web

33 lines (27 loc) 808 B
/** * Get cached response value * @typedef {import("keyv").Keyv} Keyv * @param {Keyv} cache - Application cache (returns `false` if no database) * @param {number} ttl - Time to live * @param {string} url - URL to fetch and cache (used as key) * @returns {Promise<object>} Cached response value */ export const getCachedResponse = async (cache, ttl, url) => { let cachedResponse = cache && (await cache.get(url)); if (!cachedResponse) { try { const response = await fetch(url); if (!response.ok) { console.error(url, response.statusText); return; } cachedResponse = await response.json(); } catch (error) { console.error(error); } if (cache) { await cache.set(url, cachedResponse, ttl); } } return cachedResponse; };