[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 ...
随机推荐
- Fedora 21 安装VirtualBox
注: 所有操作需要root权限 如果不是root用户在下面所有命令前加sudo 装dkms,kernel-devel,makecache: yum install dkms yum install ...
- 拓扑排序-DFS
拓扑排序的DFS算法 输入:一个有向图 输出:顶点的拓扑序列 具体流程: (1) 调用DFS算法计算每一个顶点v的遍历完成时间f[v] (2) 当一个顶点完成遍历时,将该顶点放到一个链表的最前面 (3 ...
- D题 - A+B for Input-Output Practice (III)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description Your ...
- GPUImage 自定义滤镜
GPUImage 自定义滤镜 GPUImage 是一个基于 GPU 图像和视频处理的开源 iOS 框架.由于使用 GPU 来处理图像和视频,所以速度非常快,它的作者 BradLarson 称在 iPh ...
- 不定参数函数原理以及实现一个属于自己的printf函数
一.不定参数函数原理 二.实现一个属于自己的printf函数 参考博文:王爽汇编语言综合研究-函数如何接收不定数量的参数
- asp.net mvc 强类型视图中传入List 数据到控制器
问题来源: 在和一位技术老师聊天时,老师问我一个mvc 表单提交的问题,问道:怎样在表单提交的时候,将 带有 List 属性的对象传入控制器? 这时,我有点呆了,以前一直都好像是 单一属性的表单提交, ...
- javabean 简介
javabean其实包含多个方面的含义. Java语言开发的可重用组件 优点:1,代码简洁.2,HTML与Java分离,好维护.3,将常用程序写成可重用组件,避免重复. 特点:1,所有类放在同 ...
- DDD的ABP开发框架
基于DDD的ABP开发框架初探 一.基本概念 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ABP是土耳其的以为架构师hikalkan开发 ...
- HDU1106
为了给学弟学妹讲课,我又水了一题-- 1: import java.util.*; 2: import java.io.*; 3: 4: public class HDU1106 5: { 6: pu ...
- 【Java】理解 UDDI 注册中心的 WSDL
如何发布和查找 WSDL 服务描述 Web 服务描述语言(WSDL)有多种用法.特别是,根据应用程序的需要,WSDL 在 UDDI 注册中心有好几种使用方法.在这第 1 篇文章中(本系列共三篇),我们 ...