[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 ...
随机推荐
- 《uname命令》-linux命令五分钟系列之五
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...
- 命令行,备份、导入数据库Oracle
备份库:exp username/password@Database file="h:\datas.dmp" owner=username 导入语句 :imp username2/ ...
- wpf样式绑定 行为绑定 事件关联 路由事件实例
代码说明:我要实现一个这样的功能 有三个window窗口 每个窗体有一个label标签 当我修改三个label标签中任意一个字体颜色的时候 其他的label标签字体颜色也变化 首先三个窗体不用 ...
- CSS3中动画transform必须要了解的Skew变化原理
transform是CSS3中比较安全的动画(对于性能来说),其中有一些动画属性,来执行不同的变化.今天我们来了解skew的变化原理. skew,其实使用的频率不是很高,当然也没有最低.但是往往,一直 ...
- Magento 编译 php5.6.21 命令
./configure '--prefix=/alidata/server/php' '--enable-opcache' '--with-config-file-path=/alidata/ser ...
- Python 基础-python函数
函数 1.def 2.命名 3.函数体 4.return 返回值 def get_return(): a = 1 return a 函数参数有 形参和实参 定义几个形参就 ...
- [R] /usr/share/doc/apache2/README.Debian.gz
Contents======== Apache2 Configuration under Debian GNU/Linux Files and Directories in '/etc/apache2 ...
- iOS利用响应链机制点击tableview空白处关闭键盘-可以作为参考
http://www.jianshu.com/p/9717b792599c 是原文地址 处理关闭键盘的做法一般分为两种:1.放弃第一响应者身份:2.当前视图结束编辑.通常情况下只要我们在合适的时机 ...
- [BZOJ 1833] [ZJOI2010] count 数字计数 【数位DP】
题目链接:BZOJ - 1833 题目分析 数位DP .. 用 f[i][j][k] 表示第 i 位是 j 的 i 位数共有多少个数码 k . 然后差分询问...Get()中注意一下,如果固定了第 i ...
- Journey of Android for Mac
下了决心要学Java,几个同事都建议我去学Android,自己觉得能做点应用放手机上玩玩也比较有动力. 沈逸有篇文章里面写道: 搞C的看不起搞C++的. 搞C++的看不起搞java的. 搞java的看 ...