|
1 |
| -function initSimulatorServiceWorker() { |
2 |
| - const simUrls = ["simulator.html", "build/simulator.js", "build/firmware.js"]; |
3 |
| - let didInstall = false; |
4 |
| - const cacheName = "simulator"; |
| 1 | +const version = "v0.0.1"; |
| 2 | +const assets = ["simulator.html", "build/simulator.js", "build/firmware.js"]; |
| 3 | +const cacheName = `simulator-${version}`; |
5 | 4 |
|
6 |
| - self.addEventListener("install", function (ev) { |
7 |
| - didInstall = true; |
8 |
| - console.log("Installing service worker..."); |
9 |
| - ev.waitUntil( |
10 |
| - caches |
11 |
| - .open(cacheName) |
12 |
| - .then(function (cache) { |
13 |
| - console.log("Opened cache"); |
14 |
| - return cache.addAll(simUrls); |
15 |
| - }) |
16 |
| - .then(function () { |
17 |
| - return self.skipWaiting(); |
18 |
| - }) |
19 |
| - ); |
20 |
| - }); |
| 5 | +self.addEventListener("install", (event) => { |
| 6 | + console.log("Installing simulator service worker..."); |
| 7 | + event.waitUntil( |
| 8 | + (async () => { |
| 9 | + const cache = await caches.open(cacheName); |
| 10 | + await cache.addAll(assets); |
| 11 | + self.skipWaiting(); |
| 12 | + })() |
| 13 | + ); |
| 14 | +}); |
21 | 15 |
|
22 |
| - self.addEventListener("activate", function (ev) { |
23 |
| - console.log("Activating service worker..."); |
24 |
| - ev.waitUntil( |
25 |
| - caches |
26 |
| - .keys() |
27 |
| - .then(function (_cacheNames) { |
28 |
| - // Delete old versions in cache here. |
29 |
| - }) |
30 |
| - .then(function () { |
31 |
| - if (didInstall) { |
32 |
| - // Only notify clients for the first activation |
33 |
| - didInstall = false; |
34 |
| - // Necessary? |
35 |
| - return notifyAllClientsAsync(); |
| 16 | +self.addEventListener("activate", (event) => { |
| 17 | + console.log("Activating simulator service worker..."); |
| 18 | + event.waitUntil( |
| 19 | + (async () => { |
| 20 | + const names = await caches.keys(); |
| 21 | + await Promise.all( |
| 22 | + names.map((name) => { |
| 23 | + if (name !== cacheName) { |
| 24 | + return caches.delete(name); |
36 | 25 | }
|
37 |
| - return Promise.resolve(); |
38 | 26 | })
|
39 |
| - ); |
40 |
| - }); |
41 |
| - |
42 |
| - self.addEventListener("fetch", function (ev) { |
43 |
| - ev.respondWith( |
44 |
| - caches.match(ev.request).then(function (response) { |
45 |
| - return response || fetch(ev.request); |
46 |
| - }) |
47 |
| - ); |
48 |
| - }); |
49 |
| - |
50 |
| - function notifyAllClientsAsync() { |
51 |
| - var scope = self; |
52 |
| - return scope.clients |
53 |
| - .claim() |
54 |
| - .then(function () { |
55 |
| - return scope.clients.matchAll(); |
56 |
| - }) |
57 |
| - .then(function (clients) { |
58 |
| - clients.forEach(function (client) { |
59 |
| - return client.postMessage({ |
60 |
| - type: "serviceworker", |
61 |
| - state: "activated", |
62 |
| - }); |
63 |
| - }); |
64 |
| - }); |
65 |
| - } |
66 |
| -} |
| 27 | + ); |
| 28 | + await clients.claim(); |
| 29 | + })() |
| 30 | + ); |
| 31 | +}); |
67 | 32 |
|
68 |
| -initSimulatorServiceWorker(); |
| 33 | +self.addEventListener("fetch", (event) => { |
| 34 | + event.respondWith( |
| 35 | + (async () => { |
| 36 | + const cachedResponse = await caches.match(event.request); |
| 37 | + if (cachedResponse) { |
| 38 | + return cachedResponse; |
| 39 | + } |
| 40 | + const response = await fetch(event.request); |
| 41 | + const cache = await caches.open(cacheName); |
| 42 | + cache.put(event.request, response.clone()); |
| 43 | + return response; |
| 44 | + })() |
| 45 | + ); |
| 46 | +}); |
0 commit comments