forked from WWBN/AVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
96 lines (89 loc) · 3.18 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
importScripts('workbox-v6.5.3/workbox-sw.js');
workbox.setConfig({
modulePathPrefix: 'workbox-v6.5.3/',
debug: false
});
const webSiteRootURL = this.location.href.split('sw.js?')[0];
const FALLBACK_HTML_URL = webSiteRootURL + 'offline';
const CACHE_NAME = 'avideo-cache-ver-3.6';
const staticAssetsCacheName = CACHE_NAME + '-static-assets';
console.log('sw strategy CACHE_NAME', CACHE_NAME);
self.addEventListener('install', (event) => {
console.log('Service worker installed');
event.waitUntil(
caches.open(CACHE_NAME).then(async (cache) => {
const cachedResponse = await cache.match(FALLBACK_HTML_URL);
if (!cachedResponse) {
await cache.add(FALLBACK_HTML_URL);
}
//console.log('Service worker FALLBACK_HTML_URL added', FALLBACK_HTML_URL);
// Add other static assets to precache here
})
);
});
self.addEventListener('activate', (event) => {
console.log('Service worker activated');
});
/*
self.addEventListener('fetch', (event) => {
if (event.request.mode === 'navigate') {
console.log(`Service worker intercepted request: ${event.request.url}`);
}
console.log('fetch', event.request);
event.respondWith(
caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
console.log(`Cache hit: ${event.request.url}`);
return cachedResponse;
}
console.log(`Cache miss: ${event.request.url}`);
return fetch(event.request);
})
);
});
*/
workbox.routing.registerRoute(
({ request }) => {
return (request.destination === 'script' ||
request.destination === 'style' ||
request.destination === 'image' ||
request.url.match(/\.map/) ||
request.url.match(/\.woff2/));
},
new workbox.strategies.StaleWhileRevalidate({
cacheName: staticAssetsCacheName,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200]
})
]
})
);
workbox.routing.setCatchHandler(async ({ event }) => {
console.log('setCatchHandler called', event.request.url);
if (event.request.destination === 'document') {
try {
const networkResponse = await fetch(event.request);
console.log('networkResponse', networkResponse);
if (networkResponse.ok) {
const cache = await caches.open(CACHE_NAME);
await cache.put(event.request, networkResponse.clone());
return networkResponse;
}
} catch (error) {
console.error(error);
}
// Redirect to the offline page if the user is offline
if (navigator.onLine === false) {
console.log('User is offline, redirecting to offline page');
return Response.redirect(FALLBACK_HTML_URL);
}
// Return the cached response if it exists
const cachedResponse = await caches.match(FALLBACK_HTML_URL);
console.log('cachedResponse', cachedResponse);
if (cachedResponse) {
return cachedResponse;
}
}
return Response.error();
});