You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
const CACHE_NAME = 'hub-cache-<!--@![:version]-->';
|
|
|
|
const CACHED_URLS = [
|
|
'/',
|
|
'/fonts/fa-solid-900.woff2',
|
|
'/fonts/Robotocondensed.woff2',
|
|
'/favicon.svg',
|
|
'/index.html',
|
|
'/script.js',
|
|
'/style.css',
|
|
]
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(async function () {
|
|
const cache = await caches.open(CACHE_NAME);
|
|
await cache.addAll(CACHED_URLS);
|
|
}());
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
const { request } = event;
|
|
//if (!request.destination.length) return;
|
|
if (!request.url.startsWith(self.location.href)) return;
|
|
if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') return;
|
|
|
|
event.respondWith(async function () {
|
|
const cache = await caches.open(CACHE_NAME);
|
|
const cachedResponsePromise = await cache.match(request);
|
|
const networkResponsePromise = fetch(request);
|
|
if (request.url.startsWith(self.location.origin)) {
|
|
event.waitUntil(async function () {
|
|
const networkResponse = await networkResponsePromise;
|
|
await cache.put(request, networkResponse.clone());
|
|
}());
|
|
}
|
|
return cachedResponsePromise || networkResponsePromise;
|
|
}());
|
|
});
|
|
|
|
self.addEventListener('activate', event => {
|
|
event.waitUntil(async function () {
|
|
const cacheNames = await caches.keys()
|
|
await Promise.all(
|
|
cacheNames.filter((cacheName) => {
|
|
const deleteThisCache = cacheName !== CACHE_NAME;
|
|
return deleteThisCache;
|
|
}).map(cacheName => caches.delete(cacheName))
|
|
)
|
|
}());
|
|
}); |