|
| 1 | +/** |
| 2 | + * Reactive connectivity store using Svelte 5 runes. |
| 3 | + * |
| 4 | + * `navigator.onLine` is only reliable when it reports `false` (definitely |
| 5 | + * offline); a `true` value merely means a network interface exists, not that |
| 6 | + * requests actually reach the server. It also fails to flip under DevTools' |
| 7 | + * Network "Offline" emulation once a service worker is controlling the page. |
| 8 | + * |
| 9 | + * So we probe once at startup to establish ground truth past that unreliable |
| 10 | + * initial value, then drive off the `online`/`offline` events (which fire |
| 11 | + * reliably on real transitions). The probe is a lightweight fetch to the |
| 12 | + * same-origin `/healthcheck` endpoint; the service worker serves that route |
| 13 | + * network-first, so it reflects real reachability (200 online, 503/throw |
| 14 | + * offline). |
| 15 | + */ |
| 16 | +import { browser } from "$app/environment"; |
| 17 | +import { base } from "$app/paths"; |
| 18 | + |
| 19 | +const PROBE_TIMEOUT_MS = 5_000; |
| 20 | + |
| 21 | +class IsOnlineStore { |
| 22 | + #online = $state<boolean>(browser ? navigator.onLine : true); |
| 23 | + #probing = false; |
| 24 | + |
| 25 | + constructor() { |
| 26 | + if (!browser) return; |
| 27 | + |
| 28 | + // `offline` is trustworthy on its own; react immediately. |
| 29 | + window.addEventListener("offline", () => { |
| 30 | + this.#online = false; |
| 31 | + }); |
| 32 | + // `online` only hints that connectivity *might* be back — verify it. |
| 33 | + window.addEventListener("online", () => void this.probe()); |
| 34 | + |
| 35 | + // Re-check when the tab regains focus, in case state changed while hidden. |
| 36 | + document.addEventListener("visibilitychange", () => { |
| 37 | + if (document.visibilityState === "visible") void this.probe(); |
| 38 | + }); |
| 39 | + |
| 40 | + // One probe at startup to correct navigator.onLine's unreliable initial |
| 41 | + // value; transitions after that come from the events above. |
| 42 | + void this.probe(); |
| 43 | + } |
| 44 | + |
| 45 | + /** Actively verify reachability by hitting the same-origin healthcheck. */ |
| 46 | + async probe(): Promise<void> { |
| 47 | + if (!browser || this.#probing) return; |
| 48 | + |
| 49 | + // `navigator.onLine === false` is a reliable offline signal — skip the |
| 50 | + // round-trip and avoid a doomed fetch. |
| 51 | + if (!navigator.onLine) { |
| 52 | + this.#online = false; |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + this.#probing = true; |
| 57 | + const controller = new AbortController(); |
| 58 | + const timeout = setTimeout(() => controller.abort(), PROBE_TIMEOUT_MS); |
| 59 | + try { |
| 60 | + const res = await fetch(`${base}/healthcheck`, { |
| 61 | + method: "GET", |
| 62 | + cache: "no-store", |
| 63 | + signal: controller.signal, |
| 64 | + }); |
| 65 | + this.#online = res.ok; |
| 66 | + } catch { |
| 67 | + this.#online = false; |
| 68 | + } finally { |
| 69 | + clearTimeout(timeout); |
| 70 | + this.#probing = false; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + get value(): boolean { |
| 75 | + return this.#online; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +let store: IsOnlineStore | undefined; |
| 80 | + |
| 81 | +export function createIsOnlineStore(): IsOnlineStore { |
| 82 | + if (!store) { |
| 83 | + store = new IsOnlineStore(); |
| 84 | + } |
| 85 | + return store; |
| 86 | +} |
| 87 | + |
| 88 | +export function useIsOnline(): IsOnlineStore { |
| 89 | + if (!store) { |
| 90 | + store = new IsOnlineStore(); |
| 91 | + } |
| 92 | + return store; |
| 93 | +} |
0 commit comments