mirror of
https://github.com/wukko/cobalt.git
synced 2025-01-23 19:26:26 +01:00
web/server-info: refresh server info cache if endpoint changes
This commit is contained in:
parent
f7da62e817
commit
f0ce0ccef7
2 changed files with 15 additions and 4 deletions
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
if ($cachedInfo) {
|
if ($cachedInfo) {
|
||||||
loaded = true;
|
loaded = true;
|
||||||
services = $cachedInfo.cobalt.services;
|
services = $cachedInfo.info.cobalt.services;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -3,7 +3,12 @@ import { currentApiURL } from "$lib/api/api-url";
|
||||||
|
|
||||||
import type { CobaltServerInfoResponse, CobaltErrorResponse, CobaltServerInfo } from "$lib/types/api";
|
import type { CobaltServerInfoResponse, CobaltErrorResponse, CobaltServerInfo } from "$lib/types/api";
|
||||||
|
|
||||||
export const cachedInfo = writable<CobaltServerInfo | undefined>();
|
export type CobaltServerInfoCache = {
|
||||||
|
info: CobaltServerInfo,
|
||||||
|
origin: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const cachedInfo = writable<CobaltServerInfoCache | undefined>();
|
||||||
|
|
||||||
const request = async () => {
|
const request = async () => {
|
||||||
const apiEndpoint = `${currentApiURL()}/`;
|
const apiEndpoint = `${currentApiURL()}/`;
|
||||||
|
@ -29,7 +34,10 @@ const request = async () => {
|
||||||
|
|
||||||
export const getServerInfo = async () => {
|
export const getServerInfo = async () => {
|
||||||
const cache = get(cachedInfo);
|
const cache = get(cachedInfo);
|
||||||
if (cache) return true;
|
|
||||||
|
if (cache && cache.origin === currentApiURL()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
const freshInfo = await request();
|
const freshInfo = await request();
|
||||||
|
|
||||||
|
@ -38,7 +46,10 @@ export const getServerInfo = async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!("status" in freshInfo)) {
|
if (!("status" in freshInfo)) {
|
||||||
cachedInfo.set(freshInfo);
|
cachedInfo.set({
|
||||||
|
info: freshInfo,
|
||||||
|
origin: currentApiURL(),
|
||||||
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue