-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
111 lines (103 loc) · 2.56 KB
/
Copy pathsw.js
File metadata and controls
111 lines (103 loc) · 2.56 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
/* sw.js — offline app shell. Cache-first for same-origin assets;
cross-origin (mempool instance, price APIs) always goes to the network. */
"use strict";
var VERSION = "bitcoinwatch-v4";
var ASSETS = [
"./",
"./index.html",
"./manifest.webmanifest",
"./css/style.css",
"./js/store.js",
"./js/widgets.js",
"./js/poller.js",
"./js/ui.js",
"./fonts/red-hat-mono-400.woff2",
"./fonts/red-hat-mono-500.woff2",
"./fonts/red-hat-mono-600.woff2",
"./fonts/red-hat-mono-700.woff2",
"./img/Bitcoin.svg",
"./img/icon-16.png",
"./img/icon-32.png",
"./img/icon-48.png",
"./img/icon-64.png",
"./img/icon-96.png",
"./img/icon-128.png",
"./img/icon-192.png",
"./img/icon-256.png",
"./img/icon-384.png",
"./img/icon-512.png",
"./img/apple-touch-icon.png",
"./img/icon-maskable-192.png",
"./img/icon-maskable-512.png",
];
self.addEventListener("install", function (e) {
e.waitUntil(
caches
.open(VERSION)
.then(function (cache) {
return cache.addAll(ASSETS);
})
.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 url = new URL(e.request.url);
// never intercept data feeds — they must be live
if (url.origin !== self.location.origin) return;
if (e.request.method !== "GET") return;
// navigations: network first, fall back to cached shell when offline
if (e.request.mode === "navigate") {
e.respondWith(
fetch(e.request).catch(function () {
return caches.match("./index.html");
})
);
return;
}
/* static assets: stale-while-revalidate. Cache-first alone would pin the very
first js/css a visitor ever downloaded, since index.html is network-first
but its asset URLs never change. Serve the cached copy now, refresh for
the next load. */
e.respondWith(
caches.match(e.request).then(function (hit) {
var fresh = fetch(e.request)
.then(function (res) {
if (res.ok) {
var copy = res.clone();
caches.open(VERSION).then(function (cache) {
cache.put(e.request, copy);
});
}
return res;
})
.catch(function () {
return hit; // offline: cached copy or a genuine network error
});
if (!hit) return fresh;
e.waitUntil(fresh);
return hit;
})
);
});