Improve service worker initialization
This commit is contained in:
parent
414f8e5dca
commit
f675dc01ae
@ -71,50 +71,45 @@ export class Database {
|
||||
return objectList;
|
||||
}
|
||||
|
||||
public async registerServiceWorker(path: string) {
|
||||
private async registerServiceWorker(path: string): Promise<void> {
|
||||
if (!('serviceWorker' in navigator)) return;
|
||||
console.log('[Database] Initialisation du Service Worker sur :', path);
|
||||
console.log('[Database] Initializing Service Worker:', path);
|
||||
|
||||
try {
|
||||
// 1. NETTOYAGE DES ANCIENS WORKERS (ZOMBIES)
|
||||
const registrations = await navigator.serviceWorker.getRegistrations();
|
||||
|
||||
for (const registration of registrations) {
|
||||
const scriptURL = registration.active?.scriptURL || registration.installing?.scriptURL || registration.waiting?.scriptURL;
|
||||
const scope = registration.scope;
|
||||
|
||||
// On détecte spécifiquement l'ancien dossier qui pose problème
|
||||
// L'erreur mentionne : scope ('.../src/service-workers/')
|
||||
if (scope.includes('/src/service-workers/') || (scriptURL && scriptURL.includes('/src/service-workers/'))) {
|
||||
console.warn(`[Database] 🚨 ANCIEN Service Worker détecté (${scope}). Suppression immédiate...`);
|
||||
console.warn(`[Database] Removing old Service Worker (${scope})`);
|
||||
await registration.unregister();
|
||||
// On continue la boucle, ne pas retourner ici, il faut installer le nouveau après
|
||||
}
|
||||
}
|
||||
|
||||
// 2. INSTALLATION DU NOUVEAU WORKER (PROPRE)
|
||||
// On vérifie s'il est déjà installé à la BONNE adresse
|
||||
const existingValidWorker = registrations.find((r) => {
|
||||
const url = r.active?.scriptURL || r.installing?.scriptURL || r.waiting?.scriptURL;
|
||||
// On compare la fin de l'URL pour éviter les soucis http/https/localhost
|
||||
return url && url.endsWith(path.replace(/^\//, ''));
|
||||
return url && url.endsWith(path.replace(/^\//,''));
|
||||
});
|
||||
|
||||
if (!existingValidWorker) {
|
||||
console.log('[Database] Enregistrement du nouveau Service Worker...');
|
||||
console.log('[Database] Registering new Service Worker');
|
||||
this.serviceWorkerRegistration = await navigator.serviceWorker.register(path, { type: 'module', scope: '/' });
|
||||
} else {
|
||||
console.log('[Database] Service Worker déjà actif et valide.');
|
||||
console.log('[Database] Service Worker already active');
|
||||
this.serviceWorkerRegistration = existingValidWorker;
|
||||
await this.serviceWorkerRegistration.update();
|
||||
}
|
||||
// Set up listeners
|
||||
|
||||
navigator.serviceWorker.addEventListener('message', async (event) => {
|
||||
// console.log('Received message from service worker:', event.data);
|
||||
if (event.data.type === 'DB_REQUEST') {
|
||||
await this.handleDatabaseRequest(event.data);
|
||||
return;
|
||||
}
|
||||
await this.handleServiceWorkerMessage(event.data);
|
||||
});
|
||||
|
||||
// Periodic check
|
||||
if (this.serviceWorkerCheckIntervalId) clearInterval(this.serviceWorkerCheckIntervalId);
|
||||
this.serviceWorkerCheckIntervalId = window.setInterval(async () => {
|
||||
const activeWorker = this.serviceWorkerRegistration?.active || (await this.waitForServiceWorkerActivation(this.serviceWorkerRegistration!));
|
||||
@ -125,11 +120,10 @@ export class Database {
|
||||
}
|
||||
}, 5000);
|
||||
} catch (error) {
|
||||
console.error('[Database] 💥 Erreur critique Service Worker:', error);
|
||||
console.error('[Database] Service Worker error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to wait for service worker activation
|
||||
private async waitForServiceWorkerActivation(registration: ServiceWorkerRegistration): Promise<ServiceWorker | null> {
|
||||
return new Promise((resolve) => {
|
||||
if (registration.active) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user