app: the Church App's foundation; docs for Hub 2.26.1 and two years of devotionals
apps/app (not released; app@1.0.0 is built in slices, APP-1.0-PLAN §15): - backend (Express, Prisma, pg-boss, as the Website): the Hub client (CCP-, product app, the active-member band in the heartbeat), links and church.changed, sign-in through Accounts (everyone a Member; 8 days renewed up to 90; every role only with 2FA; the first Owner from HUB_ADMIN_EMAIL), Accounts' user.updated/export/deleted, active members once a day, modules on and off, GET /api/v1/app. Migration init. - frontend (Next.js 16): header, tab bar, footer, manifest and icons from the church profile, service worker, Add to home screen, Me, the admin's first dashboard. - env.template.json, PM2 template, README, CHANGELOG, WHATS-NEW; 27 tests. Docs: the Hub runs 2.26.1 with two years of Cross Code's devotionals; an App gets 2 weeks, today included. HANDOVER lists the App's slices. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* The Church App's service worker (APP-1.0-PLAN §2, P2). No push and no notification permission
|
||||
* (decision 13): it only keeps the App usable offline.
|
||||
*
|
||||
* - Next's built files and the icons: from the cache, refreshed in the background.
|
||||
* - Pages: from the network; a copy is kept for offline. Offline, the last copy of that page shows
|
||||
* (the page says "You're offline"), else the offline page.
|
||||
* - A page rendered for a signed-in member (the proxy marks it `x-cca-signed-in: 1`) is kept apart,
|
||||
* and that cache is emptied as soon as a page comes back signed out or the member signs out, so
|
||||
* nothing personal stays on the phone for someone signed out.
|
||||
* - The API, the admin and signing in are never cached.
|
||||
*/
|
||||
const VERSION = "cca-v1";
|
||||
const SHELL = `${VERSION}-shell`;
|
||||
const PAGES = `${VERSION}-pages`;
|
||||
const PERSONAL = `${VERSION}-personal`;
|
||||
const OFFLINE = "/offline";
|
||||
|
||||
self.addEventListener("install", (event) => {
|
||||
event.waitUntil(caches.open(SHELL).then((c) => c.addAll([OFFLINE, "/manifest.webmanifest"])).then(() => self.skipWaiting()));
|
||||
});
|
||||
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(
|
||||
caches.keys()
|
||||
.then((keys) => Promise.all(keys.filter((k) => !k.startsWith(`${VERSION}-`)).map((k) => caches.delete(k))))
|
||||
.then(() => self.clients.claim()),
|
||||
);
|
||||
});
|
||||
|
||||
const neverCached = (path) => path.startsWith("/api/") || path.startsWith("/admin") || path.startsWith("/auth/");
|
||||
|
||||
async function fromCacheThenRefresh(request) {
|
||||
const cache = await caches.open(SHELL);
|
||||
const hit = await cache.match(request);
|
||||
const refresh = fetch(request).then((res) => {
|
||||
if (res.ok) cache.put(request, res.clone());
|
||||
return res;
|
||||
});
|
||||
return hit ?? refresh;
|
||||
}
|
||||
|
||||
async function page(request) {
|
||||
const url = new URL(request.url);
|
||||
try {
|
||||
const res = await fetch(request);
|
||||
const signedIn = res.headers.get("x-cca-signed-in") === "1";
|
||||
if (!signedIn) await caches.delete(PERSONAL);
|
||||
if (res.ok && !neverCached(url.pathname)) {
|
||||
const cache = await caches.open(signedIn ? PERSONAL : PAGES);
|
||||
await cache.put(url.pathname, res.clone());
|
||||
}
|
||||
return res;
|
||||
} catch {
|
||||
const personal = await caches.open(PERSONAL).then((c) => c.match(url.pathname));
|
||||
const saved = personal ?? (await caches.open(PAGES).then((c) => c.match(url.pathname)));
|
||||
return saved ?? (await caches.open(SHELL).then((c) => c.match(OFFLINE))) ?? Response.error();
|
||||
}
|
||||
}
|
||||
|
||||
self.addEventListener("fetch", (event) => {
|
||||
const { request } = event;
|
||||
if (request.method !== "GET") {
|
||||
// Signing out: forget the member's pages at once.
|
||||
if (new URL(request.url).pathname === "/api/auth/signout") event.waitUntil(caches.delete(PERSONAL));
|
||||
return;
|
||||
}
|
||||
const url = new URL(request.url);
|
||||
if (url.origin !== self.location.origin) return;
|
||||
if (url.pathname.startsWith("/_next/static/") || url.pathname.startsWith("/app-icon/")) {
|
||||
event.respondWith(fromCacheThenRefresh(request));
|
||||
return;
|
||||
}
|
||||
if (request.mode === "navigate" && !neverCached(url.pathname)) event.respondWith(page(request));
|
||||
});
|
||||
Reference in New Issue
Block a user