mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 23:33:29 +00:00
feat(pwa): register the service worker app-wide
Until now `/sw.js` registered only behind the Web-Push opt-in, so any user who never enabled notifications ran with no service worker — the offline shell cache, the precached app shell, and the network-first page cache were all inert. Add a production-gated `ServiceWorkerRegistrar` that installs the worker for everyone, deferred to the `load` event wrapped in `requestIdleCallback` so install/precache stays off the first-paint critical path. Mount it beside `VersionPoller` in the root providers. The registrar only installs. `VersionPoller` remains the sole owner of the update→reload path (poll `/api/version`, then unregister + cache-wipe + reload on a server-ahead deploy); no `updatefound`/`controllerchange` handling lives here so the two cannot double-fire. `updateViaCache: "none"` forces a network byte-check of `sw.js` on every update check so a cached worker can never mask a new deploy.
This commit is contained in:
1 parent
b59f8118a9
commit
b4c6c2cc88
3 files changed
+114
No files matched your search
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Gating predicate for app-wide service-worker registration.
|
||||
*
|
||||
* The registrar must install `/sw.js` only in production and only where the
|
||||
* browser supports the API. Dev lacks the generated `/sw-version.js` (the
|
||||
* `prebuild` step writes it) and a caching SW fights HMR, so a dev register
|
||||
* would be actively harmful.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { shouldRegisterServiceWorker } from "../service-worker-registrar";
|
||||
|
||||
describe("shouldRegisterServiceWorker", () => {
|
||||
it("registers in production with window + serviceWorker support", () => {
|
||||
expect(shouldRegisterServiceWorker("production", true, true)).toBe(true);
|
||||
});
|
||||
|
||||
it("does not register outside production", () => {
|
||||
expect(shouldRegisterServiceWorker("development", true, true)).toBe(false);
|
||||
expect(shouldRegisterServiceWorker("test", true, true)).toBe(false);
|
||||
expect(shouldRegisterServiceWorker(undefined, true, true)).toBe(false);
|
||||
});
|
||||
|
||||
it("does not register without a window (SSR)", () => {
|
||||
expect(shouldRegisterServiceWorker("production", false, true)).toBe(false);
|
||||
});
|
||||
|
||||
it("does not register when serviceWorker is unsupported", () => {
|
||||
expect(shouldRegisterServiceWorker("production", true, false)).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user