[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 . ...
随机推荐
- linux负载均衡
1.linux lvs nat实现负载均衡 添加两块网卡并开启路由管道 > /proc/sys/net/ipv4/ip_forward //开始路由管道 安装ipvsadm yum instal ...
- print 函数的进一步理解
没有括号的时候,pritn是列表操作符,会把其后列表里所有东西都数出来. 但是假如print后面紧跟着左括号,它就是一个函数调用,只会将括号内的东西输出来. “假如它看起来像函数调用,它就是一个函数调 ...
- iOS 数据持久性存储-对象归档
对象归档是将对象归档以文件的形式保存到磁盘中(也称为序列化,持久化),使用的时候读取该文件的保存路径读取文件的内容(也称为解档,反序列化) 主要涉及两个类:NSKeyedArichiver.NSKey ...
- Java多线程:Semaphore
Semaphore为并发包中提供用于控制某资源同时可以被几个线程访问的类 实例代码: // 允许2个线程同时访问 final Semaphore semaphore = new Semaphore(2 ...
- 我用Emacs,后来转向Vim——Vim学习之Vim键盘图(绝对值得珍藏)
Emacs本来就比较臃肿,麻烦.当我发现Vim键盘图时,我就渐渐转向Vim,追随Unix/Linux哲学去了.. 我用了Emacs三个月,因为它的学习曲线没Vim陡,这点吸引了,我使用Linux才7. ...
- .Net用js实现aspx页面删除TextBox输入框的前后空格
去掉TextBox输入框两头的前后空格:onblur="this.value=this.value.replace(/^\s+|\s+$/g,'');" str为要去除空格的字符串 ...
- Atom package安装失败的解决方案
cd ~/.atom/package git clone [package url] cd [package name] apm install [package name] if lack some ...
- cocod2d-x 之 HelloWorld
cocos2d-x 2.2创建项目 进入cocos2d-x-2.2/tools/project-creator,运行命令 python create_project.py -project MyGam ...
- PHP不依赖系统自动执行机制
不依赖系统,以及不依赖yii事物机制,则考虑人为触发.触发可以写在总体的公共页面上,但是考虑到对数据库以及WWW服务器的压力问题,程序的延迟问题,需要对执行函数进行一些优化. 首先,我们考虑对数据库的 ...
- ubuntu各版本的区别
ubuntu.kubuntu以及xubuntu的区别Ubuntu默认是Gnome:KUbuntu用的是KDE,效果比较炫目,但是系统要求也较高XUbuntu用的是Xface,比较轻量,系统要求较低,推 ...