fix(pwa): stop unregistering the service worker on offline boot (#1346)

Opening the installed PWA offline showed Chrome's "no internet" page instead of
the cached app. On boot the axios response interceptor reacts to a failed
request with no response by probing /api/health; the probe collapsed "genuinely
offline" and "edge-proxy auth wall" into a single reachable=false, so the
interceptor unregistered the service worker and reloaded — straight into a dead
network. navigator.onLine is true on mobile while offline, so the existing guard
didn't help. This also defeated the offline data layer (withOfflineFallback,
authStore's offline branch), which runs later in the chain.

Fix: connectivity.probe() now returns a discriminated state
('online' | 'offline' | 'proxy-wall'). A fetch that throws, or navigator.onLine
false, is 'offline'; a cross-origin redirect (CF Access, via redirect:'manual'
→ opaqueredirect) or an HTML auth wall (Pangolin) is 'proxy-wall'. The
interceptor only tears down the SW on 'proxy-wall'; on plain offline it lets the
request reject so the cached shell + IndexedDB serve the app. CF Access /
Pangolin reauth still works — the proxy always presents a reachable redirect or
HTML wall, which the probe now detects positively.

Regression dates to v3.0.16 (#964), surfaced by the 3.1.0 rewrite.

Tests: 6 new connectivity cases (offline/online/proxy-wall discrimination);
client tsc clean, full client suite green (2850).
This commit is contained in:
Maurice
2026-06-28 12:42:27 +02:00
committed by Maurice
parent 172cff57a2
commit d1e024277f
3 changed files with 92 additions and 19 deletions
+25 -11
View File
@@ -1,6 +1,12 @@
const PROBE_INTERVAL_MS = 30_000
const PROBE_TIMEOUT_MS = 1_500
// Three distinct outcomes so callers can tell a genuine offline (the request
// never reached a server — never tear down offline infrastructure) apart from an
// edge-proxy auth wall (CF Access / Pangolin intercept /api — a top-level reload
// is needed so the proxy can run its auth flow).
export type ProbeState = 'online' | 'offline' | 'proxy-wall'
let reachable = true
const listeners = new Set<(v: boolean) => void>()
@@ -10,8 +16,8 @@ function setReachable(v: boolean): void {
listeners.forEach(fn => fn(v))
}
async function probe(): Promise<void> {
if (!navigator.onLine) { setReachable(false); return }
async function probe(): Promise<ProbeState> {
if (!navigator.onLine) { setReachable(false); return 'offline' }
try {
const ctrl = new AbortController()
const t = setTimeout(() => ctrl.abort(), PROBE_TIMEOUT_MS)
@@ -19,28 +25,36 @@ async function probe(): Promise<void> {
method: 'GET',
credentials: 'include',
cache: 'no-store',
// Surface a cross-origin auth redirect (CF Access) as an opaque redirect
// instead of letting it throw — that's a positive proxy signal, not offline.
redirect: 'manual',
signal: ctrl.signal,
})
clearTimeout(t)
// /api/health returns JSON. CF Access / Pangolin will either return HTML
// (Pangolin 200 auth wall) or trigger a cross-origin redirect that throws
// below. Both proxy-auth scenarios resolve to reachable = false.
if (res.type === 'opaqueredirect') { setReachable(false); return 'proxy-wall' }
const ct = res.headers.get('content-type') || ''
setReachable(res.ok && ct.includes('application/json'))
} catch {
if (res.ok && ct.includes('application/json')) { setReachable(true); return 'online' }
// A real HTTP response that isn't our health JSON (e.g. Pangolin's HTML 200
// auth wall, or an edge 401/403) means the proxy is reachable but gating us.
setReachable(false)
return 'proxy-wall'
} catch {
// fetch threw → the request never completed: genuinely offline (or the server
// is down). Must NOT be treated as a proxy wall — that would unregister the SW.
setReachable(false)
return 'offline'
}
}
export function startConnectivityProbe(): void {
probe()
setInterval(probe, PROBE_INTERVAL_MS)
window.addEventListener('online', probe)
void probe()
setInterval(() => { void probe() }, PROBE_INTERVAL_MS)
window.addEventListener('online', () => { void probe() })
window.addEventListener('offline', () => setReachable(false))
}
export function isReachable(): boolean { return reachable }
export function probeNow(): Promise<void> { return probe() }
export function probeNow(): Promise<ProbeState> { return probe() }
export function onChange(fn: (v: boolean) => void): () => void {
listeners.add(fn)
return () => listeners.delete(fn)