[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 ...
随机推荐
- ExtJs 第二章,Ext.form.Basic表单操作
1.认识Ext.form.Panel表单面板 Ext.form.field.CheckBox 复选框 checkboxfield Ext.form.CheckBoxGroup 复选框组 ...
- yii2源码学习笔记(四)
继续了解组件Component.php /** * Returns a value indicating whether a property is defined for this componen ...
- web字体格式转换
@font-face { font-family: 'emotion'; src: url('emotion.eot'); /* IE9*/ src: url('emotion.eot?#iefix' ...
- 我和CPP的第二次约会
1.变量之间的运算形式依赖于变量的数据类型,如i = i + j;当 i 和 j 是整型或者浮点型,则代表两个数的相加,如果是第一章所说的Sales_item类型,那么就是这两个变量的成分相加(如果书 ...
- F题 - A+B for Input-Output Practice (V)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description You ...
- 转:PHP非阻塞模式
你可以任意转摘“PHP非阻塞模式”,但请保留本文出处和版权信息.作者:尘缘,QQ:130775,来源:http://www.4wei.cn/archives/1002336 让PHP不再阻塞当PHP作 ...
- yum仅下载RPM包不安装
http://www.ttlsa.com/linux/howto-yum-download-rpm-without-install/
- cf B Inna and Candy Boxes
题意:输入n,然后输入n个数ai,再输入n个数bi,如果在1-ai中能找到两个数x,y,x和y可以相等,如果x+y=bi,答案加上x*y,否则减去1,让结果尽可能大,输出结果. #include &l ...
- myisam和innodb索引实现的不同
1.MyISAM 使用B+Tree 作为索引结构,叶子节点的data存放指针,也就是记录的地址.对于主键索引和辅助索引都是一样的.2.InnoDB 也使用B+Tree作为索引结构,也别需要注意的是,对 ...
- PL/SQL developer export/import (转)
export/import图标为灰色:原因:相关应用程序没有关联菜单栏 --> Tools --> Import Tables... --> Oracle Import Export ...