ServiceWorkerGlobalScope: sync event
Background Syncservice
Background Syncservice
The sync event of the ServiceWorkerGlobalScope interface is fired when the page (or worker) that registered the event with the SyncManager is running and as soon as network connectivity is available.
This event is not cancelable and does not bubble.
Syntax
Use the event name in addEventListener():
addEventListener("sync", (event) => { })
Alternatively, assign to the event handler property of the interface:
onsync = (event) => { }
Event type
A SyncEvent. Inherits from ExtendableEvent and Event.
SyncEvent
Examples
The following example shows how to respond to a sync event in the service worker.
self.addEventListener("sync", (event) => {
if (event.tag === "sync-messages") {
event.waitUntil(sendOutboxMessages());
}
});
You can also set up the event handler using the onsync property (but note that this replaces other event listeners added using onsync, if any):
self.onsync = (event) => {
// …
};