-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
executable file
·1 lines (1 loc) · 1.1 KB
/
sw.js
1
self.importScripts('/assets/js/data/cache-list.js'); var cacheName = 'chirpy-20210227.1147'; function isExcluded(url) { const regex = /(^http(s)?|^\/)/; /* the regex for CORS url or relative url */ for (const rule of exclude) { if (!regex.test(url) || url.indexOf(rule) != -1) { return true; } } return false; } self.addEventListener('install', (e) => { self.skipWaiting(); e.waitUntil( caches.open(cacheName).then((cache) => { return cache.addAll(include); }) ); }); self.addEventListener('fetch', (e) => { e.respondWith( caches.match(e.request).then((r) => { /* console.log('[Service Worker] Fetching resource: ' + e.request.url); */ return r || fetch(e.request).then((response) => { return caches.open(cacheName).then((cache) => { if (!isExcluded(e.request.url)) { /* console.log('[Service Worker] Caching new resource: ' + e.request.url); */ cache.put(e.request, response.clone()); } return response; }); }); }) ); }); self.addEventListener('activate', (e) => { e.waitUntil( caches.keys().then((keyList) => { return Promise.all(keyList.map((key) => { if(key !== cacheName) { return caches.delete(key); } })); }) ); });