feat: SW update banner — notify instead of force-reload
- SW activate now posts SW_UPDATED message to all clients instead of calling client.navigate() (which was disruptive and unreliable) - injectPWA() calls reg.update() on every app open to eagerly check for a new SW rather than waiting for the 24h browser interval - showUpdateBanner() renders a persistent accent-bordered toast with version tag, a "Neu laden" button and a dismiss X - Banner sits above player bar, repositions on mobile above bottom-nav Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
15959b45eb
commit
e462b54adf
3 files changed
+63
-6
No files matched your search
@@ -1329,9 +1329,38 @@ async function loadArtists(){
|
||||
|
||||
// ─── PWA ───
|
||||
function injectPWA(){
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.register('sw.js').catch(() => {});
|
||||
}
|
||||
if (!('serviceWorker' in navigator)) return;
|
||||
navigator.serviceWorker.register('sw.js').then(reg => {
|
||||
// Force-check for SW update every time the app opens
|
||||
reg.update().catch(() => {});
|
||||
// New SW found while page is open (e.g. user left tab open overnight)
|
||||
reg.addEventListener('updatefound', () => {
|
||||
const sw = reg.installing;
|
||||
if (!sw) return;
|
||||
sw.addEventListener('statechange', () => {
|
||||
// 'installed' + existing controller = update ready but waiting for skipWaiting
|
||||
// With skipWaiting() this fires briefly; the SW_UPDATED message handles the banner
|
||||
});
|
||||
});
|
||||
}).catch(() => {});
|
||||
|
||||
// SW posts SW_UPDATED after it activates and claims this client
|
||||
navigator.serviceWorker.addEventListener('message', e => {
|
||||
if (e.data?.type === 'SW_UPDATED') showUpdateBanner(e.data.version);
|
||||
});
|
||||
}
|
||||
|
||||
function showUpdateBanner(version) {
|
||||
if (document.getElementById('update-banner')) return;
|
||||
const b = document.createElement('div');
|
||||
b.id = 'update-banner';
|
||||
b.className = 'update-banner';
|
||||
b.innerHTML = `
|
||||
<span class="update-banner-text">✦ Update verfügbar${version ? ' <span style="opacity:.55;font-size:10px">('+version+')</span>' : ''}</span>
|
||||
<button class="update-banner-btn" onclick="window.location.reload()">Neu laden</button>
|
||||
<button class="update-banner-close" onclick="document.getElementById('update-banner').remove()">✕</button>
|
||||
`;
|
||||
document.body.appendChild(b);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user