[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 ...
随机推荐
- Python下调用json.dumps中文显示问题解决办法
json.dumps在默认情况下,对于非ascii字符生成的是相对应的字符编码,而非原始字符,例如: import json js = json.loads('{"haha": & ...
- Meta元素可视区
一.网页手机wap2.0网页的head里加入下面这条元标签,在iPhone的浏览器中页面将以原始大小显示,并不允许缩放. <meta name="viewport" cont ...
- PHP面向对象(OOP):__call()处理调用错误
在程序开发中,如果在使用对象调用对象内部方法时候,调用的这个方法不存在那么程序就会出错,然后程序退出不能继续执行.那么可不可以在程序调用对象内部 不存在的方法时,提示我们调用的方法及使用的参数不存在, ...
- 简述一个javascript简单继承工具的实现原理
背景 由于本人非常希望能够开发自己的游戏,所以业余时间一直在想着能不能自己一些好玩又有趣的东西出来,最近随着steam上众多独立游戏的爆发,感觉自己又燃烧了起来,所以又拾起了很久以前的一个2d引擎,决 ...
- 在Adobe Reader中保存PDF表单数据的方法
通常,Adobe Reader 用户填写表单后,是无法保存所填表单的副本的.但是,对于 Reader 8 和更高版本的用户,您可以扩展其权限,使他们可以完成此操作.如果您使用的是 Acrobat Pr ...
- .c 文件取为.o文件
$(xxx:%.c=%.o) 即可 例子: $(ALLFILES:%.c=%.o)
- jQuery--Promise object
http://blog.mediumequalsmessage.com/promise-deferred-objects-in-javascript-pt2-practical-use http:// ...
- ios开发学习- 简易音乐播放器2 (基于iPhone4s屏幕尺寸)-- 歌词解析--plist文件应用--imageNamed图片加载耗内存
声明:(部分图片来自网络,如果侵犯了您的权益请联系我,会尽快删除!) 又是音乐播放器,不过这次和上次不一样了,准确说这次更像播放器了,初学者不建议看这个,可以先看前面一个音乐播放器(1),当然 我没加 ...
- 源代码安装GIT
参考URL:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25150840&id=4250659 若是条件允许,从源代 ...
- 【HDOJ】1483 Automatic Correction of Misspellings
水模拟题. /* 1483 */ #include <cstdio> #include <cstring> #include <cstdlib> #define M ...