diff --git a/apps/web/public/sw.js b/apps/web/public/sw.js index fec19f6..6ac65c8 100644 --- a/apps/web/public/sw.js +++ b/apps/web/public/sw.js @@ -1,5 +1,5 @@ // Offline support: the app shell and the last data seen load without network; actions need the network. -const CACHE = "snug-v3"; +const CACHE = "snug-v4"; const READ_API = /^\/api\/(setup|me|home|library|series\/\d+)$/; self.addEventListener("install", (event) => { @@ -26,6 +26,18 @@ const networkFirst = async (request) => { } }; +// A navigation is fetched as is, so an auth proxy's redirect to its sign-in reaches the browser, which follows it; +// only a failed network falls back to the cached app shell. The server answers index.html on every app path. +const navigation = async (request) => { + try { + const response = await fetch(request); + if (response.ok) (await caches.open(CACHE)).put("/", response.clone()); + return response; + } catch { + return (await caches.match("/")) ?? Response.error(); + } +}; + const cacheFirst = async (request) => { const cached = await caches.match(request); if (cached) return cached; @@ -45,7 +57,7 @@ self.addEventListener("fetch", (event) => { } if (READ_API.test(url.pathname)) return event.respondWith(networkFirst(request)); // A navigation to the API (the Export's download, a feed opened in a tab) reaches the server, not the app shell. - if (request.mode === "navigate" && !url.pathname.startsWith("/api/")) return event.respondWith(networkFirst(new Request("/"))); + if (request.mode === "navigate" && !url.pathname.startsWith("/api/")) return event.respondWith(navigation(request)); }); // Web Push: { title, body, url, tag } sent by the server. diff --git a/apps/web/src/api.ts b/apps/web/src/api.ts index 1f1c7dc..ef78d82 100644 --- a/apps/web/src/api.ts +++ b/apps/web/src/api.ts @@ -26,7 +26,13 @@ export async function api( headers: hasBody ? { "content-type": raw ? (init.contentType ?? "text/plain") : "application/json" } : undefined, body: raw ? init.text : hasBody ? JSON.stringify(init.body) : undefined, credentials: "same-origin", + // Snug's API never redirects: a redirect comes from an auth proxy in front of Snug (Cloudflare Access, Authelia…) asking to sign in again. + redirect: "manual", }); + if (res.type === "opaqueredirect") { + reloadForSignIn(); + throw new ApiError(0, "The sign-in in front of Snug has expired"); + } if (!res.ok) { const data = (await res.json().catch(() => null)) as { error?: string; fields?: Record } | null; throw new ApiError(res.status, data?.error ?? `Request failed (${res.status})`, data?.fields); @@ -34,6 +40,20 @@ export async function api( return (res.status === 204 || res.status === 201 ? undefined : await res.json()) as T; } +const SIGN_IN_RELOAD_KEY = "snug:sign-in-reload"; + +/** Reloads the page so the browser itself follows the auth proxy to its sign-in; at most once a minute, against a loop. */ +function reloadForSignIn() { + try { + const last = Number(sessionStorage.getItem(SIGN_IN_RELOAD_KEY) ?? 0); + if (Date.now() - last < 60_000) return; + sessionStorage.setItem(SIGN_IN_RELOAD_KEY, String(Date.now())); + } catch { + // No sessionStorage: reload all the same. + } + location.reload(); +} + export const queryClient = new QueryClient({ defaultOptions: { queries: { staleTime: 30_000, retry: (n, err) => !(err instanceof ApiError && err.status < 500) && n < 2 },