We are using BackgroundSyncPlugin and from the logs it's evident that browser triggers the background sync event but I am not sure how the browser does this.
As per the documentation, https://developer.chrome.com/docs/workbox/modules/workbox-background-sync/, BackgroundSync API is invoked automatically by the browser, if it's compatible. But is there a way we can check the browser settings on how and when the sync event is triggered?
If browsers use exponential backoff then, is there a possibility that the requests are never synced to the server and browser stops replaying the request after certain limited tries?
Can we automate triggering of the sync event, via code eg: when network connectivity is detected, without having to restart the service worker?
Yes, the Background Sync API provides a sync
event that fires when the page (or worker) that registered the event with the SyncManager
is running and as soon as network connectivity is available.
To listen for this event, you can use the addEventListener()
method or set an event handler property on the service worker registration.
Ex:
self.addEventListener('sync', (event) => {
if (event.tag === 'sync-messages') {
event.waitUntil(doSomething());
}
});