Make pwa not cache serviceworker stuff, as well as livereload variations

This commit is contained in:
Tobias Berger 2021-10-05 13:38:55 +02:00
parent a9ce798490
commit e5eafba885

View file

@ -1,8 +1,9 @@
const CACHE_NAME = "sharg-static-cache"; const CACHE_NAME = "sharg-static-cache";
const CACHE_VERSION = "v0.0.1"; const CACHE_VERSION = "v0.0.2";
const STATIC_CACHE_NAME = CACHE_NAME + "-" + CACHE_VERSION; const STATIC_CACHE_NAME = CACHE_NAME + "-" + CACHE_VERSION;
const FILES_TO_CACHE = ["/", "/index.html", "/build/bundle.js", "/build/bundle.css", "/favicon.png"]; const INITIAL_FILES_TO_CACHE = ["/", "/index.html", "/build/bundle.js", "/build/bundle.css", "/favicon.png"];
const CACHE_DENYLIST = ["/manifest.json", "/service-worker.js", "livereload.js"];
const globalScope = self as WorkerGlobalScope as ServiceWorkerGlobalScope; const globalScope = self as WorkerGlobalScope as ServiceWorkerGlobalScope;
@ -12,7 +13,7 @@ globalScope.addEventListener("install", (event) => {
event.waitUntil( event.waitUntil(
caches.open(STATIC_CACHE_NAME).then((cache) => { caches.open(STATIC_CACHE_NAME).then((cache) => {
console.debug("[ServiceWorker] Pre-caching offline page"); console.debug("[ServiceWorker] Pre-caching offline page");
return cache.addAll(FILES_TO_CACHE); return cache.addAll(INITIAL_FILES_TO_CACHE);
}) })
); );
}); });
@ -37,19 +38,21 @@ globalScope.addEventListener("activate", (evt) => {
}); });
globalScope.addEventListener("fetch", (event) => { globalScope.addEventListener("fetch", (event) => {
console.debug("[ServiceWorker] Fetch", event.request.url); const path = new URL(event.request.url).pathname;
console.debug("[ServiceWorker] Fetch", event.request.url, "==", path);
function getFromFetch(cache: Cache) { function getFromFetch(cache: Cache) {
console.debug("getFromFetch");
return fetch(event.request).then((response) => { return fetch(event.request).then((response) => {
cache.put(event.request.url, response.clone()); if (!CACHE_DENYLIST.includes(path)) {
cache.put(path, response.clone());
}
return response; return response;
}); });
} }
async function getFromCache() { async function getFromCache() {
const cache = await caches.open(STATIC_CACHE_NAME); const cache = await caches.open(STATIC_CACHE_NAME);
const cached = await cache.match(event.request); const cached = await cache.match(path);
if (cached !== undefined) { if (cached !== undefined) {
return cached; return cached;
} }