|
|
importScripts('workbox-sw.prod.v2.1.2.js');
|
|
|
|
|
|
/**
|
|
|
* DO NOT EDIT THE FILE MANIFEST ENTRY
|
|
|
*
|
|
|
* The method precache() does the following:
|
|
|
* 1. Cache URLs in the manifest to a local cache.
|
|
|
* 2. When a network request is made for any of these URLs the response
|
|
|
* will ALWAYS comes from the cache, NEVER the network.
|
|
|
* 3. When the service worker changes ONLY assets with a revision change are
|
|
|
* updated, old cache entries are left as is.
|
|
|
*
|
|
|
* By changing the file manifest manually, your users may end up not receiving
|
|
|
* new versions of files because the revision hasn't changed.
|
|
|
*
|
|
|
* Please use workbox-build or some other tool / approach to generate the file
|
|
|
* manifest which accounts for changes to local files and update the revision
|
|
|
* accordingly.
|
|
|
*/
|
|
|
const fileManifest = [
|
|
|
const workboxSW = new self.WorkboxSW({
|
|
|
clientsClaim: true,
|
|
|
skipWaiting: true
|
|
|
});
|
|
|
|
|
|
// self.addEventListener('install', function(event) {
|
|
|
// event.waitUntil(
|
|
|
// caches.open('my-test-cache-v1').then(function(cache) {
|
|
|
// return cache.addAll([
|
|
|
// '/'
|
|
|
// ]);
|
|
|
// })
|
|
|
// );
|
|
|
// });
|
|
|
|
|
|
self.addEventListener('error', event => {
|
|
|
// 上报错误信息
|
|
|
// 常用的属性:
|
|
|
// event.message
|
|
|
// event.filename
|
|
|
// event.lineno
|
|
|
// event.colno
|
|
|
// event.error.stack
|
|
|
})
|
|
|
self.addEventListener('unhandledrejection', event => {
|
|
|
// 上报错误信息
|
|
|
// 常用的属性:
|
|
|
// event.reason
|
|
|
})
|
|
|
|
|
|
self.addEventListener('fetch', function (event) {
|
|
|
console.log(event.request);
|
|
|
event.respondWith(
|
|
|
caches.match(event.request).then(function (response) {
|
|
|
// 来来来,代理可以搞一些代理的事情
|
|
|
|
|
|
// 如果 Service Worker 有自己的返回,就直接返回,减少一次 http 请求
|
|
|
if (response) {
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
// 如果 service worker 没有返回,那就得直接请求真实远程服务
|
|
|
let request = event.request.clone(); // 把原始请求拷过来
|
|
|
|
|
|
return fetch(request).then(function (httpRes) {
|
|
|
|
|
|
// http请求的返回已被抓到,可以处置了。
|
|
|
|
|
|
// 请求失败了,直接返回失败的结果就好了。。
|
|
|
if (!httpRes || httpRes.status !== 200) {
|
|
|
return httpRes;
|
|
|
}
|
|
|
|
|
|
// 请求成功的话,将请求缓存起来。
|
|
|
let responseClone = httpRes.clone();
|
|
|
|
|
|
console.log(httpRes)
|
|
|
caches.open('custom-cache').then(function (cache) {
|
|
|
cache.put(event.request, responseClone);
|
|
|
});
|
|
|
|
|
|
return httpRes;
|
|
|
}).catch(console.error);
|
|
|
})
|
|
|
);
|
|
|
});
|
|
|
|
|
|
workboxSW.precache([
|
|
|
{
|
|
|
"url": "sw.js",
|
|
|
"revision": "3764cbf82eff6f9c492a134d2059ff8f"
|
|
|
"url": "sw.js"
|
|
|
},
|
|
|
{
|
|
|
"url": "workbox-sw.prod.v2.1.2.js",
|
|
|
"revision": "685d1ceb6b9a9f94aacf71d6aeef8b51"
|
|
|
"url": "workbox-sw.prod.v2.1.2.js"
|
|
|
}
|
|
|
];
|
|
|
|
|
|
const workboxSW = new self.WorkboxSW({
|
|
|
"skipWaiting": true,
|
|
|
"clientsClaim": true
|
|
|
});
|
|
|
workboxSW.precache(fileManifest); |
|
|
]); |
...
|
...
|
|