From 3b0eb40c08a5d809a330f79f5db47c4456017362 Mon Sep 17 00:00:00 2001 From: Maya Date: Sat, 25 Oct 2025 09:36:23 +0300 Subject: [PATCH] feat: vertd server fallbacks (#157) fixes #157 --- .../sections/settings/vertdSettings.svelte.ts | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/lib/sections/settings/vertdSettings.svelte.ts b/src/lib/sections/settings/vertdSettings.svelte.ts index d9cb5e4..6fb17b0 100644 --- a/src/lib/sections/settings/vertdSettings.svelte.ts +++ b/src/lib/sections/settings/vertdSettings.svelte.ts @@ -81,13 +81,35 @@ export class VertdInstance { } public async url() { + const reachable = async (url: string) => { + try { + const res = await fetch(url + "/api/version", { + method: "GET", + cache: "no-store", + }); + return res.ok; + } catch { + return false; + } + }; + switch (this.inner.type) { case "auto": { - if (!this.cachedIp) { - this.cachedIp = await ip(); + if (!this.cachedIp) this.cachedIp = await ip(); + const ipInfo = this.cachedIp; + const primary = this.geographicallyOptimalInstance(ipInfo); + + // try primary (closest) first + if (await reachable(primary)) return primary; + + // fall back to other locations + for (const location of LOCATIONS) { + if (location.url === primary) continue; + if (await reachable(location.url)) return location.url; } - return this.geographicallyOptimalInstance(this.cachedIp); + // if none are reachable, fall back to custom + return Settings.instance.settings.vertdURL; } case "eu": {