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. github 中redisPhpAdmin redis 可视化界面

    GITHUB地址:https://github.com/ErikDubbelboer/phpRedisAdmin 在php目录下执行 git clone https://github.com/Erik ...

  2. bzoj2127: happiness

    Description 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科与理科有着自己的喜悦值,而一对好朋友 ...

  3. you don't have permission 错误

    当引入第三方的框架的时候 容易产生以下问题: The file “XXX.app” couldn’t be opened because you don’t have permission to vi ...

  4. Oracle 精简绿色版客户端的配置

    在项目开发中常常用到Oracle.但Oracle 客户端体积很大.安装后,主要用的就1个功能:TNS配置服务名,偶尔用到SqlPlus.在开发过程中,大量使用Navicate和PL/SQL Devel ...

  5. .c 文件取为.o文件

    $(xxx:%.c=%.o) 即可 例子: $(ALLFILES:%.c=%.o)

  6. A Statistical View of Deep Learning (IV): Recurrent Nets and Dynamical Systems

    A Statistical View of Deep Learning (IV): Recurrent Nets and Dynamical Systems Recurrent neural netw ...

  7. XML CDATA

    /* <![CDATA[ */var mv_dynamic_to_top = {"text":"To Top","version":& ...

  8. 查看Ubuntu操作系统位数

    查看系统是32位还是64位 #查看long的位数,返回32或64 getconf LONG_BIT

  9. Election Time

    Election Time Time Limit: 1000MS Memory limit: 65536K 题目描述 The cows are having their first election ...

  10. 应用程序的关闭退出(在FMX中,Activity替代了Form的概念)

    在VCL中,关闭程序的主窗体也就意味着程序的主循环结束,主程序自然而然结束.所以在主窗体中使用窗体的关闭函数(Close)即可,如下: procedure TfrmMain.btncloseClick ...