Background Sync Is A PWA Super Power
Here’s how you can defer network calls when your web app is offline and execute them when the network is back
Oct 3, 2023
The Background Sync API enables web apps to defer tasks when the user is offline and run these when the network is restored. For example, a messaging app could save messages that are sent while the user is offline to IndexedDB and then send them when the user is online again. This is a very powerful API that contributes to a great offline experience and makes a web app feel more like a native app.
There are a lot of confusing articles on the Background Sync API that, at least for me, don’t really explain well how to use it. The API seems a bit counter-intuitive at first, but once you understand how it works, it opens up a lot of interesting possibilities.
Let’s have a look.
To make use of background sync you need to request a sync request with the SyncManager which is available through ServiceWorkerRegistration.sync so first you need to register a Service Worker and then request a sync with a unique tag:
await navigator.serviceWorker.register('/service-worker.js');
const registration = await navigator.serviceWorker.ready;
await registration.sync.register('sync-messages');
Here we have requested a sync with the tag “sync-messages”. Inside the Service Worker, we now register an event handler for the sync event and check the tag to identify the correct sync event:
self.addEventListener('sync', (event) => {
if (event.tag === 'sync-messages') {
event.waitUntil(syncMessages());
}
});
A tag is always added to a sync request so you can have multiple sync events that you can attach different functionality to.
Now I might not be that smart, but at first, I understood that this sync event would only fire when I went offline and then came back online again but to my surprise this sync event would always fire immediately when I registered it. Then when I went offline and back online again it would never fire.
What was going on?
Well, actually this is a very clever approach that enables you to define functionality that will be deferred when the app is offline until it’s back online again without you having to check the network status.
The browser will handle all that for you.
Let’s say that you have a button that will send a notification but when the app is offline you want that notification to be saved and then delivered when the app is online again.
Without the Background Sync API, you would:
- check the network status
- if there is no network, save the notification somewhere
- wait for the network to be restored
- check for any notifications that were sent while offline
- send the notifications
With the Background Sync API, you simply register a sync request with the tag “send-notification” (or whatever makes sense to you) and add an event handler for the sync event to your Service worker that sends the notification when the tag of the sync event is "send-notification" (or whatever you chose).
When the app is online, the sync event will immediately fire and the notification will be sent. But when the app is offline, this event won't be fired until the app is back online and then it will fire and send the notification:
// inside the service worker an event handler is registered
// for the 'sync' event that sends the notification
self.addEventListener('sync', e => {
e.waitUntil(
self.registration.showNotification(title, options)
.catch(err => console.log(err))
)
});
// in the browser:
const syncButton = document.querySelector('#sync-button');
const registration = await navigator.serviceWorker.getRegistration();
if('sync' in registration) {
syncButton.addEventListener('click', async () => {
await registration.sync.register('sync-demo');
});
}
The button doesn’t actually send the notification but simply requests a sync. It’s important to understand that when a sync is requested, it only fires a single sync event, so the button will request a sync each time it's clicked.
It may be a bit counter-intuitive that the button doesn’t send the notification and only registers a sync, but the beauty of this approach is that it decouples checking the network from the code to be run: the browser handles all this for you.
It fires the sync event immediately when online (so the code in the event handler is run) and defers it when offline until the app is online again.
Another great thing is that the sync event will also be fired when the network is restored and the PWA is not running. Really cool!
I added a demo to What PWA Can Do Today (currently only supported in Chrome).
In my next article I will explain how you can implement outbox functionality with Background Sync where a series of tasks or messages are saved to IndexedDB while offline and then run/sent when the network is back.