// Configuration PWA avec mise à jour automatique
class PWAUpdater {
constructor() {
this.registration = null;
this.updateAvailable = false;
this.init();
}
async init() {
if ('serviceWorker' in navigator) {
try {
// Enregistrer le service worker
this.registration = await navigator.serviceWorker.register('/sw.js');
console.log('Service Worker enregistré avec succès');
// Écouter les mises à jour
this.registration.addEventListener('updatefound', () => {
console.log('Nouvelle version détectée');
this.handleUpdateFound();
});
// Vérifier les mises à jour périodiquement
this.checkForUpdates();
setInterval(() => this.checkForUpdates(), 60000); // Vérifier toutes les minutes
} catch (error) {
console.error('Erreur lors de l\'enregistrement du Service Worker:', error);
}
}
}
handleUpdateFound() {
const newWorker = this.registration.installing;
newWorker.addEventListener('statechange', () => {
if (newWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// Une nouvelle version est disponible
this.updateAvailable = true;
this.showUpdateNotification();
}
}
});
}
async checkForUpdates() {
if (this.registration) {
try {
await this.registration.update();
} catch (error) {
console.error('Erreur lors de la vérification des mises à jour:', error);
}
}
}
showUpdateNotification() {
// Créer une notification de mise à jour
const updateBanner = document.createElement('div');
updateBanner.id = 'pwa-update-banner';
updateBanner.innerHTML = `
🚀 Une nouvelle version est disponible !
`;
// Supprimer l'ancienne notification si elle existe
const existingBanner = document.getElementById('pwa-update-banner');
if (existingBanner) {
existingBanner.remove();
}
document.body.appendChild(updateBanner);
// Auto-update après 10 secondes si l'utilisateur ne fait rien
setTimeout(() => {
if (document.getElementById('pwa-update-banner')) {
this.applyUpdate();
}
}, 10000);
}
applyUpdate() {
// Supprimer la notification
const banner = document.getElementById('pwa-update-banner');
if (banner) {
banner.remove();
}
// Appliquer la mise à jour
if (this.registration && this.registration.waiting) {
this.registration.waiting.postMessage({ type: 'SKIP_WAITING' });
}
// Recharger la page après un court délai
setTimeout(() => {
window.location.reload();
}, 500);
}
dismissUpdate() {
const banner = document.getElementById('pwa-update-banner');
if (banner) {
banner.remove();
}
}
// Forcer la vérification des mises à jour
async forceUpdate() {
await this.checkForUpdates();
}
}
// Initialiser le système de mise à jour PWA
window.pwaUpdater = new PWAUpdater();
// Écouter les messages du service worker
navigator.serviceWorker.addEventListener('controllerchange', () => {
console.log('Service Worker mis à jour, rechargement de la page...');
window.location.reload();
});
// Vérifier les mises à jour quand l'app devient visible
document.addEventListener('visibilitychange', () => {
if (!document.hidden && window.pwaUpdater) {
window.pwaUpdater.checkForUpdates();
}
});
console.log('PWA Updater initialisé');