[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();
})
});
};
[PWA] 10. Trigger a version update的更多相关文章
- 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 ...
- 项目中使用了个quartz包,启动时提示Quartz version update check failed
<span style="font-family: Arial, Helvetica, sans-serif;">2015-09-13 00:12:02 Abstrac ...
- 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 ...
- windows 10 无法启动 windows update 服务 错误 0x80070005 拒绝访问
windows 10 无法启动 windows update 服务 错误 0x80070005 拒绝访问: 解决方法: 首先重命名系统盘 windows目录下的代号为“SoftwareDistribu ...
- Ubuntu Intel显卡驱动安装 (Ubuntu 14.04--Ubuntu 16.10 + Intel® Graphics Update Tool)
最近使用在使用Ubuntu时,发现大部分情况下,不安装显卡驱动,使用默认驱动,都是没有问题的,但对于一些比较奇特配置的电脑,如下所示,如果使用默认驱动,会时常莫名其妙死机crash,尤其是在使用Ope ...
- 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 ...
- 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 ...
- 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 ...
- pt-online-schema-change中update触发器的bug
pt-online-schema-change在对表进行表结构变更时,会创建三个触发器. 如下文测试案例中的t2表,表结构如下: mysql> show create table t2\G . ...
随机推荐
- windows7 jdk 环境变量添加
JAVA_HOME D:\Java;PATH %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;CLASSPATH .;%JAVA_HOME%\lib;%JAVA_HOME%\l ...
- call/apply的第一个参数如果为null。this指向window
call/apply是用来改变函数的作用域的,第一次参数为this,第二个参数为传输的值,例如 var a ="windowA"; var b = "windowB&qu ...
- yii2源码学习笔记(三)
组件(component),是Yii框架的基类,实现了属性.事件.行为三类功能,如果需要事件和行为的功能,需要继承该类. yii\base\Component代码详解 <?php /** * @ ...
- PHP下编码转换函数mb_convert_encoding与iconv的使用说明
mb_convert_encoding这个函数是用来转换编码的. 不过英文一般不会存在编码问题,只有中文数据才会有这个问题.比如你用Zend Studio或Editplus写程序时,用的是gbk编码, ...
- MySql数据库3【优化2】sql语句的优化
1.SELECT语句优化 1).利用LIMIT 1取得唯一行[控制结果集的行数] 有时,当你要查询一张表是,你知道自己只需要看一行.你可能会去的一条十分独特的记录,或者只是刚好检查了任何存在的记录数, ...
- css实现三角的一些方法
css实现三角没有想象中的那么难,只要明白border的各种属性的意思就很好明白css三角是如何实现的. 一下是几个很简单的例子: css三角形状的制作: css样式: .trian ...
- YII 自动引入juquery进行表单验证
在form表单 里面引入这么一句话 array( 'enableClientValidation'=>true, 'clientOptions'=>array( ...
- cf C On Number of Decompositions into Multipliers
题意:给你n个数,然后把这个n个数的乘积化成n个数相乘,可以化成多少个. 思路:分解质因数,求出每一个质因子的个数,然后用组合数学中隔板法把这些质因子分成n分,答案就是所有质因子划分成n份的情况的乘积 ...
- Truck History(poj 1789)
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- Linux2.6内核--内存管理(2)--区
由于硬件的限制,内核不能对所有的页一视同仁.有些页位于内存中的特定物理地址上,所以,不能将其用于一些特别的任务.(关于内存分页机制可以查看:http://blog.csdn.net/dlutbruce ...