dsh-lite 0.0.0-stage → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/public/sw.js ADDED
@@ -0,0 +1,76 @@
1
+ const CACHE = 'dsh-lite-v30'
2
+ const SHELL = [
3
+ '/lite/',
4
+ '/lite/app.js?v=30',
5
+ '/lite/i18n.js?v=30',
6
+ '/lite/style.css?v=30',
7
+ '/lite/theme.css?v=30',
8
+ '/lite/theme.js?v=30',
9
+ '/lite/question-answers.js?v=30',
10
+ '/lite/manifest.webmanifest',
11
+ '/lite/icon-192.png',
12
+ '/lite/icon-512.png',
13
+ '/lite/favicon.svg',
14
+ ]
15
+
16
+ self.addEventListener('install', (event) => {
17
+ event.waitUntil((async () => {
18
+ const cache = await caches.open(CACHE)
19
+ await Promise.all(SHELL.map(async (url) => {
20
+ const response = await fetch(url, { cache: 'reload' })
21
+ if (!response.ok) throw new Error(`dsh-lite: precache ${url} failed with ${response.status}`)
22
+ await cache.put(url, response)
23
+ }))
24
+ await self.skipWaiting()
25
+ })())
26
+ })
27
+
28
+ self.addEventListener('activate', (event) => {
29
+ event.waitUntil(
30
+ caches.keys()
31
+ .then((keys) => Promise.all(keys.filter((key) => key !== CACHE).map((key) => caches.delete(key))))
32
+ .then(() => self.clients.claim()),
33
+ )
34
+ })
35
+
36
+ self.addEventListener('notificationclick', (event) => {
37
+ event.notification.close()
38
+ const sessionId = event.notification.data?.sessionId
39
+ const target = new URL('/lite/', self.location.origin)
40
+ if (typeof sessionId === 'string' && sessionId.length > 0) target.searchParams.set('session', sessionId)
41
+ event.waitUntil((async () => {
42
+ const windows = await self.clients.matchAll({ type: 'window', includeUncontrolled: true })
43
+ for (const client of windows) {
44
+ if (new URL(client.url).pathname.startsWith('/lite/')) {
45
+ await client.focus()
46
+ if (typeof sessionId === 'string' && sessionId.length > 0) {
47
+ client.postMessage({ type: 'dsh-lite:open-session', sessionId })
48
+ }
49
+ return
50
+ }
51
+ }
52
+ await self.clients.openWindow(target.href)
53
+ })())
54
+ })
55
+
56
+ self.addEventListener('fetch', (event) => {
57
+ const url = new URL(event.request.url)
58
+ if (url.origin !== self.location.origin) return
59
+ if (url.pathname.startsWith('/api/')) return
60
+
61
+ if (event.request.mode === 'navigate') {
62
+ event.respondWith(fetch(event.request).catch(() => caches.match('/lite/')))
63
+ return
64
+ }
65
+
66
+ event.respondWith(
67
+ caches.match(event.request).then((hit) => {
68
+ if (hit !== undefined) return hit
69
+ return fetch(event.request).then((response) => {
70
+ const copy = response.clone()
71
+ caches.open(CACHE).then((cache) => cache.put(event.request, copy)).catch(() => {})
72
+ return response
73
+ })
74
+ }),
75
+ )
76
+ })