From f0ce0ccef7d6d04d6bd13fde9c47c6ea2e50659e Mon Sep 17 00:00:00 2001 From: wukko Date: Thu, 29 Aug 2024 18:10:46 +0600 Subject: [PATCH] web/server-info: refresh server info cache if endpoint changes --- .../components/save/SupportedServices.svelte | 2 +- web/src/lib/api/server-info.ts | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/web/src/components/save/SupportedServices.svelte b/web/src/components/save/SupportedServices.svelte index 6628c9f4..01d3a521 100644 --- a/web/src/components/save/SupportedServices.svelte +++ b/web/src/components/save/SupportedServices.svelte @@ -15,7 +15,7 @@ if ($cachedInfo) { loaded = true; - services = $cachedInfo.cobalt.services; + services = $cachedInfo.info.cobalt.services; } }; diff --git a/web/src/lib/api/server-info.ts b/web/src/lib/api/server-info.ts index 0c67f22d..51a3a90a 100644 --- a/web/src/lib/api/server-info.ts +++ b/web/src/lib/api/server-info.ts @@ -3,7 +3,12 @@ import { currentApiURL } from "$lib/api/api-url"; import type { CobaltServerInfoResponse, CobaltErrorResponse, CobaltServerInfo } from "$lib/types/api"; -export const cachedInfo = writable(); +export type CobaltServerInfoCache = { + info: CobaltServerInfo, + origin: string, +} + +export const cachedInfo = writable(); const request = async () => { const apiEndpoint = `${currentApiURL()}/`; @@ -29,7 +34,10 @@ const request = async () => { export const getServerInfo = async () => { const cache = get(cachedInfo); - if (cache) return true; + + if (cache && cache.origin === currentApiURL()) { + return true + } const freshInfo = await request(); @@ -38,7 +46,10 @@ export const getServerInfo = async () => { } if (!("status" in freshInfo)) { - cachedInfo.set(freshInfo); + cachedInfo.set({ + info: freshInfo, + origin: currentApiURL(), + }); return true; }