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

To get our service worker, we need to :

  1. Register the service worker.

Service worker in our code is just a javascirpt file.

Once the page loaded, we want to register our serivce worker:

IndexController.prototype._registerServiceWorker = function() {
// TODO: register service worker
if(!navigator.serviceWorker) return; // Check whether service worker is available
navigator.serviceWorker.register('/sw.js') // register the service worker, locate in ../sw/index.js
.then( (reg) => {
console.log("SW registered"); // success
})
.catch( (err) => {
console.log("SW faild"); // Wrong
})
};

In register() function, it can take second param, which is scope, for example:

  navigator.serviceWorker.register('/sw.js', {
scope: '/foo/'
})
.then( (reg) => {
console.log("SW registered");
})
.catch( (err) => {
console.log("SW faild");
})

SO service worker will available only in the '/foo/' scope and its child scope, such as: '/foo/bar', but NOT '/foo', so the ending '/' is very important. Normally, we don't need to add scope, we want our service worker listeren to the root scope, so it is available to all the child components.

2. Our service worker file:

// sw.js

self.addEventListener('fetch', function(event) {
console.log(event.request);
});

Once you fetch the page seond time (first time service work did cache stuff), second time you will see in the console, there are lots of requests log out.


Of course, service worker have other event listeners:

self.addEventListener('install', function(event) {
// ..
}); self.addEventListener('activate', function(event) {
// ..
}); self.addEventListener('fetch', function(event) {
console.log(event.request);
});

[Notice] Service worker only works for HTTPS and localhost.

Reference: Link

[PWA] 1. Intro to Service worker的更多相关文章

  1. [PWA] 9. Service worker registerion && service work's props, methods and listeners

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

  2. [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 ...

  3. [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 ...

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

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

  5. Service Worker和HTTP缓存

    很多人,包括我自己,初看Service Worker多一个Cache Storage的时候,就感觉跟HTTP长缓存没什么区别. 例如大家讲的最多的Service Worker能让网页离线使用,但熟悉H ...

  6. Service Worker

    Service Worker 随着前端快速发展,应用的性能已经变得至关重要,关于这一点大佬做了很多统计.你可以去看看. 如何降低一个页面的网络请求成本从而缩短页面加载资源的时间并降低用户可感知的延时是 ...

  7. 转《service worker在移动端H5项目的应用》

    1. PWA和Service Worker的关系 PWA (Progressive Web Apps) 不是一项技术,也不是一个框架,我们可以把她理解为一种模式,一种通过应用一些技术将 Web App ...

  8. 浏览器缓存和Service Worker

    浏览器缓存和Service Worker @billshooting 2018-05-06 字数 6175 Follow me on Github 标签: BOM 1. 传统的HTTP浏览器缓存策略 ...

  9. [Angular] Service Worker Version Management

    If our PWA application has a new version including some fixes and new features. By default, when you ...

随机推荐

  1. ThinkPHP接入支付宝支付功能

    最近做系统,需要实现在线支付功能,毫不犹豫,选择的是支付宝的接口支付功能.这里我用的是即时到帐的接口,具体实现的步骤如下: 一.下载支付宝接口包 下载地址:https://b.alipay.com/o ...

  2. ecshop分页问题1

    点解下一页时弹出 查找原因: json返回 分页查询之后返回的 filter 数据为空 问题在这: $deliveryInfo['fliter']  $deliveryInfo['page_count ...

  3. C#中静态方法和非静态方法的区别(二)

    一.引言 在C#中,静态和非静态的特征对于我们来说是再熟悉不过了,但是很少看到有一篇文章去好好地总结静态和非静态它们之间的不同,为了帮助大家更好地去理解静态和非静态特征, 所以将在这篇文章中帮大家全面 ...

  4. ios 排序汇总

    ios 排序汇总  IOS几种简单有效的数组排序方法 //第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象 N ...

  5. instancetype 与 id for Objective-C

    instancetype.id.NSObject的区别 - simalone   1.instancetype只能用于方法的返回类型,而id用处和NSObject *类似. 2.instancetyp ...

  6. duplicate symbols for architecture arm64 (Xcode error)

    比如 duplicate symbol _NewBase64Encode_soomla in: /Users/UnityGame/Libraries/Plugins/iOS/Soomla/libSoo ...

  7. Unity NGUI 3.0.4版本 制作网络版斗地主

    Unity NGUI 3.0.4版本 @by 灰太龙  开发环境 Win7旗舰版 Unity 4.2.1f4 本文就写个开门篇,告诉大家怎么用NGUI,第一步导入NGUI 3.0.4版本! 1.启动U ...

  8. java interface

  9. STM32F10x 学习笔记5(USART实现串口通讯 1)

    STM32F10x 系列单片机中都包含了USART 模块,所谓USART,就是通用同步异步收发器.通用同步异步收发器(USART)提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间 ...

  10. 【算法Everyday】第二日 求子数组的最大和

    题目 // 3.求子数组的最大和 // 题目: // 输入一个整形数组,数组里有正数也有负数. // 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. // 求所有子数组的和的最大值. ...