PWA相关代码
sw.js 文件
let CacheName = 'plus-v1';
var filesToCache = [
]; self.addEventListener('install', function (e) {
console.log('[ServiceWorker] Install');
e.waitUntil(
caches.open(CacheName).then(function (cache) {
console.log('[ServiceWorker] Caching app shell');
return cache
.addAll(filesToCache)
.then(function () {
return self.skipWaiting();
})
.catch(function (error) {
console.log('Failed to cache:', error);
});
})
);
}); self.addEventListener('activate', function (e) {
console.log('[ServiceWorker] Activate');
e.waitUntil(
caches
.keys()
.then(function (keyList) {
return Promise.all(
keyList.map(function (key) {
console.log(`key-----------${key}`);
if (key !== CacheName) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
})
);
})
.then(function () {
return self.clients.claim();
})
);
}); self.addEventListener('fetch', function (event) {
console.log('[Service Worker] Fetch', event.request.url);
event.respondWith(
caches.match(event.request).then(function (response) {
// 如果 Service Worker有自己的返回,就直接返回,减少一次 http 请求
if (response) {
return response;
}
// 如果 service worker 没有返回,那就得直接请求真实远程服务
var requestToCache = event.request.clone(); // 把原始请求拷过来
return fetch(requestToCache).then(function (httpRes) {
// http请求的返回已被抓到,可以处置了。 // 请求失败了,直接返回失败的结果就好了。
if (!httpRes || httpRes.status !== 200) {
return httpRes;
} // 请求成功的话,将请求缓存起来。
var responseToCache = httpRes.clone();
// 选择性缓存数据
if (
/\.js$|\.css$|\.jpg$|\.webp$|\.svg$|\.png$/.test(requestToCache.url) &&
!/sw/.test(requestToCache.url)
) {
caches.open(CacheName).then(function (cache) {
cache.put(requestToCache, responseToCache);
});
}
return httpRes;
});
})
);
});
PWA相关代码的更多相关文章
- [ARM] Cortex-M Startup.s启动文件相关代码解释
1. 定义一个段名为CSTACK, 这里: NOROOT表示如何定义的段没有被关联,那么同意会被优化掉,如果不想被优化掉就使用ROOT. 后面的括号里数字表示如下: (1):这个段是2的1次方即2字节 ...
- Kafka Producer相关代码分析【转】
来源:https://www.zybuluo.com/jewes/note/63925 @jewes 2015-01-17 20:36 字数 1967 阅读 1093 Kafka Producer相关 ...
- 命令行方式使用abator.jar生成ibatis相关代码和sql语句xml文件
最近接手一个老项目,使用的是数据库是sql server 2008,框架是springmvc + spring + ibatis,老项目是使用abator插件生成的相关代码,现在需要增加新功能,要添加 ...
- myBatis自动生成相关代码文件配置(Maven)
pom.xml文件添加配置 <build> <finalName>generator</finalName> <plugins> <!-- mav ...
- 临时2级页表的初始化过程 head_32.S 相关代码解释
page_pde_offset = (__PAGE_OFFSET >> 20); /* __PAGE_OFFSET是0xc0000000,page_pde_offset = 3072 = ...
- 使用Mybatis Generator自动生成Mybatis相关代码
本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码: 一.构建一个环境: 1. 首先创建一个表: CREATE TABLE pet (name VARCHAR(2 ...
- K:树、二叉树与森林之间的转换及其相关代码实现
相关介绍: 二叉树是树的一种特殊形态,在二叉树中一个节点至多有左.右两个子节点,而在树中一个节点可以包含任意数目的子节点,对于森林,其是多棵树所组成的一个整体,树与树之间彼此相互独立,互不干扰,但其 ...
- js 横屏 竖屏 相关代码 与知识点
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- 转:关于Latent Dirichlet Allocation及Hierarchical LDA模型的必读文章和相关代码
关于Latent Dirichlet Allocation及Hierarchical LDA模型的必读文章和相关代码 转: http://andyliuxs.iteye.com/blog/105174 ...
随机推荐
- Linux 指令表
Linux简介(了解) Linux介绍 Linux是类UNIX计算机的统称 Linux操作系统的内核也是Linux Linux是由芬兰大学生Linux Torvalds于1991年编写的 Linux这 ...
- idea安装阿里云插件和sonar插件
重启idea
- pandas对时间列分组求diff遇到的问题
例子: df = pd.DataFrame() df['A'] = [1, 1, 2] df['B'] = [datetime.date(2018, 1, 2), datetime.date(2018 ...
- Linux下操作数据库
一.安装数据库 1.卸载旧版本 检查是否安装有MySQL Server:rpm -qa | grep mysql rpm -e mysql_libs //普通删除模式 rpm -e --nod ...
- 通用mybatis单表操作接口
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
- Java 中的并发工具类
Java 中的并发工具类 CountDownLatch public class JoinCountDownLatchTest { public static void main(String[] a ...
- Nginx启动和停止
启动nginx [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx ...
- 自然语言处理(简称NLP)
自然语言处理(简称NLP),是研究计算机处理人类语言的一门技术,包括: 1.句法语义分析:对于给定的句子,进行分词.词性标记.命名实体识别和链接.句法分析.语义角色识别和多义词消歧. 2.信息抽取:从 ...
- Echarts dataZoom 区域缩放
dataZoom=[ //区域缩放 { id: 'dataZoomX', show:true, //是否显示 组件.如果设置为 false,不会显示,但是数据过滤的功能还存在. backgroundC ...
- [转帖]MySQL语句大全
MySQL语句大全 https://www.cnblogs.com/jicki/p/5548676.html 一.连接mysql. 格式: mysql -h主机地址 -u用户名 -p用户密码 二.修改 ...