Skip to content

Commit 538355f

Browse files
Update service worker, add version control
1 parent 0090371 commit 538355f

File tree

2 files changed

+53
-71
lines changed

2 files changed

+53
-71
lines changed

src/simulator.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@ declare global {
1616
}
1717

1818
function initServiceWorker() {
19-
if ("serviceWorker" in navigator) {
20-
window.addEventListener("load", () => {
19+
window.addEventListener("load", () => {
20+
if ("serviceWorker" in navigator) {
2121
navigator.serviceWorker.register("sw.js").then(
22-
function (_registration) {
23-
console.log("Simulator ServiceWorker registration successful");
22+
(registration) => {
23+
console.log("Simulator service worker registration successful");
2424
},
25-
function (err) {
26-
console.log("Simulator ServiceWorker registration failed: ", err);
25+
(error) => {
26+
console.error(
27+
`Simulator service worker registration failed: ${error}`
28+
);
2729
}
2830
);
29-
});
30-
}
31+
} else {
32+
console.error("Service workers are not supported.");
33+
}
34+
});
3135
}
3236

3337
initServiceWorker();

src/sw.js

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,46 @@
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}`;
54

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+
});
2115

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);
3625
}
37-
return Promise.resolve();
3826
})
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+
});
6732

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

Comments
 (0)