[PWA] 7. First Cache when installed
If you want your application works offline or lie-wifi. You need to use cache.
API:
Create Caches:
caches.open('cache_name').then( (cache) => {
// create name if not exists yet, return cache if there is a one
})
Create single cache:
cache.put(request, response); cache.addAll([
'/foo',
'/bar'
])
Get the cache:
cache.match(request) caches.match(request)
When to start cache:
We can do cache in 'installing' service worker, what it will do is fetch everything we need from network and create cache for each of them.

self.addEventListener('install', function(event) {
var urlsToCache = [
'/',
'js/main.js',
'css/main.css',
'imgs/icon.png',
'https://fonts.gstatic.com/s/roboto/v15/2UX7WLTfW3W8TclTUvlFyQ.woff',
'https://fonts.gstatic.com/s/roboto/v15/d-6IYplOFocCacKzxwXSOD8E0i7KZn-EPnyo3HZu7kw.woff'
];
event.waitUntil(
// TODO: open a cache named 'wittr-static-v1'
// Add cache the urls from urlsToCache
caches.open('wittr-static-v1')
.then( (cache) => {
cache.addAll(urlsToCache)
})
.catch( () => {
console.error("Cannot cache anything");
})
);
});
Now we have create the cache, but it is not useful until we use the cache.
To use cache, we can do:
self.addEventListener('install', function(event) {
var urlsToCache = [
'/',
'js/main.js',
'css/main.css',
'imgs/icon.png',
'https://fonts.gstatic.com/s/roboto/v15/2UX7WLTfW3W8TclTUvlFyQ.woff',
'https://fonts.gstatic.com/s/roboto/v15/d-6IYplOFocCacKzxwXSOD8E0i7KZn-EPnyo3HZu7kw.woff'
];
event.waitUntil(
// TODO: open a cache named 'wittr-static-v1'
// Add cache the urls from urlsToCache
caches.open('wittr-static-v4')
.then( (cache) => {
cache.addAll(urlsToCache)
})
.catch( () => {
console.error("Cannot cache anything");
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then((response)=>{
if(response){
return response;
}else{
return fetch(event.request);
}
})
)
});
So we use 'caches.match' to get all the caches for request.
In the then block, cache is successfully fetched, we check whether there is cache data, if it is, then return the response;
If there is no cache data, then we fetch the data from real-server.
But this approach has some problem:
After we go offline mode, the pictures are not showing, this is because we only cache when the servcie worker is installed.
So here is some problem we need to solve:

[PWA] 7. First Cache when installed的更多相关文章
- Distributed Cache Coherence at Scalable Requestor Filter Pipes that Accumulate Invalidation Acknowledgements from other Requestor Filter Pipes Using Ordering Messages from Central Snoop Tag
A multi-processor, multi-cache system has filter pipes that store entries for request messages sent ...
- linux apt-cache使用方法
apt-cache是linux下的一个apt软件包管理工具,它可查询apt的二进制软件包缓存文件.APT包管理的大多数信息查询功能都可以由apt-cache命令实现,通过apt-cache命令配合不同 ...
- 三本毕业(非科班),四次阿里巴巴面试,终拿 offer(大厂面经)
作者:gauseen 原文:https://github.com/gauseen/blog 公众号:「学前端」,只搞技术不搞广告文,欢迎关注~ 第一次 20:00 电话一面 - 自我介绍 - 对公司工 ...
- [PWA] 8.Unobtrusive update: Delete old cache and only keep one, hard refresh to let new SW to take control
So once you modify the code, service worker will auto create a new one and it won't take control ove ...
- [PWA] Cache JSON Data in a React PWA with Workbox, and Display it while Offline
We can view the PWA offline because we are caching the static and CDN assets for the app - but the l ...
- [PWA] 19. Cache the avatars
Cache the avatars is little different from cache photos. We need to serve the page with our cache da ...
- [PWA] 18. Clean the photo cache
We cannot let photo always keep caching new data without clean the old data. If message is not displ ...
- [PWA] 17. Cache the photo
To cache photo, You need to spreate cache db to save the photo. So in wittr example, we cache the te ...
- [PWA] 15. Using The IDB Cache And Display Entries
We want to use IDB to store the wittr messages. The logic is when the page start: service worker wil ...
随机推荐
- python3实现邮件发送程序
刚开始的想法很简单,由于有上千封的邮件需要发出去,并且需要一条一条发送,不能转发或群发,每条邮件要署对方的姓名,并加上几个相同的符件,考虑到手工操作繁琐无趣,所以想到用程序实现,python好像非常胜 ...
- TreeList的VisibleNodesCount,Noes.Count,AllNdoesCount以及焦点节点的删除
初始5个Nodes 隐藏23节点,打印全部节点Tag 显示23,打印全部节点Tag 隐藏全部节点,打印节点Tag TreeList.Nodes.Count == TreeList.AllNodesCo ...
- 最精简的代理设计模式demo - 保姆看孩子
1,协议文件 @protocol NursePtotocol <NSObject> //保姆的协议方法 - (void)startToAmuseBaby; @end 2,管理类(使用类) ...
- 一个用得比较广的微信API的XXE外部实体注入漏洞
文件地址: https://github.com/dodgepudding/wechat-php-sdk/raw/master/wechat.class.php 代码: <?php /** * ...
- TortoiseSVN 的分支合并操作
今天对svn的分支合并有了兴趣,所以对新建了一个项目练练手. 在网上找了一篇文章做指导: http://www.open-open.com/lib/view/open1346982569725.htm ...
- Node.js回调概念
什么是回调? 回调是一个异步等效的功能.在完成特定任务回调函数被调用. Node大量使用了回调.Node的所有的API都支持回调这样的一种方式. 例如,一个函数读取一个文件可能开始读取文件,并使得下一 ...
- BZOJ 1020 安全的航线flight
Description 在设计航线的时候,安全是一个很重要的问题.首先,最重要的是应采取一切措施确保飞行不会发生任何事故,但同时也需要做好最坏的打算,一旦事故发生,就要确保乘客有尽量高的生还几率.当飞 ...
- SHELL要发送HTML这类邮件的话,还得靠msmtp 和 mutt
参考蛮多的.. http://storysky.blog.51cto.com/628458/293005 http://www.wilf.cn/post/centos-mutt-msmtp-setup ...
- 优秀的开发者 vs. 差的开发者
优秀的开发者是一个艺术家,一个享受创作过程的工匠.差的开发者只将自己当作负责产生代码的码农. 优秀的开发者了解客户的问题.差的开发者只了解手头的技术问题.优秀的开发者会不断努力去理解"为什么 ...
- 使用 Spring Data JPA 简化 JPA 开发
从一个简单的 JPA 示例开始 本文主要讲述 Spring Data JPA,但是为了不至于给 JPA 和 Spring 的初学者造成较大的学习曲线,我们首先从 JPA 开始,简单介绍一个 JPA 示 ...