Background Sync with Service Workers

Service employees have been having a second. In March 2018, iOS Safari started together with service employees — so all main browsers at this level assist offline choices. And that is extra vital than ever — 20% of adults within the United States are with out Internet at dwelling, leaving these people relying solely on a cellphone to entry most data. This can embody one thing so simple as checking a financial institution steadiness or one thing as tedious as trying to find a job, and even researching sicknesses.

Offline-supported purposes are a necessity, and together with a service employee is a superb begin. However, service employees alone will solely get somebody a part of the way in which to a really seamless online-to-offline expertise. Caching belongings is nice, however with out an web connection you continue to cannot entry new information or ship any requests.

The Request Lifecycle

Currently a request may appear to be this:

A person pushes a button and a request is fired off to a server someplace. If there’s web, all the pieces ought to go off with no hitch. If there’s not web … effectively issues aren’t so easy. The request will not be despatched, and maybe the person realizes their request by no means made it by, or maybe they’re unaware. Fortunately, there’s a greater manner.

Enter: background sync.

Background Sync

The Background Sync Lifecycle

The lifecycle with background sync is barely totally different. First a person makes a request, however as an alternative of the request being tried instantly, the service employee steps in. The service employee will test if the person has web entry — in the event that they do, nice. The request will likely be despatched. If not, the service employee will wait till the person does have web and at that time ship the request, after it fetches information out of IndexedDB. Best of all, background sync will go forward and ship the request even when the person has navigated away from the unique web page.

Background Sync Support

While background sync is totally supported solely in Chrome, Firefox and Edge are presently engaged on implementing it. Fortunately with the usage of characteristic detection and onLine and offLine occasions, we are able to safely use background sync in any utility whereas additionally together with a fallback.

A simple newsletter signup app

(If you’d prefer to comply with together with the demo, the code might be discovered right here and the demo itself is discovered right here.)

Let’s assume now we have a quite simple e-newsletter signup type. We need the person to have the ability to signup for our e-newsletter whether or not or not they presently have web entry. Let’s begin with implementing background sync.

(This tutorial assumes you might be accustomed to service employees. If you aren’t, it is a good place to begin. If you are unfamiliar with IndexedDB, I like to recommend beginning right here.)

When you might be first organising a service employee, you will need to register it out of your utility’s JavaScript file. That may appear to be this:


Notice that we’re utilizing characteristic detection even when registering the service employee. There’s virtually no draw back to utilizing characteristic detection and it will cease errors from cropping up in older browsers like Internet Explorer 11 when the service employee is not accessible. Overall, it is a good behavior to maintain up even when it is not at all times crucial.

When we arrange background sync, our register operate adjustments and should look one thing like this:

return navigator.serviceWorker.prepared
.then(operate(registration) )
.catch( /…/ )

This is much more code, however we’ll break it down one line at a time.

First we’re registering the service employee like earlier than, however now we’re making the most of the truth that the register operate returns a promise. The subsequent piece you see is navigator.serviceWorker.prepared. This is a read-only property of a service employee that primarily simply lets you already know if the service employee is prepared or not. This property gives a manner for us to delay execution of the next capabilities till the service employee is definitely prepared.

Next now we have a reference to the service employee’s registration. We’ll put an occasion listener on our submit button, and at that time register a sync occasion and move in a string. That string will likely be used over on the service employee aspect afterward.

Let’s re-write this actual fast to incorporate characteristic detection, since we all know background sync does not but have large assist.

if(navigator.serviceWorker) {
return navigator.serviceWorker.prepared
doc.getElementById(‘submitForm’).addEventListener(‘click on’, (occasion) => )

Now let’s check out the service employee aspect.

self.onsync = operate(occasion)
if(occasion.tag == ‘example-sync’)

We connect a operate to onsync, the occasion listener for background sync. We wish to look ahead to the string we handed into the register operate again within the utility’s JavaScript. We’re awaiting that string utilizing occasion.tag.

We’re additionally utilizing occasion.waitUntil. Because a service employee is not frequently operating — it “wakes up” to do a process after which “goes back to sleep” — we wish to use occasion.waitUntil to maintain the service employee lively. This operate accepts a operate parameter. The operate we move in will return a promise, and occasion.waitUntil will hold the service employee “awake” till that operate resolves. If we did not use occasion.waitUntil the request may by no means make it to the server as a result of the service employee would run the onsync operate after which instantly return to sleep.

Looking on the code above, you will discover we do not have to do something to test on the standing of the person’s web connection or ship the request once more if the primary try fails. Background sync is dealing with all of that for us. Let’s check out how we entry the info within the service employee.

Because a service employee is remoted in its personal employee, we can’t be capable of entry any information immediately from the DOM. We’ll depend on IndexedDB to get the info after which ship the info onward to the server.

IndexedDB makes use of callbacks whereas a service employee is promise-based, so we’ll need to account for that in our operate. (There are wrappers round IndexedDB that make this course of slightly less complicated. I like to recommend trying out IDB or money-clip.)

Here is what our operate may appear to be:

return new Promise(operate(resolve, reject)
var db =‘newsletterSignup’);
db.onsuccess = operate(occasion)
this.end result.transaction(“newsletterObjStore”).objectStore(“newsletterObjStore”).getAll().onsuccess = operate(occasion)
resolve(occasion.goal.end result);

db.onerror = operate(err)


Walking by it, we’re returning a promise, and we’ll use the resolve and reject parameters to make this operate extra promise-based to maintain all the pieces in keeping with the service employee.

We’ll open a database and we’ll use the getAll technique to drag all the info from the required object retailer. Once that’s success, we’ll resolve the operate with the info. If now we have an error, we’ll reject. This retains our error dealing with working the identical manner as all the different guarantees and makes certain now we have the info earlier than we ship it off to the server.

After we get the info, we simply make a fetch request the way in which we usually would.

technique: ‘POST’,
physique: JSON.stringify(response),

Of course all of this can solely run if the person has Internet entry. If the person doesn’t have Internet entry, the service employee will wait till the connection has returned. If, as soon as the connection returns, the fetch request fails, the service employee will try a most of 3 times earlier than it stops making an attempt to ship the request for good.

Now that we have arrange background sync, we’re able to arrange our fallback for browsers that do not assist background sync.

Support for legacy browsers

Unfortunately, service employees aren’t supported in legacy browsers and the background sync characteristic is barely supported in Chrome as of now. In this publish we’ll concentrate on using different offline options with the intention to mimic background sync and provide the same expertise.

Online and Offline Events

We’ll begin with on-line and offline occasions. Our code to register the service work final time appeared like this:

if(navigator.serviceWorker) {
return navigator.serviceWorker.prepared
.then(operate(registration) {
doc.getElementById(‘submitForm’).addEventListener(‘click on’, (occasion) => )

Let’s do a fast recap of this code. After we register the service employee, we use the promise returned from navigator.serviceWorker.prepared to make sure that the service employee is in reality able to go. Once the service employee is able to go, we’ll connect an occasion listener to the submit button and instantly save the info into IndexedDB. Lucky for us IndexedDB is supported in successfully all browsers, so we are able to fairly effectively depend on it.

After we have saved the info, we use characteristic detection to verify we are able to use background sync. Let’s go forward and add our fallback plan within the else.

.catch(operate(err) )

Additional assist

We’re utilizing navigator.onLine to test the person’s web connection. If they’ve a connection, this can return true. If they’ve an web connection, we’ll go forward and ship off the info. Otherwise, we’ll pop up an alert letting the person know that their information hasn’t been despatched.

Let’s add a few occasions to look at the web connection. First we’ll add an occasion to look at the connection going offline.

window.addEventListener(‘offline’, operate()
alert(‘You have misplaced web entry!’);

If the person loses their web connection, they’re going to see an alert. Next we’ll add an occasion listener for awaiting the person to return again on-line.

window.addEventListener(‘on-line’, operate()
if(!navigator.serviceWorker && !window.SyncSupervisor)
if(response.size > zero)


Once the person’s web connection returns, we’ll do a fast test if a service employee is accessible and in addition sync being accessible. We wish to test on this as a result of if the browser has sync accessible, we needn’t depend on our fallback as a result of it might end in two fetches. However, if we do use our fallback, we first pull the info out of IndexedDB like so:

var myDB =‘newsletterSignup’);

myDB.onsuccess = operate(occasion)
this.end result.transaction(“newsletterObjStore”).objectStore(“newsletterObjStore”).getAll().onsuccess = operate(occasion) ;

myDB.onerror = operate(err)

Next we’ll confirm that the response from IndexedDB really has information, and if it does we’ll ship it to our server.

This fallback will not solely change background sync for a number of causes. Firstly, we’re checking for on-line and offline occasions, which we don’t have to do with background sync as a result of background sync handles all that for us. Additionally, background sync will proceed making an attempt to ship requests even when the person has navigated away from the web page.

Our answer will not be capable of ship the request even when the person navigates away, however we are able to pre-emptively test IndexedDB as quickly because the web page masses and ship any cached information instantly. This answer additionally watches for any community connection adjustments, and sends cached information as quickly because the connection returns.

Next steps in offline assist

Edge and Firefox browsers are presently engaged on implementing background sync, which is incredible. It is likely one of the finest options for offering a extra empathetic expertise for customers transferring between web connection and connection loss. Fortunately with slightly assist from on-line and offline occasions and IndexedDB, we are able to begin offering a greater expertise for customers at present.

If you’d prefer to be taught extra about offline strategies, take a look at my weblog: or comply with me on Twitter.

Carmen Bourlon

About Carmen Bourlon

Carmen is a software program developer dwelling and dealing in Oklahoma City, with a particular curiosity in offline know-how. She has spoken at a number of conferences about Offline First, and organizes an area girls’s know-how group. In her spare time, Carmen defends Hyrule from the evil Ganon and writes Twitter-bots.