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. Fedora上配置一个安全FTP

    现在流行的FTP服务器,比较著名的有WU-FTP(Washington University FTP)和VSFTP(Very Secure FTP 非常安全的FTP)以及Proftp,pureftp等 ...

  2. yii2源码学习笔记(四)

    继续了解组件Component.php /** * Returns a value indicating whether a property is defined for this componen ...

  3. Ubuntu14.04 安装QQ(国际版)

    1.在/etc/apt/source.list文件中添加: deb http://packages.linuxdeepin.com/deepin trusty main non-free univer ...

  4. operation 多线程

    2.Cocoa Operation 优点:不需要关心线程管理,数据同步的事情.Cocoa Operation 相关的类是 NSOperation ,NSOperationQueue.NSOperati ...

  5. Windows的免費hMailServer搭配SpamAssassin過濾垃圾郵件:安裝與設定

    http://jdev.tw/blog/1677/hmailserver-with-spamassassin-sawin32 為了解決垃圾信泛濫的狀況,這兩天「跨界」測試了運行在Windows上的免費 ...

  6. My ECMAScript 7 wishlist

    With ECMAScript 6 now feature complete, any further changes to the core of JavaScript will happen in ...

  7. WebKit JavaScript Binding添加新DOM对象的三种方式

    一.基础知识 首先WebKit IDL并非完全遵循Web IDL,只是借鉴使用.WebKit官网提供了一份说明(WebKitIDL),比如Web IDL称"operation”(操作), 而 ...

  8. SharePoint REST api

    http://msdn.microsoft.com/en-us/magazine/dn198245.aspx Understanding and Using the SharePoint 2013 R ...

  9. 关于如何在BCB中使用CodeGuard

    作者:深圳虫 来自:深圳虫网本文来自http://www.szbug.com/disparticle.aspID=4 一. 为什么写这篇东西自己在使用BCB5写一些程序时需要检查很多东西,例如内存泄漏 ...

  10. bzoj3230

    以前觉得这题好难,现在觉得这题还是挺简单首先看到类似LCP问题不难想到后缀数组吧前后的相似需要我们分别做一个后缀数组和“前缀数组”(就是把字符串反向然后跑后缀数组)这道题的难点就在于如何确定子串是什么 ...