diff --git a/src/components/top-matter/OnThisDay.vue b/src/components/top-matter/OnThisDay.vue index 58b177bc..389ee3b4 100644 --- a/src/components/top-matter/OnThisDay.vue +++ b/src/components/top-matter/OnThisDay.vue @@ -93,7 +93,23 @@ export default class OnThisDay extends Mixins(GlobalMixin) { } async refresh() { + // Look for cache + const dayIdToday = utils.dateToDayId(new Date()); + const cacheUrl = `/onthisday/${dayIdToday}`; + const cache = await utils.getCachedData(cacheUrl); + if (cache) this.process(cache); + + // Network request const photos = await dav.getOnThisDayRaw(); + utils.cacheData(cacheUrl, photos); + + // Check if exactly same as cache + if (cache?.length === photos.length && + cache.every((p, i) => p.fileid === photos[i].fileid)) return; + this.process(photos); + } + + async process(photos: IPhoto[]) { let currentYear = 9999; for (const photo of photos) { diff --git a/src/services/Utils.ts b/src/services/Utils.ts index c05fd0fd..6187c072 100644 --- a/src/services/Utils.ts +++ b/src/services/Utils.ts @@ -214,10 +214,10 @@ export async function openCache() { } /** Get data from the cache */ -export async function getCachedData(url: string) { +export async function getCachedData(url: string): Promise { const cache = staticCache || await openCache(); const cachedResponse = await cache.match(url); - if (!cachedResponse || !cachedResponse.ok) return false; + if (!cachedResponse || !cachedResponse.ok) return undefined; return await cachedResponse.json(); }