-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
142 lines (128 loc) · 3.81 KB
/
Copy pathsw.js
File metadata and controls
142 lines (128 loc) · 3.81 KB
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
self.addEventListener('install', (event) => {
event.waitUntil((async () => {
const cache = await caches.open(APP_SHELL_CACHE)
await cache.addAll(APP_SHELL_ASSETS)
await self.skipWaiting()
})())
})
self.addEventListener('activate', (event) => {
event.waitUntil((async () => {
const keys = await caches.keys()
await Promise.all(
keys
.filter((key) => key.startsWith('wiredove-') && key !== APP_SHELL_CACHE)
.map((key) => caches.delete(key))
)
await self.clients.claim()
})())
})
const CACHE_VERSION = 'v1'
const APP_SHELL_CACHE = `wiredove-app-shell-${CACHE_VERSION}`
const APP_SHELL_ASSETS = [
'/',
'/index.html',
'/style.css',
'/app.js',
'/manifest.json',
'/favicon.ico',
'/dovepurple_sm.png',
'/dovepurple.png',
'/dove_sm.png',
]
const isCacheableRequest = (request) => {
if (!request || request.method !== 'GET') { return false }
const url = new URL(request.url)
if (url.origin !== self.location.origin) { return false }
return true
}
const isNavigationRequest = (request) => request.mode === 'navigate'
const isStaticAssetRequest = (request) => {
const url = new URL(request.url)
return /\.(?:js|css|png|jpg|jpeg|svg|ico|webmanifest|json)$/i.test(url.pathname)
}
const canCacheResponse = (response) => Boolean(response && response.ok)
const staleWhileRevalidate = async (request) => {
const cache = await caches.open(APP_SHELL_CACHE)
const cached = await cache.match(request)
const networkPromise = fetch(request).then(async (response) => {
if (canCacheResponse(response)) {
await cache.put(request, response.clone())
}
return response
}).catch(() => null)
if (cached) {
void networkPromise
return cached
}
const fresh = await networkPromise
if (fresh) { return fresh }
return Response.error()
}
const networkFirst = async (request) => {
const cache = await caches.open(APP_SHELL_CACHE)
try {
const response = await fetch(request)
if (canCacheResponse(response)) {
await cache.put(request, response.clone())
}
return response
} catch {
const cached = await cache.match(request)
if (cached) { return cached }
if (isNavigationRequest(request)) {
const fallback = await cache.match('/index.html')
if (fallback) { return fallback }
}
return Response.error()
}
}
self.addEventListener('fetch', (event) => {
const { request } = event
if (!isCacheableRequest(request)) { return }
if (isNavigationRequest(request)) {
event.respondWith(networkFirst(request))
return
}
if (isStaticAssetRequest(request)) {
event.respondWith(staleWhileRevalidate(request))
}
})
self.addEventListener('push', (event) => {
let payload = { title: 'wiredove', body: 'New message', url: '/' }
if (event.data) {
try {
payload = event.data.json()
} catch {
try {
payload = JSON.parse(event.data.text())
} catch {
payload.body = event.data.text()
}
}
}
const hash = payload.hash
const targetUrl = payload.url ||
(typeof hash === 'string' && hash.length > 0
? `https://wiredove.net/#${hash}`
: '/')
const options = {
body: payload.body,
data: { url: targetUrl },
icon: payload.icon || '/dovepurple_sm.png',
badge: payload.badge,
}
event.waitUntil(self.registration.showNotification(payload.title, options))
})
self.addEventListener('notificationclick', (event) => {
event.notification.close()
const target = event.notification.data?.url || '/'
event.waitUntil(
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clients) => {
for (const client of clients) {
if (client.url.includes(target) && 'focus' in client) return client.focus()
}
if (self.clients.openWindow) return self.clients.openWindow(target)
return undefined
}),
)
})