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的更多相关文章

  1. [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 ...

  2. [PWA] 19. Cache the avatars

    Cache the avatars is little different from cache photos. We need to serve the page with our cache da ...

  3. 【Guava】Guava Cache用法

    背景 缓存的主要作用是暂时在内存中保存业务系统的数据处理结果,并且等待下次访问使用.在日长开发有很多场合,有一些数据量不是很大,不会经常改动,并且访问非常频繁.但是由于受限于硬盘IO的性能或者远程网络 ...

  4. Highcharts指南

    摘要 Highcharts图表控件是目前使用最为广泛的图表控件.本文将从零开始逐步为你介绍Highcharts图表控件.通过本文,你将学会如何配置Highcharts以及动态生成Highchart图表 ...

  5. Flash 二进制传图片到后台Java服务器接收

    需求:把客户端处理过的图片返还给服务器Flash端代码 01 package {02     import com.adobe.images.JPGEncoder;    03     import  ...

  6. 数据交互 ajax 初始化省

    1 //初始化省 2 function initProvince() { 3 if( areaLvel == 0 ) { 4 return; 5 } 6 // 清空option 7 $("# ...

  7. 转:Highcharts图表控件的使用

    摘要 Highcharts图表控件是目前使用最为广泛的图表控件.本文将从零开始逐步为你介绍Highcharts图表控件.通过本文,你将学会如何配置Highcharts以及动态生成Highchart图表 ...

  8. linux包之dmidecode

    http://www.dmtf.org/standards/smbios Dmidecode 这款软件允许你在 Linux 系统下获取有关硬件方面的信息.Dmidecode 遵循 SMBIOS/DMI ...

  9. JBoss 系列二十一:JBossCache核心API

    内容简介 本处介绍JBossCache相关的主要API,我们目的通过本部分描述,读者可以使用JBossCache API,在自己的应用中使用JBossCache. Cache接口 Cache 接口是和 ...

随机推荐

  1. JeeSite试用

    JeeSite主要定位于企业信息化领域.网址:http://www.oschina.net/p/jeesite 从描述来看,各种NB,下来看的最主要原因是最近还在更新,觉得有问题可以有一批人一起研究研 ...

  2. Extjs4 关于设置form中所有子控件为readOnly属性的解决方案

    之前在网上找了一堆,但那些确实没法用,后来考虑了一下,发现主要是网上提供的假设form中只有一层控件,没有考虑到布局稍微复杂的form情形,此处采用递归的形式实现对form中所有控件(grid及but ...

  3. 谷歌浏览器chrome假死、卡死、经常无反应,火狐firefox闪黑格子的解决办法(显卡/驱动兼容问题)

        问题: chrome 升级到高版本,切换标签后点击,滚轮都没反应,假死不动.F12呼出控制台来开发时更让人揪心.(大概chrome 25更高) 原因: 我的电脑是:集显+512M独显,可切换的 ...

  4. js中的潜伏者之Arguments对象

    argument 说明: 在JavaScript中,arguments是对象的一个特殊属性.arguments对象就像数组,但是它却不是数组.可以理解为他是潜伏者,通俗的说,就是你传的参数不一定按照参 ...

  5. 使用jQuery动态加载js脚本文件的方法

    动态加载Javascript是一项非常强大且有用的技术.这方面的主题在网上已经讨论了不少,我也经常会在一些个人项目上使用RequireJS和Dojo加载js 它们很强大,但有时候也会得不偿失.如果你使 ...

  6. jQuery简单的轮播特效

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. [前端笔记]第三篇:JavaScript

    JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一.代码存放位置 J ...

  8. unity3d中脚本生命周期(MonoBehaviour lifecycle)

    最近在做一个小示例,发现类继承于MonoBehaviour的类,有很多个方法,于是乎必然要问出一个问题:这么多个方法,执行先后顺序是如何的呢?内部是如何进行管理的呢?于是在网上找了许多资料,发现了Ri ...

  9. jQuery Asynchronous

    http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html http:// ...

  10. TControl.WMLButtonUp的inherited的作用——是为了给子类控件新的处理消息的机会

    意外注意到这个小细节: procedure TControl.WMLButtonUp(var Message: TWMLButtonUp); begin inherited; // 注意,如果是直接点 ...