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 ...
随机推荐
- samtools获取uniq reads
参考地址: https://www.biostars.org/p/56246/ -q INT only include reads with mapping quality >= INT [0] ...
- QT 读写.ini配置文件
当需要存放的数据量较少时合适使用.ini配置文件. #include <QCoreApplication> #include <QSettings> void SystemSe ...
- 小程序的目录结构/配置介绍/视图层wxml数据绑定/双线程模型/小程序的启动流程
安装好微信小程序开发软件,创建项目 小程序文件结构和传统web对比 结构 传统web 微信小程序 结构 HTML WXML 样式 CSS WXSS 逻辑 Javascript Javascript 配 ...
- CH02基于ZYNQ的嵌入式LINUX移植
CH02基于ZYNQ的嵌入式LINUX移植 1.1概述 实验环境: Windows 10 专业版 Vmware workstation 14.1.1 Ubuntu 16.04.3 Xilinx SDx ...
- Educational Codeforces Round 65 (Div. 2)
A.前n-10个有8即合法. #include<cstdio> #include<cstring> #include<iostream> #include<a ...
- MyBatis Generator 自动生成的POJO对象的使用(一)
MyBatis Generator 会自动生成以下几种类型的对象(除非你使用MyBatis3DynamicSql 的运行环境): Java Model Objects(总是生成) SQL Map Fi ...
- IEEE754浮点数
前言 Go语言之父Rob Pike大神曾吐槽:不能掌握正则表达式或浮点数就不配当码农! You should not be permitted to write production code if ...
- js基本用法
1. 在HTML里面加入JavaScript 方法非常简单,就是通过一对<script></script>标签,然后在标签里面书写代码即可 2. 标签位置 按照以前传统的方法, ...
- Java Web 深入分析(10) Spring 实践
Spring helloworld [http://wiki.jikexueyuan.com/project/spring/hello-world-example.html] HelloWorld.j ...
- go的安装及环境变量设置
1,go安装 https://studygolang.com/dl 官网下载,找自己需要的版本,傻瓜式安装 2.go的环境变量设置 windows下面要设置root和path root代表go安装路径 ...