[PWA] 10. Trigger a version update

Source: Internet
Author: User

When the Refersh button was clicked, we need to tell the waiting service worker to replace the current service worker right Away. Then we refresh the page load the latest cache from the new cache db.

There is three new components to help:

Service worker can all skipwaiting () while it is "waiting" or "installing".

Self.skipwaiting ()

Then the waiting service worker should take over right away.

So we want to call it function when user click on the Refresh button in the notification.

Then what can we send the signal from our page to waiting Servcie worker?

// From a page:reg.installing.postMessage ({foo: ' Bar '}); // In the service worker: function (event) {    //  {foo: ' Bar '}})

The page gets event when the value chanages, means a new service worker take over, we'll use this as a signal to reload Our page:

function () {    //  Navigator.serviceWorker.controller has changed})

--------------------------------------------------------

Our page: Send single to service worker ask it to reload:

function (worker) {  varthis. _toastsview.show ("New version available", {    buttons: [ ' Refresh ', ' dismiss ']  });  Toast.answer.then (function(answer) {    if (answer! = ' Refresh ') {       // Tell the service worker to skipwaiting      Worker.postmessage ({message: ' Skipwaiting '}}  );};

Service worker: Listen to the message event and call Skilwaiting ():

// Todo:listen for the ' message ' event, and call // skipwaiting If you get the appropriate message function (event) {    if(event.data.message = = "Skipwaiting") {        self.skipwaiting ();    }})

The on We page, listen to Controllerchange event, Insdie load the page:

Indexcontroller.prototype._registerserviceworker =function() {  if(!navigator.serviceworker)return; varIndexcontroller = This; Navigator.serviceWorker.register ('/sw.js '). Then (function(reg) {if(!Navigator.serviceWorker.controller) {return; }    if(reg.waiting) {indexcontroller._updateready (reg.waiting); return; }    if(reg.installing) {indexcontroller._trackinstalling (reg.installing); return; } reg.addeventlistener (' Updatefound ',function() {indexcontroller._trackinstalling (reg.installing);    }); //Todo:listen for the controlling service worker changing    //and reload the pageNavigator.serviceWorker.addEventListener (' Controllerchange ',function() {window.location.reload (); })  });};

[PWA] 10. Trigger a version update

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.