Background Synchronization API

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

The Background Synchronization API enables a web app to defer tasks so that they can be run in a service worker once the user has a stable network connection.

Concepts and usage

The Background Synchronization API allows web applications to defer server synchronization work to their service worker to handle at a later time, if the device is offline. Uses may include sending requests in the background if they couldn't be sent while the application was being used.

For example, an email client application could let its users compose and send messages at any time, even when the device has no network connection. The application frontend just registers a sync request and the service worker gets alerted when the network is present again and handles the sync.

The SyncManager interface is available through ServiceWorkerRegistration.sync. A unique tag identifier is set to 'name' the sync event, which can then be listened for within the ServiceWorker script. Once the event is received you can then run any functionality available, such as sending requests to the server.

As this API relies on service workers, functionality provided by this API is only available in a secure context.

Interfaces

SyncManager

Registers tasks to be run in a service worker at a later time with network connectivity. These tasks are referred to as background sync requests.

SyncEvent

Represents a synchronization event, sent to the global scope of a ServiceWorker. It provides a way to run tasks in the service worker once the device has network connectivity.

Extensions to other interfaces

The following additions to the Service Worker API provide an entry point for setting up background synchronization.

ServiceWorkerRegistration.sync Read only

Returns a reference to the SyncManager interface for registering tasks to run once the device has network connectivity.

sync event

An event handler fired whenever a sync event occurs. This happens as soon as the network becomes available.

Examples

The following examples show how to use the interface.

Requesting a background sync

The following asynchronous function registers a background sync from a browsing context:

js
async function syncMessagesLater() {
  const registration = await navigator.serviceWorker.ready;
  try {
    await registration.sync.register("sync-messages");
  } catch {
    console.log("Background Sync could not be registered!");
  }
}

Verifying a background sync by Tag

This code checks to see if a background sync task with a given tag is registered.

js
navigator.serviceWorker.ready.then((registration) => {
  registration.sync.getTags().then((tags) => {
    if (tags.includes("sync-messages")) {
      console.log("Messages sync already requested");
    }
  });
});

Listening for a background sync within a Service Worker

The following example shows how to respond to a background sync event in the service worker.

js
self.addEventListener("sync", (event) => {
  if (event.tag === "sync-messages") {
    event.waitUntil(sendOutboxMessages());
  }
});

Specifications

Specification
Web Background Synchronization

Browser compatibility

api.SyncManager

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
SyncManager
Chrome – Full support
Chrome 49 (Release date: 2016-03-02)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 36 (Release date: 2016-03-15)
footnote Full support
Safari – No support
Safari
footnote
footnote See bug 182565
Chrome Android – Full support
Chrome Android 49 (Release date: 2016-03-09)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 36 (Release date: 2016-03-31)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote
footnote See bug 182565
Samsung Internet – Full support
Samsung Internet 5 (Release date: 2016-12-15)
footnote Full support
WebView Android – No support
WebView Android
footnote
footnote See bug 40449796
WebView on iOS – No support
WebView on iOS
footnote
footnote See bug 182565
getTags
Chrome – Full support
Chrome 49 (Release date: 2016-03-02)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 36 (Release date: 2016-03-15)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 49 (Release date: 2016-03-09)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 36 (Release date: 2016-03-31)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 5 (Release date: 2016-12-15)
footnote Full support
WebView Android – No support
WebView Android
footnote
footnote See bug 40449796
WebView on iOS – No support
WebView on iOS
footnote No support
register
Chrome – Full support
Chrome 49 (Release date: 2016-03-02)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 36 (Release date: 2016-03-15)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 49 (Release date: 2016-03-09)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 36 (Release date: 2016-03-31)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 5 (Release date: 2016-12-15)
footnote Full support
WebView Android – No support
WebView Android
footnote
footnote See bug 40449796
WebView on iOS – No support
WebView on iOS
footnote No support
Available in workers
Chrome – Partial support
Chrome 49 – 60 (Release date: 2016-03-02)
footnote Partial support
footnote Only available in the Window and ServiceWorker global scopes.
Chrome – Full support
Chrome 61 (Release date: 2017-09-05)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – No support
Opera
footnote No support
Safari – No support
Safari
footnote No support
Chrome Android – Partial support
Chrome Android 49 – 60 (Release date: 2016-03-09)
footnote Partial support
footnote Only available in the Window and ServiceWorker global scopes.
Chrome Android – Full support
Chrome Android 61 (Release date: 2017-09-05)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Partial support
Samsung Internet 5 – 7.4 (Release date: 2016-12-15)
footnote Partial support
footnote Only available in the Window and ServiceWorker global scopes.
Samsung Internet – Full support
Samsung Internet 8 (Release date: 2018-07-18)
footnote Full support
WebView Android – No support
WebView Android
footnote
footnote See bug 40449796
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
See implementation notes.
Has more compatibility info.

api.ServiceWorkerGlobalScope.sync_event

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
sync event
Chrome – Full support
Chrome 49 (Release date: 2016-03-02)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 24 (Release date: 2014-09-02)
footnote Full support
Safari – No support
Safari
footnote
footnote See bug 182565
Chrome Android – Full support
Chrome Android 49 (Release date: 2016-03-09)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 24 (Release date: 2014-09-10)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote
footnote See bug 182565
Samsung Internet – Full support
Samsung Internet 5 (Release date: 2016-12-15)
footnote Full support
WebView Android – No support
WebView Android
footnote
footnote See bug 40449796
WebView on iOS – No support
WebView on iOS
footnote
footnote See bug 182565

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.

See also