NostrBand cache fix

This commit is contained in:
Martti Malmi 2023-12-26 10:20:18 +02:00
parent 9c94e84b9d
commit 1f03a5ee5a

View File

@ -69,10 +69,10 @@ export default class NostrBandApi {
return await this.#json<TrendingHashtagsResponse>("GET", "/v0/trending/hashtags"); return await this.#json<TrendingHashtagsResponse>("GET", "/v0/trending/hashtags");
} }
async #json<T>(method: string, path: string, storageKey: string) { async #json<T>(method: string, path: string) {
throwIfOffline(); throwIfOffline();
// Try to get cached data first const storageKey = `nostr-band-${path}`;
const cachedData = localStorage.getItem(storageKey); const cachedData = localStorage.getItem(storageKey);
if (cachedData) { if (cachedData) {
const parsedData = JSON.parse(cachedData); const parsedData = JSON.parse(cachedData);
@ -80,12 +80,10 @@ export default class NostrBandApi {
const ageInMinutes = (new Date().getTime() - timestamp) / 1000 / 60; const ageInMinutes = (new Date().getTime() - timestamp) / 1000 / 60;
if (ageInMinutes < 15) { if (ageInMinutes < 15) {
// Use cached data if it's not older than 15 minutes
return data as T; return data as T;
} }
} }
// Fetch new data if no valid cache is found
try { try {
const res = await fetch(`${this.#url}${path}`, { method: method ?? "GET" }); const res = await fetch(`${this.#url}${path}`, { method: method ?? "GET" });
if (res.ok) { if (res.ok) {
@ -98,10 +96,8 @@ export default class NostrBandApi {
} }
} catch (error) { } catch (error) {
if (cachedData) { if (cachedData) {
// If an error occurs and there is cached data, return the cached data
return JSON.parse(cachedData).data as T; return JSON.parse(cachedData).data as T;
} else { } else {
// If no cache is available, rethrow the error
throw error; throw error;
} }
} }