[PWA] 7. First Cache when installed
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的更多相关文章
- 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 ...
- linux apt-cache使用方法
apt-cache是linux下的一个apt软件包管理工具,它可查询apt的二进制软件包缓存文件.APT包管理的大多数信息查询功能都可以由apt-cache命令实现,通过apt-cache命令配合不同 ...
- 三本毕业(非科班),四次阿里巴巴面试,终拿 offer(大厂面经)
作者:gauseen 原文:https://github.com/gauseen/blog 公众号:「学前端」,只搞技术不搞广告文,欢迎关注~ 第一次 20:00 电话一面 - 自我介绍 - 对公司工 ...
- [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 ...
- [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 ...
- [PWA] 19. Cache the avatars
Cache the avatars is little different from cache photos. We need to serve the page with our cache da ...
- [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] 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 ...
- [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 ...
随机推荐
- html 中 #include file 的用法
有两个文件a.htm和b.htm,在同一目录下a.htm内容如下 <!-- #include file="b.htm" --> b.htm内容如下 今天:雨 31 ℃- ...
- ajax验证用户名和找回密码参考
// JavaScript Document function chkname(form){ var user = form.user.value; if(user == ''){ alert('请输 ...
- 那些年被我坑过的Python——一夫当关 第十三章(堡垒机初步设计)
堡垒机架构 堡垒机的主要作用权限控制和用户行为审计,堡垒机就像一个城堡的大门,城堡里的所有建筑就是你不同的业务系统 , 每个想进入城堡的人都必须经过城堡大门并经过大门守卫的授权,每个进入城堡的人必 ...
- 测试gcc的优化选项
一.测试准备及原理 测试代码: static void wait(volatile unsigned long dly) { ; dly--); } int main(void) { unsigned ...
- Java 内部类种类及使用解析
package com.learnjava.innerclass; class MemberInner { private int d = 1; private int a = 2; // 定义一个成 ...
- Java中的那些名词术语(不断更新中)
在工作和学习中,总会遇到各种术语.岁月不饶人,记忆力越来越下降.在这里记录下那些曾经关注过学习过的东西. POJO: Plain Old Java Object DI: Dependency Inje ...
- python 性能鸡汤
转载自:http://www.oschina.net/question/1579_45822 1:使用内建函数input() int() isinstance() issubclass() iter( ...
- 使用CSS3改变选中元素背景色
CSS3代码如下: /* SELECTION ----------------- */ ::-moz-selection { background: #f00533; color: white; te ...
- [BZOJ 1257] [CQOI2007] 余数之和sum 【数学】
题目链接:BZOJ - 1257 题目分析 首先, a % b = a - (a/b) * b,那么答案就是 sigma(k % i) = n * k - sigma(k / i) * i ( ...
- 蓝牙4.0BLE cc2540 usb-dongle的 SmartRF Packet Sniffer 抓取数据方法
蓝牙4.0的开发, 现在真热火的很, 但是很多朋友买了我们出品的cc2540 usb-dongle后, 都反馈说不知道如何抓包, 并且, 即使很多朋友到TI官网论坛去找信息,不少朋友依然是无功而返,实 ...