[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 text already, if there in the wittr there is photo, we will create cache for it, so next time we can fetch from cache.
For the incoming photo, we also need to create cache for them.
For the 'install' event, we only cache assets, static imgs, css and js.
var staticCacheName = 'wittr-static-v7';
var contentImgsCache = 'wittr-content-imgs';
var allCaches = [
staticCacheName,
contentImgsCache
]; self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(staticCacheName).then(function(cache) {
return cache.addAll([
'/skeleton',
'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'
]);
})
);
});
Here we don't cache dynamic photos. But at the beginning we define the cache name for photo .
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
return cacheName.startsWith('wittr-') &&
!allCaches.includes(cacheName);
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
'activate' event is the place to clean the old version cahce but keep the current version cache and photo cache.
In 'fetch' event, this is the place we want to cache the photos.
For each request, we want to check,
- whether we have the cache for the photo request?
- if not, fetch from network and cache it.
self.addEventListener('fetch', function(event) {
var requestUrl = new URL(event.request.url);
// make sure the same origin
if (requestUrl.origin === location.origin) {
// serve cache with the skeleton
if (requestUrl.pathname === '/') {
event.respondWith(caches.match('/skeleton'));
return;
}
// cache the photo
if (requestUrl.pathname.startsWith('/photos/')) {
event.respondWith(servePhoto(event.request));
return;
}
}
// cache the assets
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
});
The servePhoto():
we want to make sure two things:
- we don't care the photo size, 800px,200px or 40px
- because respond object can be only access once, so we need clone() the original one and use clone one for the cahce, return the original one to the browser.
function servePhoto(request) {
var storageUrl = request.url.replace(/-\d+px\.jpg$/, '');
return caches.open(contentImgsCache).then(function(cache) {
return cache.match(storageUrl).then(function(response) {
if (response) return response;
return fetch(request).then(function(networkResponse) {
cache.put(storageUrl, networkResponse.clone());
return networkResponse;
});
});
});
}
So first, remove those px info: (/photos/awefaef-af23-fwq23f-800px.jpg) --> (/photos/awefaef-af23-fwq23f)
var storageUrl = request.url.replace(/-\d+px\.jpg$/, '');
Second, clone the network response and return origial one to browser and clone one to cache
return fetch(request).then(function(networkResponse) {
cache.put(storageUrl, networkResponse.clone());
return networkResponse;
});
Unitl now, we are able to cache the photo, event in the offline mode, we are still able to see the photos return from the cache.
[PWA] 17. Cache the photo的更多相关文章
- [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] 19. Cache the avatars
Cache the avatars is little different from cache photos. We need to serve the page with our cache da ...
- 【Guava】Guava Cache用法
背景 缓存的主要作用是暂时在内存中保存业务系统的数据处理结果,并且等待下次访问使用.在日长开发有很多场合,有一些数据量不是很大,不会经常改动,并且访问非常频繁.但是由于受限于硬盘IO的性能或者远程网络 ...
- Highcharts指南
摘要 Highcharts图表控件是目前使用最为广泛的图表控件.本文将从零开始逐步为你介绍Highcharts图表控件.通过本文,你将学会如何配置Highcharts以及动态生成Highchart图表 ...
- Flash 二进制传图片到后台Java服务器接收
需求:把客户端处理过的图片返还给服务器Flash端代码 01 package {02 import com.adobe.images.JPGEncoder; 03 import ...
- 数据交互 ajax 初始化省
1 //初始化省 2 function initProvince() { 3 if( areaLvel == 0 ) { 4 return; 5 } 6 // 清空option 7 $("# ...
- 转:Highcharts图表控件的使用
摘要 Highcharts图表控件是目前使用最为广泛的图表控件.本文将从零开始逐步为你介绍Highcharts图表控件.通过本文,你将学会如何配置Highcharts以及动态生成Highchart图表 ...
- linux包之dmidecode
http://www.dmtf.org/standards/smbios Dmidecode 这款软件允许你在 Linux 系统下获取有关硬件方面的信息.Dmidecode 遵循 SMBIOS/DMI ...
- JBoss 系列二十一:JBossCache核心API
内容简介 本处介绍JBossCache相关的主要API,我们目的通过本部分描述,读者可以使用JBossCache API,在自己的应用中使用JBossCache. Cache接口 Cache 接口是和 ...
随机推荐
- centos7上安装与配置Tomcat7(整理篇)
1.检查tomcat7是否已经安装 rpm -qa | grep tomcat ps -ef | grep tomcat 第一条命令查看是用rpm安装过tomcat,由于我们倾向于安装解压版的tomc ...
- Linux 系统命令及其使用详解(大全)
(来源: 中国系统分析员) cat cd chmod chown cp cut 1.名称:cat 使用权限:所有使用者 使用方式:cat [-AbeEnstTuv] [--help] [--versi ...
- Transpose File
Given a text file file.txt, transpose its content. You may assume that each row has the same number ...
- MVC Unit Testing学习笔记
MVC Unit Testing 参考文档: 1.http://www.asp.net/mvc/overview/testing 2.http://www.asp.net/mvc/tutorials/ ...
- scons小结
scons是用python写的,据说是比make要方便很多,其实我都没写过makeFile...... 1.安装 方式1:下载安装包安装,需要用python setup.py install去编译 方 ...
- 数组去重的三种方法及from方法
直接上代码: var str="adbbckddwerivka"; var arr=str.split(""); console.log(arr); //ind ...
- Java线程运行轨迹-代码追踪与定位
今天在写程序时,想到一个问题,当我的程序出异常的时候,控制台输出信息中,明确指出了出现异常的位置,并详细列举了函数的调用层次关系,它是怎么做到的. 竟然想到了这个问题,就去查看了源代码,不过没点几下, ...
- MySql数据库3【优化4】连接设置的优化
1.wait_timeout / interactive_timeout 连接超时 服务器关闭连接之前等待活动的秒数.MySQL所支持的最大连接数是有限的,因为每个连接的建立都会消耗内存,因此我们希 ...
- QQ空间API接口
(以下内容可能会随着时间改变而改变!) 查看对方QQ空间的背景音乐 http://qzone-music.qq.com/fcg-bin/cgi_playlist_xml.fcg?json=0& ...
- openshif ssh proxy
最近google又被墙了.没办法 1:注册一个openshift账号.申请注册一个app,获取一个免费主机. https://www.openshift.com/ 2:去PuTTY官方网站下载pL ...