This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,68 @@ | ||
const CACHE_NAME = 'salam-cache-v1'; | ||
|
||
const ASSETS_TO_CACHE = [ | ||
'site.webmanifest', | ||
'/site.webmanifest', | ||
|
||
// style | ||
'style/style.css', | ||
// style | ||
'/style/style.css', | ||
|
||
// script | ||
'script/script.js', | ||
// script | ||
'/script/script.js', | ||
|
||
// salam | ||
'salam-wa.js', | ||
'salam-wa.wasm', | ||
// salam | ||
'/salam-wa.js', | ||
'/salam-wa.wasm', | ||
|
||
// images | ||
'image/android-chrome-192x192.png', | ||
'image/android-chrome-512x512.png', | ||
'image/apple-touch-icon.png', | ||
'image/favicon-16x16.png', | ||
'image/favicon-32x32.png', | ||
'image/favicon.ico', | ||
// images | ||
'/image/android-chrome-192x192.png', | ||
'/image/android-chrome-512x512.png', | ||
'/image/apple-touch-icon.png', | ||
'/image/favicon-16x16.png', | ||
'/image/favicon-32x32.png', | ||
'/image/favicon.ico', | ||
]; | ||
|
||
// Install Event - Cache the defined assets | ||
self.addEventListener('install', event => { | ||
event.waitUntil( | ||
caches.open(CACHE_NAME) | ||
.then(cache => { | ||
return cache.addAll(ASSETS_TO_CACHE); | ||
}) | ||
.then(() => self.skipWaiting()) | ||
); | ||
event.waitUntil( | ||
caches.open(CACHE_NAME) | ||
.then(cache => { | ||
return cache.addAll(ASSETS_TO_CACHE); | ||
}) | ||
.then(() => self.skipWaiting()) | ||
); | ||
}); | ||
|
||
// Activate Event - Clean up old caches | ||
self.addEventListener('activate', event => { | ||
const cacheWhitelist = [CACHE_NAME]; | ||
event.waitUntil( | ||
caches.keys().then(cacheNames => { | ||
return Promise.all( | ||
cacheNames.map(cacheName => { | ||
if (!cacheWhitelist.includes(cacheName)) { | ||
return caches.delete(cacheName); | ||
} | ||
}) | ||
); | ||
}).then(() => self.clients.claim()) | ||
); | ||
const cacheWhitelist = [CACHE_NAME]; | ||
event.waitUntil( | ||
caches.keys().then(cacheNames => { | ||
return Promise.all( | ||
cacheNames.map(cacheName => { | ||
if (!cacheWhitelist.includes(cacheName)) { | ||
return caches.delete(cacheName); | ||
} | ||
}) | ||
); | ||
}).then(() => self.clients.claim()) | ||
); | ||
}); | ||
|
||
// Fetch Event - Serve cached assets, update cache when online | ||
self.addEventListener('fetch', event => { | ||
event.respondWith( | ||
caches.match(event.request).then(cachedResponse => { | ||
const fetchPromise = fetch(event.request).then(networkResponse => { | ||
if (networkResponse && networkResponse.status === 200) { | ||
caches.open(CACHE_NAME).then(cache => { | ||
cache.put(event.request, networkResponse.clone()); | ||
}); | ||
} | ||
return networkResponse; | ||
}).catch(() => cachedResponse); | ||
return cachedResponse || fetchPromise; | ||
}) | ||
); | ||
event.respondWith( | ||
caches.match(event.request).then(cachedResponse => { | ||
const fetchPromise = fetch(event.request).then(networkResponse => { | ||
if (networkResponse && networkResponse.status === 200) { | ||
caches.open(CACHE_NAME).then(cache => { | ||
cache.put(event.request, networkResponse.clone()); | ||
}); | ||
} | ||
return networkResponse; | ||
}).catch(() => cachedResponse); | ||
return cachedResponse || fetchPromise; | ||
}) | ||
); | ||
}); |