feat: update ready reloade stuck

Task #55 completed by ClaudeQueue

ClaudeQueue
This commit is contained in:
Claude Worker
2026-07-01 02:38:45 +00:00
parent 50cf655b71
commit 395af38246
2 changed files with 157 additions and 10 deletions

View File

@@ -10,10 +10,14 @@
*
* Auto-update flow:
* 1. New SW installs alongside the old one.
* 2. activate: broadcast SW_UPDATE_AVAILABLE to all clients.
* 2. activate: if an older *versioned shell cache* is found (i.e. this
* activation is genuinely replacing a previous deploy, not just the
* first-ever install of a freshly (re)registered worker), broadcast
* SW_UPDATE_AVAILABLE to all clients.
* 3. Client shows "Update ready" banner.
* 4. User clicks → client sends { type: 'SKIP_WAITING' }.
* 5. SW calls skipWaiting() → takes over → client reloads.
* 4. User clicks "Refresh UI" → client (hardReloadUI in app.js) wipes every
* cache, unregisters the SW, and hard-navigates to fetch everything
* fresh from the network.
* ========================================================================== */
// BUILD_TAG is injected by the server at request time (GET /sw.js).
@@ -22,6 +26,15 @@
const VERSION = typeof __BUILD_TAG__ !== 'undefined' ? __BUILD_TAG__ : 'v1.0.3';
const CACHE = 'ytplayer-' + VERSION;
// Prefix shared by every versioned app-shell cache (ytplayer-<VERSION>).
// Utility caches (ytplayer-thumbs, ytplayer-fonts) intentionally do NOT
// match this — they aren't versioned and must survive every activate.
const SHELL_CACHE_PREFIX = 'ytplayer-';
const UTILITY_CACHES = new Set(['ytplayer-thumbs', 'ytplayer-fonts']);
function isVersionedShellCache(key) {
return key.startsWith(SHELL_CACHE_PREFIX) && !UTILITY_CACHES.has(key);
}
// Files that form the installable app shell.
const SHELL = [
'/',
@@ -48,16 +61,33 @@ self.addEventListener('install', (e) => {
// ---- Activate: evict old caches, claim clients, notify about update ----
self.addEventListener('activate', (e) => {
e.waitUntil((async () => {
// Delete every cache that isn't the current version
const keys = await caches.keys();
await Promise.all(
keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))
);
// Was there a *previous deploy's* app-shell cache lying around? If so,
// this activation is a genuine version bump — worth telling the client
// about. If the only versioned shell cache present is our own CACHE (or
// none at all), this is the first-ever activation of a freshly
// (re)registered worker — e.g. right after hardReloadUI() unregisters
// the old SW and hard-navigates — and there is nothing new to report.
// Without this check, that harmless re-install would re-broadcast
// SW_UPDATE_AVAILABLE and immediately reopen the "Update ready" banner
// the user just dismissed by clicking "Reload now".
const staleShellCaches = keys.filter((k) => isVersionedShellCache(k) && k !== CACHE);
const isGenuineUpdate = staleShellCaches.length > 0;
// Delete every stale *versioned shell* cache — never the utility caches
// (thumbs/fonts), which aren't tied to a deploy version and should
// survive every activate.
await Promise.all(staleShellCaches.map((k) => caches.delete(k)));
// Claim all open clients immediately (new installs)
await self.clients.claim();
// Broadcast to every open window so the app can show an update banner
const all = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
all.forEach((c) => c.postMessage({ type: 'SW_UPDATE_AVAILABLE', version: VERSION }));
if (isGenuineUpdate) {
// Broadcast to every open window so the app can show an update banner
const all = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
all.forEach((c) => c.postMessage({ type: 'SW_UPDATE_AVAILABLE', version: VERSION }));
}
})());
});