mirror of
https://github.com/peter-tanner/peter-tanner.github.io.git
synced 2024-11-30 20:10:18 +08:00
83 lines
1.8 KiB
JavaScript
83 lines
1.8 KiB
JavaScript
import { baseurl } from '../../_config.yml';
|
|
|
|
importScripts(`${baseurl}/assets/js/data/swconf.js`);
|
|
|
|
const purge = swconf.purge;
|
|
|
|
function verifyUrl(url) {
|
|
const requestPath = new URL(url).pathname;
|
|
|
|
for (const path of swconf.denyPaths) {
|
|
if (requestPath.startsWith(path)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
self.addEventListener('install', (event) => {
|
|
if (purge) {
|
|
return;
|
|
}
|
|
|
|
event.waitUntil(
|
|
caches.open(swconf.cacheName).then((cache) => {
|
|
return cache.addAll(swconf.resources);
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((keyList) => {
|
|
return Promise.all(
|
|
keyList.map((key) => {
|
|
if (purge) {
|
|
return caches.delete(key);
|
|
} else {
|
|
if (key !== swconf.cacheName) {
|
|
return caches.delete(key);
|
|
}
|
|
}
|
|
})
|
|
);
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('message', (event) => {
|
|
if (event.data === 'SKIP_WAITING') {
|
|
self.skipWaiting();
|
|
}
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
if (event.request.headers.has('range')) {
|
|
return;
|
|
}
|
|
|
|
event.respondWith(
|
|
caches.match(event.request).then((response) => {
|
|
if (response) {
|
|
return response;
|
|
}
|
|
|
|
return fetch(event.request).then((response) => {
|
|
const url = event.request.url;
|
|
|
|
if (purge || event.request.method !== 'GET' || !verifyUrl(url)) {
|
|
return response;
|
|
}
|
|
|
|
// See: <https://developers.google.com/web/fundamentals/primers/service-workers#cache_and_return_requests>
|
|
let responseToCache = response.clone();
|
|
|
|
caches.open(swconf.cacheName).then((cache) => {
|
|
cache.put(event.request, responseToCache);
|
|
});
|
|
return response;
|
|
});
|
|
})
|
|
);
|
|
});
|