ServiceWorkerService manages Progressive Web App (PWA) updates through Angular’s service worker. It monitors version changes, notifies users of available updates, and tracks update check timestamps.
Location
src/app/services/service-worker/service-worker.service.ts
Properties
Read-only signal containing the ISO timestamp of the last update check. Persisted to localStorage.
Methods
checkForUpdate()
Manually checks for available updates. If an update is already downloaded and ready, prompts the user to reload immediately.Promise<boolean> - true if an update is available or ready
Update Flow
The service automatically handles the complete update lifecycle:Version Detection
When a new version is detected, shows an alert: “Downloading updates…”Analytics event:
version_detectedVersion Events
The service subscribes to Angular’sSwUpdate.versionUpdates and handles:
NO_NEW_VERSION_DETECTED
- Updates
lastUpdateCheckTimestamp - Logs analytics event:
no_new_version_detected
VERSION_DETECTED
- Updates
lastUpdateCheckTimestamp - Shows “Downloading updates…” alert
- Logs analytics event with version hash
VERSION_READY
- Sets internal
isUpdateReadyflag - Shows “Reload to update” alert with action button
- Logs analytics event with current and latest version hashes
VERSION_INSTALLATION_FAILED
- Logs error with LoggerService
- Shows error alert to user
- Logs analytics event:
version_installation_failed
Usage in Settings Page
The SettingsPageComponent allows users to manually check for updates:Configuration
Service worker configuration is defined inngsw-config.json:
Persistence
ThelastUpdateCheckTimestamp is automatically persisted to localStorage using an Angular effect:
Best Practices
Automatic Update Checks: The service worker automatically checks for updates on page load and periodically. Manual checks are optional.
Related
- PWA Features - Overview of PWA capabilities
- AlertService - Used to show update notifications
- AnalyticsService - Tracks update events