Skip to content

Commit

Permalink
Added serviceworker.js πŸ‘¨β€πŸ”§βš™
Browse files Browse the repository at this point in the history
- πŸ› 
- πŸ”§
- πŸ—œ
  • Loading branch information
Pranav-yadav authored Oct 23, 2021
1 parent 3dee848 commit 0744081
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions public/serviceworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const CACHE_NAME = "version-1";
const urlsToCache = ["index.html", "offline.html"];

// Install SW
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log("Opened cache");
return cache.addAll(urlsToCache);
})
);
});

// Listen for Request
self.addEventListener("fetch", (event) => {
event.respondWith(
caches.match(event.request).then(() => {
return fetch(event.request).catch(() => {
caches.match("offline.html");
});
})
);
});

// Activate the SW
self.addEventListener("activate", (event) => {
const cacheWhitelist = [];
cacheWhitelist.push(CACHE_NAME);

event.waitUntil(
caches.keys().then((cacheNames) =>
Promise.all(
cacheNames.map((cacheName) => {
if (!cacheWhitelist.includes(cacheName)) {
return caches.delete(cacheName);
}
})
)
)
);
});

0 comments on commit 0744081

Please sign in to comment.