-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
137 lines (119 loc) · 3.24 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
const CACHE = "v2.0" // name of the current cache
const OFFLINE = "/offline"
const CDNS = "Third-Party"
const AUTO_CACHE = [
OFFLINE,
"/",
"/css/home.css",
"/css/main.css",
"/js/home.js",
"/js/main.js",
"/media/asteroid.png",
"/media/astro.png",
"/media/cosmonaut.png",
"/media/satellite.png",
"/media/starfield.jpg",
"/apple-touch-icon.png",
"/logo512.png",
"/favicon.ico",
"/site.webmanifest",
"/game"
]
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)
})
.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("logo512.png") || url.includes("favicon.ico") || url.includes("site.webmanifest")) return true
return false
}
self.addEventListener("fetch", (event) => {
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 (event.request.url.includes("cdnjs.cloudflare.com")
|| event.request.url.includes("maxcdn.bootstrapcdn.com")
|| event.request.url.includes("code.jquery.com")
|| event.request.url.includes("cdn.jsdelivr.net")
|| event.request.url.includes("fonts.googleapis.com")
|| event.request.url.includes("fonts.gstatic.com")
|| event.request.url.includes("use.fontawesome.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(!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()
})
})
)
}
})