Files
wizarr/app/static/js/pwa-registration.js
RicoUHD 123a967190 fix(pwa): improve Service Worker update strategy and cache management
- Set updateViaCache to 'none' in SW registration to bypass HTTP cache for the worker script.
- Implemented periodic update checks every hour and update listeners.
- Integrated immediate activation (self.skipWaiting) and adoption (self.clients.claim) in sw.js to resolve cache lock-in instantly.
- Optimized stale-while-revalidate strategy in sw.js to use event.waitUntil to guarantee background fetches complete.
- Removed duplicate css/main.css stylesheet load in base.html template.
- Added app/static/js/vendor/ and app/static/css/vendor/ to .dockerignore to keep Docker daemon build contexts clean.
2026-05-25 14:36:33 +02:00

62 lines
2.0 KiB
JavaScript

// PWA Service Worker Registration - Prevent double execution
(function() {
'use strict';
// Guard against double execution
if (window.wizarrPWALoaded) {
return;
}
window.wizarrPWALoaded = true;
// Service Worker Registration
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
// updateViaCache: 'none' ensures the browser always fetches sw.js from the
// server instead of the HTTP cache, so service worker updates are detected
// immediately after deployment.
navigator.serviceWorker.register('/static/sw.js', { updateViaCache: 'none' })
.then(function(registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
// Check for service worker updates every hour
setInterval(function() { registration.update(); }, 60 * 60 * 1000);
// Listen for a new service worker becoming available
registration.addEventListener('updatefound', function() {
var newWorker = registration.installing;
newWorker.addEventListener('statechange', function() {
if (newWorker.state === 'activated') {
console.log('New Wizarr version activated');
}
});
});
})
.catch(function(err) {
console.log('ServiceWorker registration failed: ', err);
});
});
}
// Handle install prompt
let deferredPrompt;
window.addEventListener('beforeinstallprompt', function(e) {
console.log('beforeinstallprompt event fired');
e.preventDefault();
deferredPrompt = e;
// Show install button/banner if needed
showInstallPromotion();
});
function showInstallPromotion() {
// You can create a custom install button here
// For now, we'll just log that the app is installable
console.log('PWA is installable');
}
// Handle successful installation
window.addEventListener('appinstalled', function(e) {
console.log('PWA was installed');
deferredPrompt = null;
});
})();