In some rare cases, you need to ask user to refresh the browsser to update the version. Maybe because some secrity issues.

As we have learnt so far. And code change will generate a new service work in the waiting list. Unitl the current service worker completely die (hard refresh or close the window). Then the new service work will take control.

So what we want to do is when there is a new service work ready, we want to notify user that he or she need to refresh the browser to update the appliation version.

First, let's see what kinds of event listener and status service worker has.

Once we register the service worker, it will return a registerion object:

navigation.serviceWorker.register('/sw.js').then(function(reg) {
// method
reg.unregister();
reg.update(); // state
/*
Pointing to the serviceWorker object or be null
*/
reg.installing; // start installing new service worker, if install faild then throw away
reg.waiting; // service worker is ready to take over
reg.activate; // service worker is not take over control // has listener when update is found
reg.addEventListener('updatefound', function(){
// reg.installing is changed
})
})

API: Link

For the service worker itself:

var sw = reg.installing;
console.log(sw.state); // ...logs "installing"
// state can also be:
// "installed"
// "activating"
// "avvtivated"
// "redundant" sw.addEventListener('statechange', function(){
// sw.state has changed
})

Aslo about:

naviagetor.serviceWorker.controller

"navigator.serviceWorker.controller" refer to the service worker that controls the page.

So if there is no controller, then it means the data is loading from network:

if(!navigator.serviceWorker.controller){
// page didn't load using a service worker but from network
}

Otherwise we need to look for registration.

If there is a "waiting" worker:

if(reg.waiting){

   // there is a update ready! Notify user
}

Else If there is a "installing" worker:

if(reg.installing){
// there is an update in progress, but update may fail
// So we still need to track state change
// if it reached installed statue, then notify user
reg.installing.addEventListener('statechange', function(){
if(this.state == "installed"){
// there is an update ready!
}
})
}

Otherwise, we listen 'updatefound', we track "installing" state until it reach "installed", then again we tell the user.

reg.addEventListener('updatefound', function(){
reg.installing.addEventListener('statechange', function(){
if(this.satate == "installed"){
// there is an update ready!
}
})
})

[PWA] 9. Service worker registerion && service work's props, methods and listeners的更多相关文章

  1. Service worker (@nuxtjs/workbox) 采坑记

    PWA(Progressive Web App)是前端的大趋势,它能极大的加快前端页面的加载速度,得到近乎原生 app 的展示效果(其实难说).PWA 其实是多种前端技术的组合,其中最重要的一个技术就 ...

  2. Service Worker MDN英文笔记

    前言: 以前学习基础知识的时候总看别人写的入门文章,但有时候还是一脸懵逼,直到自己用心阅读了MDN的英文文档才对基础知识的一些理论有了更深的理解,所以我在边阅读文档的时候边记录下帮助比较大的,也方便大 ...

  3. 渐进式web应用开发---service worker 原理及介绍(一)

    渐进式web应用(progressive Web app) 是现代web应用的一种新形式.它利用了最新的web功能,结合了原生移动应用的独特特性与web的优点,为用户带来了新的体验. 一:传统web端 ...

  4. 渐进式web应用开发---Service Worker 与页面通信(七)

    _ 阅读目录 一:页面窗口向 service worker 通信 二:service worker 向所有打开的窗口页面通信 三:service worker 向特定的窗口通信 四:学习 Messag ...

  5. [转] service worker初探:超级拦截器与预缓存

    在2014年,W3C公布了service worker的草案,service worker提供了很多新的能力,使得web app拥有与native app相同的离线体验.消息推送体验. service ...

  6. [PWA] 2. Service worker life cycle

    Once serive worker is registered, the first time we go to the app, we cannot see the logs from servc ...

  7. [PWA] 1. Intro to Service worker

    Service worker stays between our browser and noetwork requests. It can help to fetch data from cache ...

  8. [PWA] Show Notifications when a Service Worker is Installed or Updated

    Service Workers get installed and activated in the background, but until we reload the page they don ...

  9. PWA - service worker - Workbox(未完)

    Get Started(开始) 只有get请求才能cache缓存吗? Create and Register a Service Worker File(创建和注册 Service Worker) B ...

随机推荐

  1. Idea中运行Testng时,报SAXParseException:parallel为none的问题原因及解决

    今天更新了testng的版本为6.9.10, 在idea中运行测试案例时,报错如下: org.testng.TestNGException: org.xml.sax.SAXParseException ...

  2. 封装JDBC:实现简单ORM框架lfdb

    作者:Vinkn 来自http://www.cnblogs.com/Vinkn/ 一.简介 框架就是一组可重用的构件,LZ自己写的姑且就叫微型小框架:lfdb.LZ也对其他的ORM框架没有什么了解,现 ...

  3. MS SQL SERVER 2008 R2 实例服务启动出现10048错误解决办法

    由于个人癖好,把MSSQLSERVER服务禁止了开机启动,每次需要的时候就输入CMD命令开启.今天在开启的时候,系统提示“发生服务特定错误:10048”. 于是打开SQL Server配置管理器,发现 ...

  4. php基础知识【函数】(4)时间date

    一.time() -- 返回当前的 Unix 时间戳 $nextWeek = time() + (7 * 24 * 60 * 60); echo 'Next Week: '. date('Y-m-d' ...

  5. opencv数据结构总结

    OpenCV里面用到了很多图像相关的数据结构,熟练掌握它们是学习图像的基础. 1.IplImage IplImage IplImage IPL 图像头 typedef struct _IplImage ...

  6. C#【数据库】 Access类

    using System; using System.Data; using System.Data.OleDb; namespace AccessDb { /**//// <summary&g ...

  7. Python爬虫第一步

    这只是记录一下自己学习爬虫的过程,可能少了些章法.我使用过的是Python3.x版本,IDE为Pycharm. 这里贴出代码集合,这一份代码也是以防自己以后忘记了什么,方便查阅. import req ...

  8. RabbitMQ启动出错:- unable to connect to epmd on xxxx: timeout (timed out)

    yum install后启动rabbitmq报错: [root@www ~]# /etc/init.d/rabbitmq-server start Starting rabbitmq-server: ...

  9. 测试通过Word直接发布博文

    这里是来自word 2013的一篇测试文章. 测试直接通过Word自带的bloger功能发布博客文章. 这里插入一张图片

  10. DevExpress::XtraBars::BarEditItem获取EditValue值事件

    //视图设计器中拖动一个barManager,添加一个bar,再添加一个BarEditItem控件,如下代码: private: DevExpress::XtraEditors::Repository ...