[PWA] 10. Trigger a version update

When the refersh button is 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 are 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 refresh button in the notification.

 

Then how can we send the signal from our page to waiting servcie worker?

// from a page:
reg.installing.postMessage({foo: 'bar'});

// in the service worker:
self.addEventListener('message', function(event){
    event.data; // {foo: 'bar'}
})

 

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

navigator.serviceWorker.addEventListener('controllerchange', function(){
    // navigator.serviceWorker.controller has changed
})

 

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

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

复制代码
IndexController.prototype._updateReady = function(worker) {
  var toast = this._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
self.addEventListener('message', function(event){
    if(event.data.message == "skipWaiting"){
        self.skipWaiting();
    }
})

 

The on our page, listen to controllerchange event, insdie load the page:

复制代码
IndexController.prototype._registerServiceWorker = function() {
  if (!navigator.serviceWorker) return;

  var indexController = 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 page
    navigator.serviceWorker.addEventListener('controllerchange', function(){
      window.location.reload();
    })
  });
};
复制代码

 

posted @   Zhentiw  阅读(307)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-05-17 [R] Draw a wordcloud
点击右上角即可分享
微信分享提示