Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
fix service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
BaseMax committed Sep 12, 2024
1 parent d26f66e commit 0754f0d
Showing 1 changed file with 48 additions and 48 deletions.
96 changes: 48 additions & 48 deletions script/service-worker.js
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;
})
);
});

0 comments on commit 0754f0d

Please sign in to comment.