-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsw.js
89 lines (82 loc) · 2.3 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
var CACHE_NAME = 'appcache-9';
var urlsToCache = [
'/',
'sw.js',
'manifest.webmanifest',
'js/components/main.js',
'images/anilist-icon.svg',
'audio/on.ogg',
'audio/off.ogg',
'https://cdn.jsdelivr.net/npm/[email protected]/pwacompat.min.js',
'https://unpkg.com/@alpinejs/[email protected]/dist/module.esm.js',
'https://unpkg.com/@alpinejs/[email protected]/dist/module.esm.js',
'https://unpkg.com/@alpinejs/[email protected]/dist/module.esm.js',
'https://unpkg.com/[email protected]/dist/module.esm.js',
'https://cdn.skypack.dev/pin/[email protected]/mode=imports,min/optimized/twind/shim.js',
'https://rsms.me/inter/inter.css',
'images/icons/apple-touch-icon.png',
'images/icons/icon_x192.png',
'images/icons/icon_x512.png',
'images/icons/maskable_icon_x512.png',
'images/icons/favicon-32x32.png',
'images/icons/favicon-16x16.png',
'images/icons/favicon.ico',
'https://clovre.pigeonivy.com/images/ogimage.png',
];
self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
console.log('Opened cache:', CACHE_NAME);
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('activate', function (event) {
var cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener('message', function (event) {
if (event.data.action === 'skipWaiting') {
self.skipWaiting();
}
});
self.addEventListener('fetch', function (event) {
if (event.request.mode === 'navigate') {
event.respondWith(caches.match('/'));
return;
}
event.respondWith(
caches.match(event.request).then(function (response) {
if (response) {
return response;
}
var fetchRequest = event.request.clone();
if (navigator.onLine) {
return fetch(fetchRequest).then(function (response) {
if (
!response ||
response.status !== 200 ||
response.type !== 'basic'
) {
return response;
}
var responseToCache = response.clone();
caches.open(CACHE_NAME).then(function (cache) {
cache.put(event.request, responseToCache);
});
return response;
});
}
})
);
});