Using Service Workers

1、This is an experimental technology
  Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

  

  

  

  Safari:Desktop、Mobile均不支持。

  IE:Desktop不支持。

  Chrome:Desktop、Mobile 40.0版本后均支持。

  FireFox:Desktop、Mobile 33.0版本后均支持。

  Android WebView:不支持。Android Webview不支持,导致应用前景堪忧。

2、Note: As of Firefox 44, when AppCache is used to provide offline support for a page a warning message is now displayed in the console advising developers to use Service workers instead

  Firefox 44版本以后,已不推荐使用AppCache,推荐使用Service workers。

3、You’ll also need to serve your code via HTTPS — Service workers are restricted to running across HTTPS for security reasons.

4、Base Architecture

  1)通过 serviceWorkerContainer.register() 注册service worker url.

  2)注册成功后,the service worker is executed in a ServiceWorkerGlobalScoperunning off the main script execution thread, with no DOM access.

  3)The service worker is now ready to process events.

  4)Installation of the worker is attempted when service worker-controlled pages are accessed subsequently. An Install event is always the first one sent to a service worker (this can be used to start the process of populating an IndexedDB, and caching site assets).

  ServiceWorker控制的页面被访问时,ServiceWorker即会被安装。Install 事件是ServiceWorker收到的第一个事件,通过在此事件中,从indexDB、cache中导出数据 。

  5)When the oninstall handler completes, the service worker is considered installed.

  6)Next is activation. When the service worker is installed, it then receives an activate event. The primary use ofonactivate is for cleanup of resources used in previous versions of a Service worker script.

  7)The Service worker will now control pages, but only those opened after the register() is successful. i.e. a document starts life with or without a Service worker and maintains that for its lifetime. So documents will have to be reloaded to actually be controlled.

5、注册示例:

if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw-test/sw.js', {scope: '/sw-test/'})
.then(function(reg) {
// registration worked
console.log('Registration succeeded. Scope is ' + reg.scope);
}).catch(function(error) {
// registration failed
console.log('Registration failed with ' + error);
});
}

6、A single service worker can control many pages. Each time a page within your scope is loaded, the service worker is installed against that page and operates on it. Bear in mind therefore that you need to be careful with global variables in the service worker script: each page doesn’t get its own unique worker.

 一个service worker可以控制其scope下的所有页面,即一个service worker可对应多个页面。

 而一个页面也可能属于多个scope,即一个页面可对应多个service worker。

7、You are not running your application through HTTPS.

  使用https协议的站点,才能注册service worker。

8、The max scope for a service worker is the location of the worker.

9、The install event is fired when an install is successfully completed. The install event is generally used to populate your browser’s offline caching capabilities with the assets you need to run your app offline. To do this, we use Service Worker’s brand new storage API — cache — a global on the service worker that allows us to store assets delivered by responses, and keyed by their requests. This API works in a similar way to the browser’s standard cache, but it is specific to your domain. It persists until you tell it not to — again, you have full control.  

  安装成功后,install event被触发。

参考:https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers

Using Service Workers的更多相关文章

  1. PWA & Service Workers 版本更新 bug

    PWA & Service Workers 版本更新 bug PWA & Service Worker https://developer.mozilla.org/zh-CN/docs ...

  2. [翻译]Service workers:PWA背后的英雄

    原文地址:https://medium.freecodecamp.org/service-workers-the-little-heroes-behind-progressive-web-apps-4 ...

  3. Webpack之(progressive web application) - PWA中的 Service Workers 是什么

    学习文档:https://webpack.docschina.org/guides/progressive-web-application/ 参考文档:https://developers.googl ...

  4. Cesium原理篇:4Web Workers剖析(2)

    What's the WebWorkers? 2008 年 W3C 制定出第一个 HTML5 草案中提出了工作线程(Web Worker)的概念,并且规范出 Web Worker 的三大主要特征:能够 ...

  5. Service Worker基础知识整理

    Service Worker是什么 service worker 是独立于当前页面的一段运行在浏览器后台进程里的脚本.它的特性将包括推送消息,背景后台同步, geofencing(地理围栏定位),拦截 ...

  6. Service Worker MDN英文笔记

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

  7. 通过使用Web Workers,Web应用程序可以在独立于主线程的后台线程中,运行一个脚本操作。这样做的好处是可以在独立线程中执行费时的处理任务,从而允许主线程(通常是UI线程)不会因此被阻塞/放慢。

    Web Workers API - Web API 接口参考 | MDNhttps://developer.mozilla.org/zh-CN/docs/Web/API/Web_Workers_API ...

  8. JavaScript是如何工作的:Service Worker的生命周期及使用场景

    摘要: 理解Service Worker. 原文:JavaScript 是如何工作的:Service Worker 的生命周期及使用场景 作者:前端小智 Fundebug经授权转载,版权归原作者所有. ...

  9. JavaScript是如何工作的:Web Workers的构建块 + 5个使用他们的场景

    摘要: 理解Web Workers. 原文:JavaScript是如何工作的:Web Workers的构建块 + 5个使用他们的场景 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 这 ...

随机推荐

  1. spring揭密学习笔记(3)-spring ioc容器:Spring的IoC容器之BeanFactory

    1. Spring的IoC容器和IoC Service Provider的关系 Spring的IoC容器和IoC Service Provider所提供的服务之间存在一定的交集,二者的关系如图4-1所 ...

  2. 关于xampp默认安装后mysql/mariadb密码的修改

    关于xampp默认按照后mysql/mariadb密码的修改 默认安装的mysql/mariadb 是没有密码的 只能跳过  然后进行修改密码 /opt/lampp/bin/mysqld_safe - ...

  3. js: 字符集

    用js生成字符集 一般网页制作中需要一些向上向下的小箭头,用图片非常不合算(一个页面发起多个http请求.css.文件大小等方面考虑) 所以用一些字符集的字符图形,效果很好 下面是用js生成字符集,以 ...

  4. 24.纯 CSS 创作出平滑的层叠海浪特效

    原文地址:https://segmentfault.com/a/1190000014895634 感想:这里的波浪只是侧面的,利用几个平面一部分弧旋转得到. HTML代码: <div class ...

  5. __file__ 作用以及模块导入方法

    python 执行py 文件的时候,默认就会把当前目录增加到sys.path中 import os print(__file__) #打印文件当前的位置 直接在目录里面执行,结果显示当前文件(pych ...

  6. golang 的 mysql 操作

    goLang的mysql操作,大致可分为三个步骤: 1.下载mysql驱动:go get github.com/go-sql-driver/mysql 2.建立连接:sql.Open("my ...

  7. linux下通过sed命令直接修改文件内容

    sed是实现对流的编辑.通常,我们使用sed可以实现内容的编辑后然后保存成另外的一个文件,如果正确的话,才写入到源文件.但是某些时候,我们需要直接修改文件,因为,保存文件到一个文件,然后再覆盖原文件的 ...

  8. 关于vector变量的size,是一个无符号数引发的bug。LeetCode 3 sum

    class Solution { public: vector<vector<int>> threeSum(vector<int>& a) { vector ...

  9. js 提示框的实现---组件开发之(一)

    自己做了一个简单的提示框,供自己使用,也可以供他人参考,看懂此文,是理解组件开发的入门 思路比较简单: 1.常规写法: 1.1. 创建一个构造函数 1.2. 给构造函数的原型对象添加show 和hid ...

  10. Zookeeper原理架构

    Zookeeper到底是什么!? 学一个东西,不搞明白他是什么东西,哪还有心情学啊!! 首先,Zookeeper是Apache的一个java项目,属于Hadoop系统,扮演管理员的角色. 然后看到官网 ...