-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
54 lines (48 loc) · 1.83 KB
/
Copy pathsw.js
File metadata and controls
54 lines (48 loc) · 1.83 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
/* TestnetScan service worker: offline app shell + static-asset caching.
Chain data (/api, pages) stays network-first so it is never stale.
SPDX-License-Identifier: AGPL-3.0-or-later */
const VERSION = 'ts-v33';
const SHELL = [
'/assets/app.css?v=30',
'/assets/app.js?v=13',
'/assets/qrcode.js?v=1',
'/assets/theme-init.js?v=1',
'/assets/favicon.svg',
'/assets/coins/btc.svg',
'/assets/coins/ltc.svg',
'/assets/coins/xmr.svg',
'/offline.html',
];
self.addEventListener('install', function (e) {
e.waitUntil(caches.open(VERSION).then(function (c) { return c.addAll(SHELL); }).then(function () { return self.skipWaiting(); }));
});
self.addEventListener('activate', function (e) {
e.waitUntil(caches.keys().then(function (keys) {
return Promise.all(keys.filter(function (k) { return k !== VERSION; }).map(function (k) { return caches.delete(k); }));
}).then(function () { return self.clients.claim(); }));
});
self.addEventListener('fetch', function (e) {
var req = e.request;
if (req.method !== 'GET') { return; }
var url = new URL(req.url);
if (url.origin !== location.origin) { return; }
// Static assets: cache-first (immutable, versioned via ?v=).
if (url.pathname.indexOf('/assets/') === 0) {
e.respondWith(caches.match(req).then(function (r) {
return r || fetch(req).then(function (res) {
var copy = res.clone();
caches.open(VERSION).then(function (c) { c.put(req, copy); });
return res;
});
}));
return;
}
// Page navigations: network-first, fall back to cache then offline shell.
if (req.mode === 'navigate') {
e.respondWith(fetch(req).catch(function () {
return caches.match(req).then(function (r) { return r || caches.match('/offline.html'); });
}));
return;
}
// Everything else (including /api chain data): straight to network.
});