[PWA] 19. Cache the avatars
Cache the avatars is little different from cache photos. We need to serve the page with our cache data and also go to the network for fetch avatars in case some user like change their avatars frequently.
self.addEventListener('fetch', function(event) {
var requestUrl = new URL(event.request.url);
if (requestUrl.origin === location.origin) {
if (requestUrl.pathname === '/') {
event.respondWith(caches.match('/skeleton'));
return;
}
if (requestUrl.pathname.startsWith('/photos/')) {
event.respondWith(servePhoto(event.request));
return;
}
// TODO: respond to avatar urls by responding with
// the return value of serveAvatar(event.request)
if(requestUrl.pathname.startsWith('/avatars/')){
event.respondWith(serveAvatar(event.request));
return;
}
}
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
});
function serveAvatar(request) {
// Avatar urls look like:
// avatars/sam-2x.jpg
// But storageUrl has the -2x.jpg bit missing.
// Use this url to store & match the image in the cache.
// This means you only store one copy of each avatar.
var storageUrl = request.url.replace(/-\dx\.jpg$/, '');
// TODO: return images from the "wittr-content-imgs" cache
// if they're in there. But afterwards, go to the network
// to update the entry in the cache.
//
// Note that this is slightly different to servePhoto!
return caches.open(contentImgsCache)
.then(function(cache){
return cache.match(storageUrl).then(function(response){
var netFetchResponse = fetch(request).then(function(networkResponse){
cache.put(storageUrl ,networkResponse.clone());
return networkResponse;
});
return response || netFetchResponse;
})
})
}
[PWA] 19. Cache the avatars的更多相关文章
- [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 ...
- 【Guava】Guava Cache用法
背景 缓存的主要作用是暂时在内存中保存业务系统的数据处理结果,并且等待下次访问使用.在日长开发有很多场合,有一些数据量不是很大,不会经常改动,并且访问非常频繁.但是由于受限于硬盘IO的性能或者远程网络 ...
- Spark 算子
0.parallelize 1.map 2.mapValues 3.flatMap 4.mapPartitions 5.mapPartitionsWithIndex 6.filter 7.reduce ...
- ECMAScript 6 学习(二)async函数
1.什么是async函数 2.用法 2.1基本用法 3.语法 3.1返回promise对象 3.2promise状态的变化 3.3await命令 1.什么是async函数 async函数也是异步编程 ...
- ES2017中的async函数
前面的话 ES2017标准引入了 async 函数,使得异步操作变得更加方便.本文将详细介绍async函数 概述 async 函数是 Generator 函数的语法糖 使用Generator 函数,依 ...
- 04_Linux目录文件操作命令1(mv ls cd...)_我的Linux之路
上一节已经给大家讲了Linux的目录结构,相信大家已经对Linux的整个目录结构有所了解 现实中,服务器(包含Linux,Unix,windows server)一般都摆放在机房里,因为一个机房摆放了 ...
- JavaScript中的Generator函数
1. 简介 Generator函数时ES6提供的一种异步编程解决方案.Generator语法行为和普通函数完全不同,我们可以把Generator理解为一个包含了多个内部状态的状态机. 执行Genera ...
- Ehcache
前言:设计一套缓存框架需要关注的要素 本文来源:RayChase 的<设计一套缓存框架需要关注的要素> 最近关注了一些缓存框架的特性和实现,包括OSCache.JCS.Ehcache.M ...
- Linux常用命令详解-目录文件操作命令
来源:https://www.linuxidc.com/Linux/2018-04/151801.htm 现实中,服务器(包含Linux,Unix,Windows Server)一般都摆放在机房里,因 ...
随机推荐
- Linux平台上搭建apache+tomcat负载均衡集群
传统的Java Web项目是通过tomcat来运行和发布的.但在实际的企业应用环境中,采用单一的tomcat来维持项目的运行是不现实的.tomcat 处理能力低,效率低,承受并发小(1000左右).当 ...
- PHP代码分离
所谓的代码分离 其实只是一种思路,既然是一种思路 那就意味着他是有需求的 没有需求就没有解决方案 没有方案就不存在思路. 在这之前,我们制作 PHP 程序页面的时候.都是 HTML 和 PHP 混合写 ...
- 《C和指针》章节后编程练习解答参考——第8章
8.1 #include <stdio.h> int main (void) { int a, b, c, d; // 不使用嵌套花括号初始化 unsigned ][][][] = { , ...
- Objective-C property属性解析
@interface … @property (原子性,可写性,内存管理) id name; @end 原子性: nonatomic, atomic 默认atomic 可写性: rea ...
- spoj gss2 : Can you answer these queries II 离线&&线段树
1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...
- Keil C51调试程序时, 对ROM的查看以及RAM 查看或修改
在Keil 里使用 DeBug 模式时,如要 查看外部 RAM 的数据 或查看 ACC 的内容可以进行以下操作; sysGetTxMode: LCALL Com0185(C:2B95) ,sysGet ...
- Delphi与Java中的日期互换
在最近做的一个项目中用到了Java和Delphi,发现它们不能正确读取对方的日期类型,如在Java中写入一个值为“2007-12-1”的日期值,通过Delphi读取却不是这个值了.通过查阅资料,发现两 ...
- Android中关于List与Json转化问题
比如 List<String>list=new ArrayList<String>(); list.add("test1"); list.add(" ...
- Haskell函数的语法
本章讲的就是 Haskell 那套独特的语法结构,先从模式匹配开始.模式匹配通过检查数据的特定结构来检查其是否匹配,并按模式从中取得数据. 在定义函数时,你可以为不同的模式分别定义函数本身,这就让代码 ...
- 【HDOJ】1501 Zipper
DFS.注意剪枝,0ms. #include <stdio.h> #include <string.h> #define False 0 #define True 1 #def ...