From 1f03a5ee5af002254a0956c1e93ccb38a568e273 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Tue, 26 Dec 2023 10:20:18 +0200 Subject: [PATCH] NostrBand cache fix --- packages/app/src/External/NostrBand.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/app/src/External/NostrBand.ts b/packages/app/src/External/NostrBand.ts index fe0d97e95..0f819126b 100644 --- a/packages/app/src/External/NostrBand.ts +++ b/packages/app/src/External/NostrBand.ts @@ -69,10 +69,10 @@ export default class NostrBandApi { return await this.#json("GET", "/v0/trending/hashtags"); } - async #json(method: string, path: string, storageKey: string) { + async #json(method: string, path: string) { throwIfOffline(); - // Try to get cached data first + const storageKey = `nostr-band-${path}`; const cachedData = localStorage.getItem(storageKey); if (cachedData) { const parsedData = JSON.parse(cachedData); @@ -80,12 +80,10 @@ export default class NostrBandApi { const ageInMinutes = (new Date().getTime() - timestamp) / 1000 / 60; if (ageInMinutes < 15) { - // Use cached data if it's not older than 15 minutes return data as T; } } - // Fetch new data if no valid cache is found try { const res = await fetch(`${this.#url}${path}`, { method: method ?? "GET" }); if (res.ok) { @@ -98,10 +96,8 @@ export default class NostrBandApi { } } catch (error) { if (cachedData) { - // If an error occurs and there is cached data, return the cached data return JSON.parse(cachedData).data as T; } else { - // If no cache is available, rethrow the error throw error; } }