Build the real /display Chromium signage client as a React route on the existing single-page build. The server remains authoritative; the display only consumes the verified Phase 3B snapshot and reports telemetry. The renderer owns two permanent scene layers that are never recreated. A transition prepares the hidden layer, decodes the incoming image on the element that will actually be composited, then raises that layer above the current one at opacity 0 and fades it in while the outgoing layer stays fully opaque underneath. This removes the v1 defect where two simultaneous fades let the black root through the composite. Every asynchronous load and transition carries a generation token, so work finishing after authoritative state moved on is discarded rather than shown. A true 16:9 slide fills the frame with no backdrop or shadow; anything else is contained, centred, and uncropped over a blurred, solid, or black background with a subtle shadow. Blur uses the global setting and is overscanned; boolean darkening applies to the background only. Static targets use the same renderer with the snapshot's global presentation defaults, and mode off is a topmost gate while the renderer keeps tracking underneath. Slide bytes live in an explicit Cache Storage cache with reference counted object URLs and conservative pruning, never in ordinary HTTP caching. A new manifest is staged before promotion, so one unfetchable asset marks only itself unavailable instead of rejecting a playlist. A small IndexedDB store with explicit version handling holds the persistent client ID, last known good manifest, timing anchor, and last structured error, and discards malformed records on read. A narrow service worker caches only the /display app shell, network-first for navigations, and explicitly bypasses media, API, and socket traffic. The display sends a real display:heartbeat every 12 seconds with its actual state, and reports structured errors as bounded JSON that the server's existing string validation already accepts; /api/displays parses them back into an object, so no migration was required. Verified against an isolated stack through Nginx with 20 Playwright Chromium tests plus 9 client unit tests, and 187/187 backend tests. Co-Authored-By: Claude Opus 5 <[email protected]>
96 lines
3.1 KiB
JavaScript
96 lines
3.1 KiB
JavaScript
/* Announcements signage app-shell worker.
|
|
*
|
|
* Scope is deliberately narrow: this worker makes the /display application
|
|
* shell available for a future offline start. Slide bytes are NOT its job —
|
|
* those are owned explicitly by the display asset manager in Cache Storage, so
|
|
* the two caches never compete for the same responsibility.
|
|
*/
|
|
const SHELL_CACHE = "announcements-display-shell-v1";
|
|
const SHELL_FALLBACK = "/display";
|
|
|
|
// Paths the worker must never handle. Media is the asset manager's; the API and
|
|
// socket are live server state and must never be answered from a cache.
|
|
const BYPASS_PREFIXES = ["/api/", "/socket.io/", "/media/", "/health"];
|
|
|
|
function isBypassed(url) {
|
|
return BYPASS_PREFIXES.some(
|
|
(prefix) => url.pathname === prefix.replace(/\/$/, "") || url.pathname.startsWith(prefix),
|
|
);
|
|
}
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(
|
|
(async () => {
|
|
const cache = await caches.open(SHELL_CACHE);
|
|
try {
|
|
await cache.add(new Request(SHELL_FALLBACK, { cache: "reload" }));
|
|
} catch {
|
|
// A first install while the network is unavailable still succeeds; the
|
|
// shell is captured on the next successful navigation instead.
|
|
}
|
|
// A kiosk must never be trapped on a stale application version.
|
|
await self.skipWaiting();
|
|
})(),
|
|
);
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
(async () => {
|
|
for (const name of await caches.keys()) {
|
|
// Only this worker's own shell generations are removed. The display
|
|
// asset cache is left strictly alone.
|
|
if (name.startsWith("announcements-display-shell-") && name !== SHELL_CACHE) {
|
|
await caches.delete(name);
|
|
}
|
|
}
|
|
await self.clients.claim();
|
|
})(),
|
|
);
|
|
});
|
|
|
|
async function networkFirst(request) {
|
|
const cache = await caches.open(SHELL_CACHE);
|
|
try {
|
|
const response = await fetch(request);
|
|
if (response && response.ok) {
|
|
// Keep the shell fallback fresh so an online kiosk always updates.
|
|
await cache.put(SHELL_FALLBACK, response.clone());
|
|
}
|
|
return response;
|
|
} catch (error) {
|
|
const cached = (await cache.match(SHELL_FALLBACK)) || (await cache.match("/index.html"));
|
|
if (cached) return cached;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function cacheFirst(request) {
|
|
const cache = await caches.open(SHELL_CACHE);
|
|
const cached = await cache.match(request);
|
|
if (cached) return cached;
|
|
const response = await fetch(request);
|
|
if (response && response.ok) await cache.put(request, response.clone());
|
|
return response;
|
|
}
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
const { request } = event;
|
|
if (request.method !== "GET") return;
|
|
const url = new URL(request.url);
|
|
if (url.origin !== self.location.origin) return;
|
|
if (isBypassed(url)) return;
|
|
|
|
if (request.mode === "navigate") {
|
|
// Online kiosks always get the newest application; offline ones get the
|
|
// last known good shell.
|
|
event.respondWith(networkFirst(request));
|
|
return;
|
|
}
|
|
|
|
// Vite emits content-hashed asset filenames, so these are safely immutable.
|
|
if (url.pathname.startsWith("/assets/")) {
|
|
event.respondWith(cacheFirst(request));
|
|
}
|
|
});
|