How to Do PWA Auto Update in Ionic 4?

At first, we want to know what is PWA (Progressive Web Applications).PWA is a type of mobile app delivered through the web, built using common web technologies including HTML, CSS, and JavaScript. So, They are intended to work on any platform that uses a standards-compliant browser. Functionality includes working offline, push notifications, and device hardware access, enabling creating user experiences similar to native applications on mobile devices. Since they are a type of web page or website known as a web application, there is no requirement for developers or users to install the web apps via digital distribution systems like Apple App Store or Google Play. Now, we will discuss how to do progressive web applications auto-update in Ionic 4.

Now, with the Angular Service Worker and the Angular CLI built-in PWA support, it's now simpler than ever to make our web application downloadable and installable, just like a native mobile application

How to PWA auto-update works as defined below.

in app.module.ts file add the below code...

import { Component } from '@angular/core';
    import { SwUpdate } from '@angular/service-worker';

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {    

      constructor(public updates:SwUpdate) {
        updates.available.subscribe(event => {
          console.log('current version is', event.current);
          console.log('available version is', event.available);
        });
        updates.activated.subscribe(event => {
          console.log('old version was', event.previous);
          console.log('new version is', event.current);
        });

        updates.available.subscribe(event => {
            updates.activateUpdate().then(() => this.updateApp());
        });
       }

       updateApp(){
        document.location.reload();
        console.log("The app is updating right now");

       }
}

Conclusion:

It works for me and I personally tested the code. So, I installed it on my mobile. When I update my code and live it then open my PWA after some time it is updated. It is very important for Ionic mobile app development. Please share your reviews in the comment section.

We Serve clients globally in diverse industries

Stay Upto Date With Our Newsletter.