Resume offline preparation on launch and show cache readiness

This commit is contained in:
Jonathan Sykes
2026-10-09 09:33:23 +08:00
parent 0251720fa2
commit 009a155568
6 changed files with 63 additions and 8 deletions

View File

@@ -250,14 +250,15 @@
// Opt-in: without an askStatus the download path runs exactly as before.
// (It must never be implicit — a probe that waits on a reply would hang
// forever if the caller injected a setTimeout that never fires.)
let precached = false;
let precached = false, offlineStatus;
if (waiting && askStatus) {
try {
const status = await askStatus(waiting);
precached = !!(status && status.ready);
offlineStatus = status;
} catch { precached = false; }
}
if (onPhase) onPhase(precached ? 'ready' : 'downloading');
if (onPhase) onPhase(precached ? 'ready' : 'downloading', offlineStatus);
if (!precached && refreshShell) await refreshShell();
if (!guard()) throw new Error('Playing - update after this song / when paused');
@@ -280,7 +281,25 @@
waiting.postMessage({ type: 'SKIP_WAITING' });
}
const SwUpdate = { applyUpdate, refreshShellInPlace, askCacheStatus, BUST_PARAM };
function showOfflineStatus(status) {
const el = root.document?.getElementById('offlineAssetStatus');
if (el && typeof status?.total === 'number') {
el.textContent = `${status.offlineReady ? 'Offline ready' : 'Preparing offline'} ${status.cached}/${status.total}`;
} else if (el && typeof status?.ready === 'boolean') {
el.textContent = status.ready ? 'Offline ready (legacy cache)' : 'Offline cache incomplete (legacy mode)';
}
}
async function offlineStatus() {
const registration = await root.navigator?.serviceWorker?.getRegistration();
const status = await askCacheStatus(registration?.active);
showOfflineStatus(status);
return status;
}
root.navigator?.serviceWorker?.addEventListener?.('message', event => {
if (event.data?.type === 'OFFLINE_STATUS') showOfflineStatus(event.data);
});
const SwUpdate = { offlineStatus, showOfflineStatus, applyUpdate, refreshShellInPlace, askCacheStatus, BUST_PARAM };
if (typeof module !== 'undefined' && module.exports) {
module.exports = SwUpdate;