-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
116 lines (107 loc) · 3.5 KB
/
Copy pathservice-worker.js
File metadata and controls
116 lines (107 loc) · 3.5 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
/* Swara Sadhana Basic — service worker (offline-capable PWA) */
const VERSION = 'swarasadhana-basic-v87';
const CORE_CACHE = VERSION + '-core';
const RUNTIME_CACHE = VERSION + '-runtime';
/* Same-origin assets precached on install. Paths are relative to the SW
location (the bundle folder), so this works under any sub-path host. */
const CORE_ASSETS = [
'./',
'./index.html',
'./Thala Guide.dc.html',
'./Thala Table.dc.html',
'./Composition Player.dc.html',
'./SwaraSelect.dc.html',
'./manifest.json',
'./assets/support.js',
'./assets/audio-engine.js',
'./assets/audio/03_harmonium_E3.mp3',
'./assets/i18n.js',
'./assets/styles.css',
'./assets/images/favicon.png',
'./assets/images/apple-touch-icon.png',
'./assets/images/icon-192.png',
'./assets/images/icon-512.png',
'./assets/images/logo-white.png',
'./assets/images/logo.png',
'./assets/images/tanpura.png'
];
/* Cross-origin runtime deps cached the first time they load (React + Babel
from unpkg, Google Fonts). After one online visit the app runs fully offline. */
const RUNTIME_HOSTS = [
'unpkg.com',
'fonts.googleapis.com',
'fonts.gstatic.com'
];
self.addEventListener('install', (event) => {
event.waitUntil((async () => {
const cache = await caches.open(CORE_CACHE);
// Cache individually so one failed request doesn't abort the whole install.
await Promise.all(CORE_ASSETS.map((url) =>
cache.add(new Request(url, { cache: 'reload' })).catch(() => {})
));
self.skipWaiting();
})());
});
self.addEventListener('activate', (event) => {
event.waitUntil((async () => {
const keys = await caches.keys();
await Promise.all(keys
.filter((k) => k !== CORE_CACHE && k !== RUNTIME_CACHE)
.map((k) => caches.delete(k)));
await self.clients.claim();
})());
});
self.addEventListener('fetch', (event) => {
const req = event.request;
if (req.method !== 'GET') return;
const url = new URL(req.url);
const sameOrigin = url.origin === self.location.origin;
const isRuntimeHost = RUNTIME_HOSTS.some((h) => url.hostname === h || url.hostname.endsWith('.' + h));
// Navigations: serve cached shell first, fall back to network, then index.
if (req.mode === 'navigate') {
event.respondWith((async () => {
try {
const net = await fetch(req);
const cache = await caches.open(CORE_CACHE);
cache.put('./index.html', net.clone());
return net;
} catch (e) {
return (await caches.match(req)) || (await caches.match('./index.html')) || Response.error();
}
})());
return;
}
// Same-origin assets: cache-first, update in background.
if (sameOrigin) {
event.respondWith((async () => {
const cached = await caches.match(req);
if (cached) return cached;
try {
const net = await fetch(req);
const cache = await caches.open(CORE_CACHE);
cache.put(req, net.clone());
return net;
} catch (e) {
return cached || Response.error();
}
})());
return;
}
// CDN / font deps: cache-first into the runtime cache.
if (isRuntimeHost) {
event.respondWith((async () => {
const cached = await caches.match(req);
if (cached) return cached;
try {
const net = await fetch(req);
if (net && (net.ok || net.type === 'opaque')) {
const cache = await caches.open(RUNTIME_CACHE);
cache.put(req, net.clone());
}
return net;
} catch (e) {
return cached || Response.error();
}
})());
}
});