I have the following minor application:
(angular 13 - main thread)
home.component.ts
public sendIssueDescription(e) : void {
e.preventDefault();
if('serviceWorker' in navigator && 'SyncManager' in window) {
navigator.serviceWorker.ready
.then((sw:any) => {
let issue = this.getIssue();
this.dbService.saveIssue(issue)
.subscribe(
res => {
console.log("Saving a problem was Ok:" + JSON.stringify(res));
sw.sync.register('sync-new-problem');
},
err => console.log("Error ocurred:", err),
() => console.log("Syncronisation is totally completed")
);
});
}
else {
// A browser do not support a background syncronization, we need to send the data right now
let issue = this.getIssue();
this.problemReportingService.postIssue(issue).subscribe(
res => window.console.log("Post issue Ok"),
err => window.console.log("Post issue error!")
);
}
}
So, what's going on here:
- If a service worker is available, and I was able to read one, I'm saving data to the IndexedDb database.
- After that, I perform a call of
sw.sync.register('sync-new-problem'); to fire synchronization event.
The 'sync' event fires and everything works perfect.
I'm catching a 'sync' event in the service worker:
self.addEventListener("sync", function(event) {
console.log('[Service Worker] background synchronization', event);
if(self.indexedDB && event.tag === 'sync-new-problem') {
console.log('[Service Worker] syncing a new Problem');
event.waitUntil(
readAllData('Problems')
.then(function(data) {
for(var dt of data) {
console.log("Data: " + dt);
})
.then(function(res) {
console.log("It's required to clean storage data: " + res);
})
.catch(error => {
console.log("Error ocurred" + error);
throw error;
})
);
}
});
When I'm registering sync task, the sync event is fired immediately and everything works well. But this event should also be automatically thrown when internet connection is restored.
I tried to turn my Wi-Fi/Ethernet Off and On. I also tried to select Offline in Chrome Development Tools. Nothing helps, 'sync' event is not fired when the network is restored.
According to Background Sync tab in Chrome, my sync task was registered. Then I turned Wi-Fi Off/On, checked and unchecked Offline checkbox on the Application tab of Chrome Dev Tools. Nothing works, 'sync' event is not fired on connection restore.
I'd be very grateful for any suggestion, how can I get 'sync' event fired on network restore. I tried with Chrome v.96