[PWA] 1. Intro to Service worker
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 :
- 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- PWA - service worker - Workbox(未完)
Get Started(开始) 只有get请求才能cache缓存吗? Create and Register a Service Worker File(创建和注册 Service Worker) B ...
- Service Worker和HTTP缓存
很多人,包括我自己,初看Service Worker多一个Cache Storage的时候,就感觉跟HTTP长缓存没什么区别. 例如大家讲的最多的Service Worker能让网页离线使用,但熟悉H ...
- Service Worker
Service Worker 随着前端快速发展,应用的性能已经变得至关重要,关于这一点大佬做了很多统计.你可以去看看. 如何降低一个页面的网络请求成本从而缩短页面加载资源的时间并降低用户可感知的延时是 ...
- 转《service worker在移动端H5项目的应用》
1. PWA和Service Worker的关系 PWA (Progressive Web Apps) 不是一项技术,也不是一个框架,我们可以把她理解为一种模式,一种通过应用一些技术将 Web App ...
- 浏览器缓存和Service Worker
浏览器缓存和Service Worker @billshooting 2018-05-06 字数 6175 Follow me on Github 标签: BOM 1. 传统的HTTP浏览器缓存策略 ...
- [Angular] Service Worker Version Management
If our PWA application has a new version including some fixes and new features. By default, when you ...
随机推荐
- Window.onload事件
window.onload是一个事件,当文档加载完成之后就会触发该事件,可以为此事件注册事件处理函数,并将要执行的脚本代码放在事件处理函数中,于是就可以避免获取不到对象的情况
- dede操作成功信息提示修改
函数修改 include/common.func.php 文件 function ShowMsg()函数
- Java包详解
背景: 在java中要求文件名和类名相同,所以如果把多个类放在一起,就可能出现文件名冲突 所以用包来解决,一个包中可以包含多个类 包是java提供的一种用于区别类的名字空间的机制,是类的组织方式,是一 ...
- Bow模型(解释的很好)
Bag-of-words model (BoW model) 最早出现在NLP和IR领域. 该模型忽略掉文本的语法和语序, 用一组无序的单词(words)来表达一段文字或一个文档. 近年来, BoW模 ...
- xamarin SimpleAdapter绑定出错问题
问题:今天在实验xamarin中SimpleAdapter绑定到ListView时,出现闪退的现象, 见图: 解决方法: SimpleAdapter中的构造函数public SimpleAdapter ...
- mysql oracle 删除外键约束
mysql alter table xxx drop foreign key xxx cascade; oracle alter table drop constraint xxxxx cascade ...
- Extjs打开window窗口自动加载html网页
Window inherits the autoLoad config option from Panel. Note that I included all config options below ...
- cf B George and Round
题意:输入n,m,下一行为n个数a1<a2<a3......<an:然后再输入m个数b1<=b2<=b3<.....<=bm: 每个ai都必须在b中找到相等的 ...
- 智能硬件开发如何选择低功耗MCU
本文将市场上典型的低功耗MCU系列进行了比较,分析得出基于ARM. Cortex M0+内核的MCU系列最适合穿戴式医疗设备的开发.设备开发者当密切关注其发展动向,结合现有的市场需求.产品体系的构建和 ...
- Android 中使用MediaRecorder进行录像详解(视频录制)
在这里给出自己的一个测试DEMO,里面注释很详细.简单的视频录制功能. package com.video; import java.io.IOException; import android.ap ...