I am currently in the process of developing an Angular Progressive Web Application (PWA) with offline capabilities. While I have made significant progress, I am facing challenges regarding events for the service worker. Specifically, I am unsure about where to correctly implement the addEventListener(..)
. Most tutorials suggest using a separate service-worker.js file, but that approach feels awkward to me within a TypeScript environment. Moreover, I would like to leverage my Angular Services to interact with the backend, which seems unattainable from a standalone .js file.
In an attempt to address this issue, I created a Service to register a periodicSync and tried to listen to the event. Although the periodic sync is successfully registered in Chrome, the event is not being captured by the listener.
export class SyncService{
constructor(indexedDbService: IndexedDbService, apiService: ApiService)
{}
public RegisterSyncStuff(){
//Works fine
const registration : any = await navigator.serviceWorker.ready;
try {
await registration.periodicSync.register('download-stammdaten',{
minInterval: 30 * 1000,
});
} catch {
Console.log('no periodicSync possible')
}
//Does not work at all.
addEventListener('periodicSync', () => {
let stuffToStore = this.apiService.getStuff();
indexDbSerivce.StoreStuff(stuffToStore);
})
}
}