-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
68 lines (60 loc) · 1.83 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
const CACHE_NAME = "static_cache"
const STATIC_ASSETS = [
'/',
'/index.html',
'/start.css',
'/app_start.js',
'/manifest.json',
'/read/index.html',
'/public/perilous_quest/index.html',
'/public/perilous_quest/styles.css',
'/public/perilous_quest/script.js',
'/public/perilous_quest/dark_mode.css',
'/public/favicon.ico',
'/public/wonder.wav',
'/public/lovely_town.wav',
'public/boss_theme.wav',
'/public/selectfx.wav',
'/public/select_null.wav',
'/public/lose.wav',
'/404.html',
'https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0',
'https://fonts.googleapis.com/css2?family=Pixelify+Sans:wght@400..700&display=swap',
'https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap',
'/public/perilous_quest/other_scripts/vanilla-tilt-min.js',
]
async function preCache() {
const cache = await caches.open(CACHE_NAME)
return cache.addAll(STATIC_ASSETS)
}
self.addEventListener('install', event => {
console.log("[SW] installed");
self.skipWaiting()
event.waitUntil(preCache())
})
async function cleanupCache() {
const keys = await caches.keys()
const keysToDelete = keys.map(key => {
if (key !== CACHE_NAME) {
return caches.delete(key)
}
})
return Promise.all(keysToDelete)
}
self.addEventListener('activate', event => {
console.log("[SW] activated");
event.waitUntil(cleanupCache())
})
async function fetchAssets(event) {
try {
const response = await fetch(event.request)
return response
} catch (err) {
const cache = await caches.open(CACHE_NAME)
return cache.match(event.request)
}
}
self.addEventListener('fetch', event => {
console.log("[SW] fetched");
event.respondWith(fetchAssets(event))
})