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();
})
});
};

[PWA] 10. Trigger a version update的更多相关文章

  1. APPLE-SA-2019-3-25-2 macOS Mojave 10.14.4,Security Update 2019-002 High Sierra, Security Update 2019-002 Sierra

    APPLE-SA-2019-3-25-2 macOS Mojave 10.14.4, Security Update2019-002 High Sierra, Security Update 2019 ...

  2. 项目中使用了个quartz包,启动时提示Quartz version update check failed

    <span style="font-family: Arial, Helvetica, sans-serif;">2015-09-13 00:12:02 Abstrac ...

  3. Windows10+Android Studio 3.5编译项目报错——NDK Resolution Outcome: Project settings: Gradle model version=4.10.1, NDK version is UNKNOWN

    项目背景: 系统有C.D两个盘,Android Studio安装在D盘,sdk安装在C盘. 出现的问题: 从git拉取项目后,一直编译不通过,提示“NDK Resolution Outcome: Pr ...

  4. windows 10 无法启动 windows update 服务 错误 0x80070005 拒绝访问

    windows 10 无法启动 windows update 服务 错误 0x80070005 拒绝访问: 解决方法: 首先重命名系统盘 windows目录下的代号为“SoftwareDistribu ...

  5. Ubuntu Intel显卡驱动安装 (Ubuntu 14.04--Ubuntu 16.10 + Intel® Graphics Update Tool)

    最近使用在使用Ubuntu时,发现大部分情况下,不安装显卡驱动,使用默认驱动,都是没有问题的,但对于一些比较奇特配置的电脑,如下所示,如果使用默认驱动,会时常莫名其妙死机crash,尤其是在使用Ope ...

  6. Resolve Error While Windows 10 Updating Cross Multiple Updating Versions (Such as Error 0x800f0831 when Update KB4556799) | 解决跨多个更新版本升级Windows 10时遭遇错误的问题(如 KB4556799 / 错误0x800f0831)

    Upgrade memory size for a laptop recently, the OS on the laptop was Windows 10 with a version of 201 ...

  7. MySQL - 问题集 - 触发器更新本表数据异常"Can’t update table ‘tbl’ in stored function/trigger because it is already used by statement which invoked this"

    如果你在触发器里面对刚刚插入的数据进行了 insert/update, 则出现这个问题.因为会造成循环的调用. create trigger test before update on test fo ...

  8. MySQL触发器更新本表数据异常:Can't update table 'tbl' in stored function/trigger because it

    MySQL触发器更新本表数据异常:Can't update table 'tbl' in stored function/trigger because it 博客分类: 数据库 MySQLJava ...

  9. pt-online-schema-change中update触发器的bug

    pt-online-schema-change在对表进行表结构变更时,会创建三个触发器. 如下文测试案例中的t2表,表结构如下: mysql> show create table t2\G . ...

随机推荐

  1. PHP 用Class构造JSON数据

    header('Content-type: appliction/json; charset=shift-JIS'); // error //{ // "result": fals ...

  2. request.getSession()

    request.getSession(); 与request.getSession(false);区别 服务器把session信息发送给浏览器   浏览器会将session信息存入本地cookie中 ...

  3. PHPCMS(2)PHPCMS V9 环境搭建(转)

    转自:http://www.cnblogs.com/Braveliu/p/5072920.html PHPCMS V9的学习总结分为以下几点: [1]PHPCMS 简介 PHP原始为Personal ...

  4. 技术大牛面试 http://www.itmian4.com/forum.php?mod=viewthread&tid=3824

    不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,NOI金牌,开源社区名人,三年级开始写Basic...在跪拜之余我们不禁要想,和这些牛人比,作为绝大部分技 ...

  5. Android SurfaceView使用

    与View区别 更新View任务太重会导致UI线程阻塞 而SurfaceView不会,可以在UI线程之外更新UI 工程代码 SurfaceViewDemo.zip ------------------ ...

  6. Oracle数据库还原方法

    Win +X → 运行→cmd C:\Documents and Settings\Administrator>sqlplus /nolog SQL> connect sys/passwo ...

  7. c++ 顺序容器学习 - 容器适配器

    摘要: 对 容器适配器 的疑问. 刚开始接触 容器适配器 时,总感觉怪怪的,认为多此一举,顺手搜了搜,原来我在这一点is not alone: STL容器适配器的用途 其中有个老兄说的好,这里 引用一 ...

  8. hibernate 一张数据表的流程

    1. 写一个domain类来映射数据库表 2. 写一个*.hbm.xml文件来配置映射 <?xml version="1.0"?> <!DOCTYPE hiber ...

  9. [BZOJ 3747] [POI 2015] Kinoman【线段树】

    Problem Link : BZOJ 3747 题解:ZYF-ZYF 神犇的题解 解题的大致思路是,当区间的右端点向右移动一格时,只有两个区间的左端点对应的答案发生了变化. 从 f[i] + 1 到 ...

  10. codeforces D. Long Path

    http://codeforces.com/contest/408/problem/D 题意:有一排房间每个房间有两扇门,一扇通往第i+1个房间,另一扇通往第p[i]个房间,(p[i]<=i)然 ...