From 8f6981c8a208e0b3a116ee3737368a8a99b7abc2 Mon Sep 17 00:00:00 2001 From: Elijah Date: Thu, 22 Oct 2026 11:19:30 -0700 Subject: [PATCH] Apply app updates automatically and survive proxy caching - Check for a new service worker on every return to the foreground and every 30 minutes; apply it in the background or just after launch, never with unsynced edits or an open sheet. The Update prompt remains. - Register the worker at extension-less /sw (no-cache), so proxies that cache *.js regardless of headers cannot hide updates - Return 404 for missing files instead of the HTML shell - Tests for the static routes; smoke test covers /sw; document the Nginx Proxy Manager "Cache Assets" pitfall Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- docs/UNRAID.md | 4 +- scripts/container-smoke.js | 1 + server/app.js | 17 +++++- src/App.tsx | 58 ++++++++------------ src/appUpdate.ts | 108 +++++++++++++++++++++++++++++++++++++ tests/api.test.js | 40 ++++++++++++++ 7 files changed, 190 insertions(+), 40 deletions(-) create mode 100644 src/appUpdate.ts diff --git a/README.md b/README.md index 2c0c75a..ee6ccef 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ A single-user, self-hosted nutrition journal. PWA first, with a responsive deskt - Settings save automatically as you change them: theme and accent switch instantly, and number fields commit after a short pause, on Return, or when you leave the field. - On phones, swipe left/right through Today → Library → Weight → Settings. Neighboring screens are already rendered and move together under your finger; settling uses a composited slide with no fade-to-blank delay. Every tab retains its own scroll position. Vertical scrolling, form controls, horizontal macro scrolling, and browser edge gestures remain independent. The calorie simulation pauses on other tabs. Pull down from a sheet header to dismiss it. System Reduce Motion disables these transitions. - Markdown, CSV and JSON export with date range, nutrient and weight selections. Markdown is designed for LLM analysis. Data is downloaded locally, never submitted to an LLM. Each download remembers the export format, range preset and selections for next time. -- Password authentication, IndexedDB offline journal/outbox, a precached app shell, automatic foreground/reconnect sync, and explicit PWA update prompts. +- Password authentication, IndexedDB offline journal/outbox, a precached app shell, automatic foreground/reconnect sync, and automatic app updates: each return to the foreground checks for a new version, which applies in the background or right after launch (never with unsynced edits or an open sheet), with an Update prompt to switch immediately. ### Phone layout diff --git a/docs/UNRAID.md b/docs/UNRAID.md index de87535..3ee3b7c 100644 --- a/docs/UNRAID.md +++ b/docs/UNRAID.md @@ -46,6 +46,8 @@ chmod 770 /mnt/user/appdata/calio These commands affect only Calio's directory. If you choose another path, use that path instead. Calio does not implement `PUID`/`PGID` environment variables: Docker's `--user` selects the identity. If migrating existing data, ensure its database and sidecar files are also writable by the chosen identity while Calio is stopped. The standalone image otherwise defaults to UID 1000. +If you use Nginx Proxy Manager, leave **Cache Assets** off for this host. It caches every `.js` file for hours regardless of the app's `no-cache` headers, which delays app updates. Calio already caches its own versioned assets on devices. (Current builds also load the service worker from the extension-less `/sw`, so such a cache cannot block updates, but a stale cache still wastes a day on older clients.) + Configure your HTTPS reverse proxy to forward **all paths** to `http://:3080`, or `http://calio:3000` on a shared Docker network. Leave `/api` intact. Open the public HTTPS address for sign-in and PWA installation. The upstream HTTP port alone does not provide the secure cookies or service worker origin needed by a phone PWA. On iPhone, use Safari → Share → **Add to Home Screen**. On Android, use Chrome → **Install app** / **Add to home screen**. Sign in online once and allow the initial sync to finish before testing offline use. @@ -63,7 +65,7 @@ docker cp calio:/data/backups/before-update.sqlite ./calio-backup.sqlite Use a new backup filename each time and copy it off the server. Do not copy only the live `calio.sqlite` file while Calio is writing: committed data can still be in its WAL sidecar. See the [README](../README.md#persistent-data-updates-and-backups) for restoration and device-cache considerations. -After a successful main build, select **Check for Updates → Update** on Unraid's Docker page. Unraid pulls `latest` and recreates the container with the saved port, path and variable settings. The PWA offers its own **Update** prompt after reconnecting; queued offline edits must sync before accepting it. +After a successful main build, select **Check for Updates → Update** on Unraid's Docker page. Unraid pulls `latest` and recreates the container with the saved port, path and variable settings. Devices then update themselves: the app checks for a new version each time it returns to the foreground, downloads it in the background, and switches over at a moment that cannot interrupt you (while it is in the background, or just after you open it). It waits while edits are unsynced or a sheet is open. An **Update** prompt is also shown for switching immediately. For a specific build, replace `:latest` in the repository field with its full commit SHA or published version tag. Database schema checks prevent an older app from opening a newer unsupported schema; a rollback across schema changes may need the matching database backup. diff --git a/scripts/container-smoke.js b/scripts/container-smoke.js index 2d0028e..d7fb8d8 100644 --- a/scripts/container-smoke.js +++ b/scripts/container-smoke.js @@ -129,6 +129,7 @@ try { for (const path of [ "/", "/sw.js", + "/sw", "/manifest.webmanifest", "/icons/apple-touch-icon.png", "/icons/apple-touch-icon-v2.png", diff --git a/server/app.js b/server/app.js index 58070dd..02d582b 100644 --- a/server/app.js +++ b/server/app.js @@ -148,6 +148,15 @@ export function createApp({ }); app.use("/api", (_, res) => res.status(404).json({ error: "Not found" })); if (existsSync(dist)) { + // The service worker is also served without a file extension. Reverse + // proxies that cache by extension (e.g. Nginx Proxy Manager's "Cache + // Assets") ignore no-cache on .js files, which hides app updates. + app.get("/sw", (_, res) => + res + .set("Cache-Control", "no-cache") + .type("text/javascript") + .sendFile(join(dist, "sw.js")), + ); app.use( express.static(dist, { maxAge: 0, @@ -159,7 +168,13 @@ export function createApp({ }, }), ); - app.get("/{*path}", (_, res) => res.sendFile(join(dist, "index.html"))); + // Only app routes fall back to the shell. A missing file (an old build's + // asset, say) must be a 404, never HTML that a cache could keep as script. + app.get("/{*path}", (req, res) => { + if (/\.[a-z0-9]+$/i.test(req.path)) + return res.status(404).type("text/plain").send("Not found"); + res.set("Cache-Control", "no-cache").sendFile(join(dist, "index.html")); + }); } app.use((error, _req, res, _next) => res diff --git a/src/App.tsx b/src/App.tsx index a292c0f..a0ed815 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -42,7 +42,6 @@ import { deleteRecord, initStore, login, - pendingChanges, sync, useJournal, useSyncStatus, @@ -83,6 +82,9 @@ import { useNavigation, type Page } from "./useNavigation"; import { PageActivity } from "./PageActivity"; import { useTiltResume } from "./useTilt"; import { useFollowToday } from "./useToday"; +import { applyUpdate, startAppUpdates, useUpdateWaiting } from "./appUpdate"; + +let updatesStarted = false; function PagePanel({ name, page, @@ -904,7 +906,7 @@ export function App() { const [deleting, setDeleting] = useState(null); const [reauth, setReauth] = useState(false); const [installEvent, setInstallEvent] = useState(null); - const [waiting, setWaiting] = useState(null); + const waiting = useUpdateWaiting(); const settings = (recordsOf(journal.records, "settings")[0] ?.data || defaultSettings) as SettingsData; const foods = useMemo( @@ -968,44 +970,26 @@ export function App() { setInstallEvent(event as InstallEvent); }; window.addEventListener("beforeinstallprompt", installListener); - if ("serviceWorker" in navigator && import.meta.env.PROD) { - navigator.serviceWorker - .register("/sw.js") - .then((registration) => { - if (registration.waiting) setWaiting(registration.waiting); - registration.addEventListener("updatefound", () => { - const worker = registration.installing; - worker?.addEventListener("statechange", () => { - if ( - worker.state === "installed" && - navigator.serviceWorker.controller - ) - setWaiting(worker); - }); - }); - }) - .catch(() => - notify( - "Offline app caching could not start. Check your browser storage settings.", - true, - ), - ); - } return () => window.removeEventListener("beforeinstallprompt", installListener); - }, [notify]); - async function update() { - await sync(); - if (pendingChanges()) { - notify("Sync your pending entries before updating.", true); - return; - } - navigator.serviceWorker.addEventListener( - "controllerchange", - () => location.reload(), - { once: true }, + }, []); + useEffect(() => { + // Start after the journal loads, so update timing knows about unsynced edits. + if (!ready || updatesStarted) return; + updatesStarted = true; + startAppUpdates(() => + notify( + "Offline app caching could not start. Check your browser storage settings.", + true, + ), ); - waiting?.postMessage("ACTIVATE"); + }, [ready, notify]); + async function update() { + try { + await applyUpdate(); + } catch (error) { + notify((error as Error).message, true); + } } if (fatal) return ( diff --git a/src/appUpdate.ts b/src/appUpdate.ts new file mode 100644 index 0000000..a247c3a --- /dev/null +++ b/src/appUpdate.ts @@ -0,0 +1,108 @@ +import { useSyncExternalStore } from "react"; +import { pendingChanges, sync } from "./store"; + +// The service worker serves the app shell from a device cache so Calio opens +// offline. A new build therefore arrives as a new worker that must take over +// and reload the page. This module finds new builds whenever the app returns to +// the foreground and applies them at moments that cannot interrupt anything: +// in the background, or right after launch/resume. The journal lives in +// IndexedDB, so a reload never loses entries; it waits while edits are +// unsynced or a sheet is open, and the Update prompt remains as a fallback. + +const APPLY_AFTER_LAUNCH_MS = 4000; +const CHECK_INTERVAL_MS = 30 * 60000; + +let registration: ServiceWorkerRegistration | null = null; +let waiting: ServiceWorker | null = null; +let resumedAt = performance.now(); +let reloadPending = false; +let reloading = false; +const listeners = new Set<() => void>(); + +const sheetOpen = () => Boolean(document.querySelector("dialog[open]")); +const quiet = () => !pendingChanges() && !sheetOpen(); + +function reload() { + if (reloading) return; + reloading = true; + location.reload(); +} + +function activate() { + waiting?.postMessage("ACTIVATE"); +} + +function maybeApply() { + if (!waiting || !quiet()) return; + if (document.hidden || performance.now() - resumedAt < APPLY_AFTER_LAUNCH_MS) + activate(); +} + +function setWaiting(worker: ServiceWorker) { + waiting = worker; + listeners.forEach((fn) => fn()); + maybeApply(); +} + +function check() { + registration?.update().catch(() => {}); +} + +export function startAppUpdates(onError: () => void) { + if (!("serviceWorker" in navigator) || !import.meta.env.PROD) return; + const container = navigator.serviceWorker; + const hadController = Boolean(container.controller); + container.addEventListener("controllerchange", () => { + // The very first install claims the page without changing the app. + if (!hadController) return; + if (document.hidden || !sheetOpen()) reload(); + else reloadPending = true; + }); + document.addEventListener("visibilitychange", () => { + if (document.hidden) { + if (reloadPending) reload(); + else maybeApply(); + return; + } + resumedAt = performance.now(); + check(); + }); + setInterval(() => { + if (!document.hidden) check(); + }, CHECK_INTERVAL_MS); + // Registered without a file extension: proxies that cache *.js regardless + // of Cache-Control would otherwise keep serving an old worker. + container + .register("/sw", { updateViaCache: "none" }) + .then((next) => { + registration = next; + if (next.waiting && container.controller) setWaiting(next.waiting); + next.addEventListener("updatefound", () => { + const worker = next.installing; + worker?.addEventListener("statechange", () => { + if (worker.state === "installed" && container.controller) + setWaiting(worker); + }); + }); + }) + .catch(onError); +} + +// Applies the waiting version now, from the Update prompt. +export async function applyUpdate() { + await sync(); + if (pendingChanges()) + throw new Error("Sync your pending entries before updating."); + activate(); +} + +export const useUpdateWaiting = () => + useSyncExternalStore( + (fn) => { + listeners.add(fn); + return () => { + listeners.delete(fn); + }; + }, + () => waiting !== null, + ); diff --git a/tests/api.test.js b/tests/api.test.js index d9a4730..9f70bfc 100644 --- a/tests/api.test.js +++ b/tests/api.test.js @@ -174,3 +174,43 @@ test("login is rate limited", async (t) => { 429, ); }); +test("static app: uncacheable worker route, shell fallback for routes, 404 for missing files", async (t) => { + const { mkdtempSync, mkdirSync, writeFileSync, rmSync } = await import("node:fs"); + const { tmpdir } = await import("node:os"); + const { join } = await import("node:path"); + const dist = mkdtempSync(join(tmpdir(), "calio-dist-")); + mkdirSync(join(dist, "assets")); + writeFileSync(join(dist, "index.html"), "Calio"); + writeFileSync(join(dist, "sw.js"), "const CACHE='calio-test';"); + writeFileSync(join(dist, "assets", "index-abc.js"), "console.log(1)"); + const store = openDatabase(":memory:"); + const app = createApp({ + store, + username: "test", + passwordHash: hashPassword(password), + origin, + secureCookies: false, + dist, + }); + const server = app.listen(0, "127.0.0.1"); + await new Promise((resolve) => server.once("listening", resolve)); + t.after(() => { + server.close(); + store.close(); + rmSync(dist, { recursive: true, force: true }); + }); + const base = `http://127.0.0.1:${server.address().port}`; + const worker = await fetch(base + "/sw"); + assert.equal(worker.status, 200); + assert.match(worker.headers.get("content-type"), /javascript/); + assert.equal(worker.headers.get("cache-control"), "no-cache"); + assert.equal(await worker.text(), "const CACHE='calio-test';"); + assert.equal((await fetch(base + "/sw.js")).status, 200); + assert.equal((await fetch(base + "/assets/index-abc.js")).status, 200); + const missing = await fetch(base + "/assets/index-old.js"); + assert.equal(missing.status, 404); + assert.doesNotMatch(missing.headers.get("content-type"), /html/); + const route = await fetch(base + "/weight"); + assert.equal(route.status, 200); + assert.match(await route.text(), /Calio/); +});