web/sw.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-05-09 19:28:18 +03:00
---
layout: compress
# Chirpy v2.2
# https://github.com/cotes2020/jekyll-theme-chirpy
# © 2020 Cotes Chung
# MIT Licensed
---
self.importScripts('{{ "/assets/data/cache-list.js" | relative_url }}');
var cacheName = 'chirpy-{{ "now" | date: "%Y%m%d.%H%M" }}';
self.addEventListener('install', (e) => {
self.skipWaiting();
e.waitUntil(
caches.open(cacheName).then((cache) => {
return cache.addAll(cacheList);
})
);
});
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) => {
/* 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);
}
}));
})
);
});