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. web版扫雷小游戏(二)

    接上篇~~第一次写这种技术博客,发现把自己做的东西介绍出来还是一件脑力活,不是那么轻松啊,好吧,想到哪写到哪,流水记录之,待完成之后再根据大家的意见进行修改吧. 游戏实现 根据对扫雷游戏的体验和分析, ...

  2. C# .NET3.5 改为 到.NET2.0 时 TypedTableBase 报错解决方法

    NET 3.5 降版本 到.NET 2.0.不出意外,问题必然来了.编译错误一:错误 1 命名空间“System”中不存在类型或命名空间名称“Linq”(是缺少程序集引用吗?)解决:删掉该引用--没用 ...

  3. js切换换class

    1, js代码 function ntabs(thisObj,Num)            {if(thisObj.className == "active")return;   ...

  4. linker command failed with exit code 1

    这种问题,通常出现在添加第三方库文件或者多人开发时. 这种问题一般是找不到文件而导致的链接错误. 我们可以从如下几个方面着手排查. 1.以如下错误为例,如果是多人开发,你同步完成后发现出现如下的错误. ...

  5. avi文件格式详解【转】

    AVI是音频视频交错(Audio Video Interleaved)的英文缩写,它是Microsoft公司开发的一种符合RIFF文件规范的数字音频与视频文件格式,原先用于Microsoft Vide ...

  6. Java中接口与实例化

    一.问题引入         前两天学代理模式的时候想到的,接口可不可以new呢?         接口是特殊的抽象类,接口的方法都默认为  public  abstract  的... 抽象的方法不 ...

  7. George and Cards

    Codeforces Round #227 (Div. 2) E:http://codeforces.com/contest/387/problem/E 题意:给你一个n个数的序列,然后给你一个标准序 ...

  8. 使用 getNextException() 来检索已经过批处理的特定元素的异常。 ERRORCODE=-4228, SQLSTATE=null

    今天查询了一天发现的问题,用ibatis做批量操作时,报错: [非原子批处理出现故障]使用 getNextException() 来检索已经过批处理的特定元素的异常. ERRORCODE=-4228, ...

  9. 工程师必知ZigBee技术问答精华汇总

    本文是关于ZigBee技术的一些基础知识.行业应用方面的精华汇总.希望通过本文的分析,能让大家对ZigBee技术的起源.发展.特点.前景及其在通信网络中的相关应用有全面直观的了解. 1.基础知识篇 Q ...

  10. Strust2的json插件

    以下这段摘自网上: Json是一种轻量级的数据交换格式,JSon插件提供了一种名为json的ActionResultType .一旦为Action指定了该结果处理类型,JSON插件就会自动将Actio ...