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

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

  2. linux apt-cache使用方法

    apt-cache是linux下的一个apt软件包管理工具,它可查询apt的二进制软件包缓存文件.APT包管理的大多数信息查询功能都可以由apt-cache命令实现,通过apt-cache命令配合不同 ...

  3. 三本毕业(非科班),四次阿里巴巴面试,终拿 offer(大厂面经)

    作者:gauseen 原文:https://github.com/gauseen/blog 公众号:「学前端」,只搞技术不搞广告文,欢迎关注~ 第一次 20:00 电话一面 - 自我介绍 - 对公司工 ...

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

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

  6. [PWA] 19. Cache the avatars

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

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

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

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

随机推荐

  1. MySQL 5.6 for Windows 解压缩版配置安装(转)

    转自:http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html MySQL是一个小巧玲珑但功能强大的数据库,目前十分流行.但是官网给 ...

  2. Python学习的一些好资料

    教程: 1. 廖雪峰的Python教程:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a0 ...

  3. apt-cache madison package-name

    apt-cache madison package-name 搜索软件有那些可用版本,

  4. jQuery在on绑定事件时,使用Function.prototype.bind上下文,只能用off(event)解绑函数,否则可能导致事件叠加

    因为一个bind函数,未解绑成功导致事件叠加,搞了一下午. keyup事件绑定: this.$document.on('keyup', this.keyUp.bind(this)); 原解绑函数: t ...

  5. [POJ 2774] Long Long Message 【后缀数组】

    题目链接:POJ - 2774 题目分析 题目要求求出两个字符串的最长公共子串,使用后缀数组求解会十分容易. 将两个字符串用特殊字符隔开再连接到一起,求出后缀数组. 可以看出,最长公共子串就是两个字符 ...

  6. Unity NGUI 网络斗地主 -制作图集 Atlas

    Unity NGUI 网络斗地主 -制作图集 Atlas by @杨海龙 开发环境   Win7+Unity4.2.1f4+NGUI 3.0.4版本 这一节告诉大家如何制作(图集)Atlas! 1.首 ...

  7. mysql 安装1

    Linux 安装mysql.tar.gz包(2012-09-28 19:25:06) 标签: it 分类: linux学习编 我用的mysql的版本的是:mysql--linux-i686-icc-g ...

  8. Gvim自动编译运行c++11的程序

    gcc中对c++11的支持是默认不开启的,要想在实现对其的成功编译,需要添加参数-std=c++11: g++ -o test.exe test.cpp g++ -o test.exe test.cp ...

  9. Linux共享库两种加载方式简述

      Linux共享库两种加载方式简述  动态库技术通常能减少程序的大小,节省空间,提高效率,具有很高的灵活性,对于升级软件版本也更加容易.与静态库不同,动态库里面的函数不是执行程序本身 的一部分,而是 ...

  10. VIM default configuration

    == Vim的行号.语法显示等设置(.vimrc文件的配置) ==2008年01月18日 星期五 23:01 在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有显示行号.语法高亮度显示.智能 ...