diff options
| author | Ariel Costas Guerrero <ariel@costas.dev> | 2025-09-07 17:29:53 +0200 |
|---|---|---|
| committer | Ariel Costas Guerrero <ariel@costas.dev> | 2025-09-07 17:29:53 +0200 |
| commit | 8182a08f60e88595984ba80b472f29ccf53c19bd (patch) | |
| tree | c377ad8a7d43b8794b5df0f7283f71ac24408210 /src/frontend/app/utils/serviceWorkerManager.ts | |
| parent | 577a6f00a0f006ca51276ae606835c2d892da872 (diff) | |
feat: Enhance development scripts and add Angular support
- Added new scripts for Angular development and formatting in package.json.
- Updated workspaces to include Angular frontend.
- Modified backend project file to exclude specific views from content inclusion.
- Updated logging settings in appsettings.json to include HttpClient warnings.
- Refactored TimetableTable component for cleaner rendering.
- Removed UpdateNotification component and related service worker management code.
- Simplified service worker registration in root component.
- Cleaned up settings page by removing update management functionality.
- Improved stoplist component structure for better readability.
- Updated PWA worker to streamline caching and response handling.
Diffstat (limited to 'src/frontend/app/utils/serviceWorkerManager.ts')
| -rw-r--r-- | src/frontend/app/utils/serviceWorkerManager.ts | 155 |
1 files changed, 0 insertions, 155 deletions
diff --git a/src/frontend/app/utils/serviceWorkerManager.ts b/src/frontend/app/utils/serviceWorkerManager.ts deleted file mode 100644 index a1ddbab..0000000 --- a/src/frontend/app/utils/serviceWorkerManager.ts +++ /dev/null @@ -1,155 +0,0 @@ -export class ServiceWorkerManager { - private registration: ServiceWorkerRegistration | null = null; - private updateAvailable = false; - private onUpdateCallback?: () => void; - - async initialize() { - if (!("serviceWorker" in navigator)) { - console.log("Service Workers not supported"); - return; - } - - try { - // First, unregister any old service workers to start fresh - const registrations = await navigator.serviceWorker.getRegistrations(); - for (const registration of registrations) { - if (registration.scope.includes(window.location.origin)) { - console.log("Unregistering old service worker:", registration.scope); - await registration.unregister(); - } - } - - // Register the new worker with a fresh name - this.registration = await navigator.serviceWorker.register("/pwa-worker.js", { - updateViaCache: 'none' // Disable caching for the SW file itself - }); - console.log("PWA Worker registered with scope:", this.registration.scope); - - // Implement proper updatefound detection (web.dev pattern) - await this.detectSWUpdate(); - - // Check for updates periodically - setInterval(() => { - this.checkForUpdates(); - }, 30 * 1000); - - // Check when page becomes visible - document.addEventListener("visibilitychange", () => { - if (!document.hidden) { - this.checkForUpdates(); - } - }); - - } catch (error) { - console.error("Service Worker registration failed:", error); - } - } - - private async detectSWUpdate() { - if (!this.registration) return; - - // Listen for new service worker discovery - this.registration.addEventListener("updatefound", () => { - const newSW = this.registration!.installing; - if (!newSW) return; - - console.log("New service worker found, monitoring installation..."); - - newSW.addEventListener("statechange", () => { - console.log("New SW state:", newSW.state); - - if (newSW.state === "installed") { - if (navigator.serviceWorker.controller) { - // New service worker is installed, but old one is still controlling - // This means an update is available - console.log("New service worker installed - update available!"); - this.updateAvailable = true; - this.onUpdateCallback?.(); - } else { - // First install, no controller yet - console.log("Service worker installed for the first time"); - } - } - - if (newSW.state === "activated") { - console.log("New service worker activated"); - // Optionally notify about successful update - } - }); - }); - - // Also listen for controller changes - navigator.serviceWorker.addEventListener("controllerchange", () => { - console.log("Service worker controller changed - reloading page"); - window.location.reload(); - }); - } - - async checkForUpdates() { - if (this.registration) { - try { - await this.registration.update(); - } catch (error) { - console.error("Failed to check for updates:", error); - } - } - } - - activateUpdate() { - if (this.registration && this.registration.waiting) { - this.registration.waiting.postMessage({ type: "SKIP_WAITING" }); - this.updateAvailable = false; - } - } - - onUpdate(callback: () => void) { - this.onUpdateCallback = callback; - } - - isUpdateAvailable() { - return this.updateAvailable; - } - - async clearCache(): Promise<void> { - try { - // Delete all caches - const cacheNames = await caches.keys(); - await Promise.all(cacheNames.map(name => caches.delete(name))); - console.log("All caches cleared"); - } catch (error) { - console.error("Failed to clear cache:", error); - throw error; - } - } - - // Nuclear option: completely reset the PWA - async resetPWA(): Promise<void> { - try { - console.log("Resetting PWA completely..."); - - // 1. Unregister ALL service workers - const registrations = await navigator.serviceWorker.getRegistrations(); - await Promise.all(registrations.map(reg => reg.unregister())); - - // 2. Clear all caches - await this.clearCache(); - - // 3. Clear local storage (optional) - localStorage.clear(); - sessionStorage.clear(); - - console.log("PWA reset complete - reloading..."); - - // 4. Force reload after a short delay - setTimeout(() => { - window.location.reload(); - }, 500); - - } catch (error) { - console.error("Failed to reset PWA:", error); - throw error; - } - } -} - -export const swManager = new ServiceWorkerManager(); |
