-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
171 lines (152 loc) · 4.16 KB
/
serviceworker.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
importScripts("/assets/js/localforage.js")
importScripts("/assets/js/pure.js")
const CACHE = "content-v7" // name of the current cache
const OFFLINE = "/offline.html" // URL to offline HTML document
const AVATARS = "avatars"
const CDNS = "cdns"
const DEFAULT_AVATAR = "/assets/logo512.png"
const AUTO_CACHE = [
OFFLINE,
"/",
"/logo512.png",
"/transparent.png",
"/favicon.ico",
"/qr",
"/history",
"/login",
"/register",
"/privacy",
"/authorize",
"/apple-touch-icon.png",
"/site.webmanifest",
"/assets/js/localforage.js",
"/assets/js/pure.js",
"/assets/js/qrCodeScanner.js",
"/assets/js/authorize.js",
"/assets/js/home.js",
"/assets/js/history.js",
"/assets/js/login.js",
"/assets/js/sitepointqr.js",
"/assets/js/endpoints.js",
"/assets/js/nprogress.js",
"/assets/css/authorize.css",
"/assets/css/home.css",
"/assets/css/login.css",
"/assets/css/qr.css",
"/assets/css/history.css",
"/assets/images/done.gif",
"/assets/images/loading.gif",
"/assets/images/registrationsuccess.gif",
"/assets/images/authorizing.gif",
"/assets/images/qr.png",
"/assets/images/bluewave.png",
"/assets/images/history.svg",
"/assets/images/back.svg",
]
self.addEventListener("install", (event) => {
event.waitUntil(
caches
.open(CACHE)
.then((cache) => cache.addAll(AUTO_CACHE))
.then(self.skipWaiting())
)
})
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return cacheNames.filter((cacheName) => CACHE !== cacheName && AVATARS !== cacheName)
})
.then((unusedCaches) => {
console.log("DESTROYING CACHE", unusedCaches.join(","))
return Promise.all(
unusedCaches.map((unusedCache) => {
return caches.delete(unusedCache)
})
)
})
.then(() => self.clients.claim())
)
})
function isCached(url) {
let origin = self.location.origin
if (url.includes("assets")) return true
if (url == `${origin}/`) return true
if (url.includes("logo.png") || url.includes("favicon.ico") || url.includes("site.webmanifest")) return true
return false
}
self.addEventListener("fetch", (event) => {
if (event.request.url.includes("avatars.dicebear.com")) {
event.respondWith(
caches.open(AVATARS).then((cache) => {
return cache.match(event.request).then((response) => {
if (response) return response
return fetch(event.request).then((response) => {
cache.put(event.request, response.clone())
return response
}).catch(() => {
return caches.match(DEFAULT_AVATAR)
})
})
})
)
return
}
if (event.request.url.includes("cdnjs.cloudflare.com")) {
event.respondWith(
caches.open(CDNS).then((cache) => {
return cache.match(event.request).then((response) => {
if (response) return response
return fetch(event.request).then((response) => {
cache.put(event.request, response.clone())
return response
})
})
})
)
return
}
if (
!event.request.url.startsWith(self.location.origin) ||
event.request.method !== "GET"
) {
return void event.respondWith(fetch(event.request).catch((err) => console.log(err)))
}
if(!isCached(event.request.url)){
event.respondWith(
fetch(event.request)
.then((response) => {
caches.open(CACHE).then((cache) => {
cache.put(event.request, response)
})
return response.clone()
})
.catch((_err) => {
return caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse
}
return caches.open(CACHE).then((cache) => {
const offlineRequest = new Request(OFFLINE)
return cache.match(offlineRequest)
})
})
})
)
} else {
event.respondWith(
caches.match(event.request).then((response) => {
if (response) {
return response
}
return fetch(event.request).then((response) => {
caches.open(CACHE).then((cache) => {
cache.put(event.request, response)
})
return response.clone()
})
})
)
}
})