Kick starting with Progressive Web Apps Part 2

Posted by Shailaja Shah · 28 Mar, 2017 · 3 min read
Kick starting with Progressive Web Apps Part 2

In the previous blog you learnt how to register service worker and cache the resources in our app on the initial load.Now when you open up a page that has a service worker already registered. Then if you open up another page that falls within the scope of that service worker the browser doesn’t spin up the another service worker it uses the same one. That means if you have multiple tabs open or windows open there exists only one service worker.

But what happens when you update the service worker, the new service worker sits idle waiting until all the windows using previous service worker gets closed.

In order to avoid this we can use skipWaiting() in install event and clients claim() in activate event so that without any waiting the new service worker takes over the control of the pages.

var cacheName = 'cache_version2';
    console.log("[Service Worker] Installed");
            console.log("[ServiceWorker] Caching cacheFiles");
        }).then(function() {
            return self.skipWaiting();
self.addEventListener('activate', function(e){
    console.log("[Service Worker] Activated");

But wait what happens to the files previously stored in the cache after installing a new one?

The browser doesn’t know whether you are going to need the old ones or not. So it’s up to you to clean the unused files from the cache.The activate event is the perfect place to do this.

var cacheName = 'cache_version2';
self.addEventListener('activate', function(e){
   console.log("[Service Worker] Activated");
     return Promise.all({
     if(thisCacheName != cacheName)
      console.log("[Service Worker] Removing Cached Files from", thisCacheName);
      return caches.delete(thisCacheName);
       return self.clients.claim();

When the activate event is fired the code gets a list of current cache keys and iterates through them using map function.It then compares the key to the current key and if they’re not equal purges them.

Note : If your service worker file doesn’t get updated and still points out the old service worker file be sure to check that your browser cache max age is set zero.(cache max age : 0)

To kick start with Progressive Web App you can go through my blogs :

1) Register service worker and cache the resources in our app on the initial load.

2) Update the old service worker and cache the new response in Cache Storage.

3) Get out the resources from the cache and intercept the network requests.

4) Make our web app installable via the Web App Manifest File.

5) Grey out the things when offline.
. . .

SoluteLabs is a high-performance team of 25 focused on mobile and web design and development; we have produced top #10 chart topping applications on Android and iOS app stores, graphics that have gone viral and applications with Millions of downloads.

Subscribe to The Friday Brunch
Our twice a month newsletter featuring insights and tips on how to build better experiences for your customers

Have a product idea?

Talk to our experts to see how you can turn it
into an engaging, sustainable digital product.