You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
944 B

3 years ago
const CACHE_NAME = "version-26-6-19";
3 years ago
const urlsToCache = ["index.html", "offline.html"];
const self = this;
//Install SW
3 years ago
self.addEventListener("install", (event) => {
event.waitUntil(
3 years ago
caches.open(CACHE_NAME).then((cache) => {
console.log("Opened cache");
3 years ago
return cache.addAll(urlsToCache);
})
);
});
//Listen for requests
3 years ago
self.addEventListener("fetch", (event) => {
event.respondWith(
3 years ago
caches.match(event.request).then(() => {
return fetch(event.request).catch(() => caches.match("offline.html"));
})
);
});
//Activate the SW
3 years ago
self.addEventListener("activate", (event) => {
const cacheWhiteList = [];
cacheWhiteList.push(CACHE_NAME);
event.waitUntil(
3 years ago
caches.keys().then((cacheNames) =>
Promise.all(
cacheNames.map((cacheName) => {
if (!cacheWhiteList.includes(cacheName)) {
return caches.delete(cacheName);
}
})
)
)
);
});