Show New App Version Available using Service Worker in React.

Let's first understand what is service worker?

A service worker is a script that your browser runs in the background, separate from a web page. Using service workers you can run your app in offline mode. It also allows users to install your web app in their Mobile.

  • If you want to use a service worker then set up your app using create-react-app cli because it provides service worker related boilerplate(Template).
  • Now go to the main index.js file.
  • Now in that file, perform following steps:
  • You can see:
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
serviceWorker.unregister()
  • Just change the unregister to register.
  • Why should you change it to register?
  • When you do this browser register service worker and start installing service worker.
  • Below is the life cycle of a service worker.
        - install: this is the first event which is called when the installation of service worker get started.
     - activate: fired when service worker successfully installed and becomes active.
      - updatefound: fired when new service worker available. this new service worker keep waiting until old service get unregistered. so, we have to listen on this event and based on that event we have to show new version available dialog to the user and perform a hard reload to get updated content on page.

Now, We are going to learn how to show app version update dialog to the user.

  • Go to the src/index.js file.
  • Remember, I told you to change serviceWorker.unregister() to serviceWorker.register().
  • Now, You have to call the register method by passing configuration object to the method.

src/index.js:


const configuration = {
 onUpdate: (registration) => {
   if (registration && registration.waiting) {
     if (window.confirm('New version available!  refresh to update your app?')) {
       registration.waiting.postMessage({ type: 'SKIP_WAITING' });
       window.location.reload();
     }
   }
 }
};
 
serviceWorker.register(configuration);
  • In the code above, We have a configuration object which contains the onUpdate method.
  • This method is invoked whenever a new service worker is available and an 'updatefound' method is fired.
  • In this method, What we did is simply check new service worker is waiting to take control over the page or not.
  • If it is waiting then, We just show a new version popup available to the user.
  • If a user click on the ok button, then we simply refresh the page. So that, this user can see new changes in the web app.
  • The last step is just updating the service worker file by putting or changing version or timestamp in service worker file whenever you are going to deploy new changes to the production.

serviceWorker.js:

const VERSION = require('../package.json').version; 
           OR 
timestamp = 1592035319057

src/index.js:

import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below.
const configuration = {
  onUpdate: (registration) => {
    if (registration && registration.waiting) {
      if (window.confirm('New version available!  refresh to update your app?')) {
        registration.waiting.postMessage({ type: 'SKIP_WAITING' });
        window.location.reload();
      }
    }
  }
};
  
serviceWorker.register(configuration);
  • Note: We can trigger any component of any framework to show them instead of basic confirm alert.

Conclusion

Finally, We have cache busted the React app created using create-react-app cli and Service Worker.

  • If you want to learn more about service worker then you can visit following link:

https://developers.google.com/web/fundamentals/primers/service-workers