-
Notifications
You must be signed in to change notification settings - Fork 13
/
sw.js
50 lines (40 loc) · 1.29 KB
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const CACHE = "quran-app-data";
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
const offlineFallbackPage = "offline-page.html";
// self.addEventListener("message", (event) => {
// if (event.data && event.data.type === "SKIP_WAITING") {
// self.skipWaiting();
// }
// });
self.addEventListener('install', async (event) => {
event.waitUntil(
caches.open(CACHE)
.then((cache) => cache.add(offlineFallbackPage))
);
});
workbox.setConfig({ debug: false });
if (workbox.navigationPreload.isSupported()) {
workbox.navigationPreload.enable();
}
workbox.routing.registerRoute(
new RegExp('/*'),
new workbox.strategies.StaleWhileRevalidate({
cacheName: CACHE
})
);
self.addEventListener('fetch', (event) => {
if (event.request.mode === 'navigate') {
event.respondWith((async () => {
try {
const preloadResp = await event.preloadResponse;
if (preloadResp) {
return preloadResp;
}
return await fetch(event.request);
} catch (error) {
const cache = await caches.open(CACHE);
return await cache.match(offlineFallbackPage);
}
})());
}
});