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相关代码的更多相关文章

  1. [ARM] Cortex-M Startup.s启动文件相关代码解释

    1. 定义一个段名为CSTACK, 这里: NOROOT表示如何定义的段没有被关联,那么同意会被优化掉,如果不想被优化掉就使用ROOT. 后面的括号里数字表示如下: (1):这个段是2的1次方即2字节 ...

  2. Kafka Producer相关代码分析【转】

    来源:https://www.zybuluo.com/jewes/note/63925 @jewes 2015-01-17 20:36 字数 1967 阅读 1093 Kafka Producer相关 ...

  3. 命令行方式使用abator.jar生成ibatis相关代码和sql语句xml文件

    最近接手一个老项目,使用的是数据库是sql server 2008,框架是springmvc + spring + ibatis,老项目是使用abator插件生成的相关代码,现在需要增加新功能,要添加 ...

  4. myBatis自动生成相关代码文件配置(Maven)

    pom.xml文件添加配置 <build> <finalName>generator</finalName> <plugins> <!-- mav ...

  5. 临时2级页表的初始化过程 head_32.S 相关代码解释

    page_pde_offset = (__PAGE_OFFSET >> 20); /* __PAGE_OFFSET是0xc0000000,page_pde_offset = 3072 = ...

  6. 使用Mybatis Generator自动生成Mybatis相关代码

    本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码: 一.构建一个环境: 1. 首先创建一个表: CREATE TABLE pet (name VARCHAR(2 ...

  7. K:树、二叉树与森林之间的转换及其相关代码实现

    相关介绍:  二叉树是树的一种特殊形态,在二叉树中一个节点至多有左.右两个子节点,而在树中一个节点可以包含任意数目的子节点,对于森林,其是多棵树所组成的一个整体,树与树之间彼此相互独立,互不干扰,但其 ...

  8. js 横屏 竖屏 相关代码 与知识点

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  9. 转:关于Latent Dirichlet Allocation及Hierarchical LDA模型的必读文章和相关代码

    关于Latent Dirichlet Allocation及Hierarchical LDA模型的必读文章和相关代码 转: http://andyliuxs.iteye.com/blog/105174 ...

随机推荐

  1. 【机器学习】attention机制

    参考: 1.https://lilianweng.github.io/lil-log/2018/06/24/attention-attention.html

  2. C# RESTful API

    C# RESTful API REST 全称是 Representational State Transfer,有人说它是一种风格,并非一种标准,个人觉得挺有道理.它本身并没有创造新的技术.组件与服务 ...

  3. 教你使用Webpack搭建环境 TypeScript (2)

      一. 环境搭建1.1. TypeScript环境安装已经配置好的环境,大家可以直接下载:https://github.com/coderwhy/HYLearnTS.git在上一个章节中我们说过,T ...

  4. spring添加事物

    <context:component-scan base-package="com.zlkj" > <context:include-filter type=&q ...

  5. html5 canvas 自定义画图裁剪图片

    html5 给我们带来了极大惊喜的canvas标签,有了它我们可以在浏览器客户端处理图片,不需要经过服务器周转.可以实现: 1.照片本地处理,ps有的一些基本功能都有 2.结合js可以实现一些很炫的动 ...

  6. 《Mysql - Order By 的工作原理?》

    一:概述 - order by 用于 SQL 语句中的排序. - 以  select city,name,age from t where city='杭州' order by name limit ...

  7. http拦截器-HandlerInterceptor

    简介 拦截器我想大家都并不陌生,最常用的登录拦截.权限校验.防重复提交.记录日志等等,总之可以去做很多的事情. 自定义拦截器HandlerInterceptorAdapter 我们以记录日志为例,介绍 ...

  8. python 之 logger日志 字典配置文件

    import os import logging.config #不能只导入logging BASE_DIR=os.path.dirname(os.path.dirname(__file__)) # ...

  9. Python进阶:对象复制与比较,分深浅,见真假

    "==" 与 is python 为 10 开辟内存空间, a与b同时指向这块内存,即a与b的值相等,a与b的id也相等.因此 a==b 与 a is b 都返回True: a = ...

  10. ALV报表——抓取工单长文

    ABAP抓取工单长文 运行效果: 代码: *&---------------------------------------------------------------------* *& ...