-
Notifications
You must be signed in to change notification settings - Fork 972
/
sw.js
145 lines (133 loc) · 4.88 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
importScripts('workbox-v6.5.3/workbox-sw.js');
workbox.setConfig({
modulePathPrefix: 'workbox-v6.5.3/',
debug: false
});
importScripts('workbox-v6.5.3/workbox-expiration.prod.js');
const webSiteRootURL = this.location.href.split('sw.js?')[0];
const FALLBACK_HTML_URL = webSiteRootURL + 'offline';
const CACHE_NAME = 'avideo-cache-ver-3.6';
const _maxEntries = 400;
const _1_WEEK = 7 * 24 * 60 * 60;
const staticAssetsCacheName = CACHE_NAME + '-static-assets';
function hasCacheParameter(url) {
return url.includes('cache=');
}
function isRequestValid(request) {
return (!request.url.match(/\.php/) || request.url.match(/\.js.php/)) && (request.destination === 'script' ||
request.destination === 'style' ||
request.destination === 'image' ||
request.url.match(/\.map/) ||
request.url.match(/\.ico/) ||
request.url.match(/\.woff2/));
}
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');
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if (cache !== CACHE_NAME) {
console.log("Service worker: Clearing old cache", cache);
return caches.delete(cache);
}
})
);
})
);
});
const cacheFirst = new workbox.strategies.CacheFirst({
cacheName: staticAssetsCacheName,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200]
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: _maxEntries, // Adjust this value based on your needs.
maxAgeSeconds: _1_WEEK, // 1 week
}),
]
});
const staleWhileRevalidate = new workbox.strategies.StaleWhileRevalidate({
cacheName: staticAssetsCacheName,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200]
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: _maxEntries, // Adjust this value based on your needs.
maxAgeSeconds: _1_WEEK, // 1 week
}),
]
});
const networkFirst = new workbox.strategies.NetworkFirst({
cacheName: staticAssetsCacheName,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200]
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: _maxEntries, // Adjust this value based on your needs.
maxAgeSeconds: _1_WEEK, // 1 week
}),
]
});
workbox.routing.registerRoute(
({ request }) => isRequestValid(request),
async ({ request, event }) => {
try {
if (hasCacheParameter(request.url)) {
//console.log('cacheFirst', request.url);
return await cacheFirst.handle({ request, event });
} else {
// console.log('staleWhileRevalidate', request.url);
return await staleWhileRevalidate.handle({ request, event });
}
} catch (error) {
console.error('registerRoute networkFirst', request.url);
return await networkFirst.handle({ request, event });
}
}
);
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();
});