Make pwa not cache serviceworker stuff, as well as livereload variations
This commit is contained in:
parent
a9ce798490
commit
e5eafba885
1 changed files with 10 additions and 7 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue