Skip to content

Commit 07b703b

Browse files
Auto reload the page when new service worker is installed
1 parent 4c724eb commit 07b703b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/simulator.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,22 @@ function initServiceWorker() {
1919
window.addEventListener("load", () => {
2020
if ("serviceWorker" in navigator) {
2121
navigator.serviceWorker.register("sw.js").then(
22-
(_registration) => {
22+
(registration) => {
2323
console.log("Simulator service worker registration successful");
24+
// Reload the page when a new service worker is installed.
25+
registration.onupdatefound = function () {
26+
const installingWorker = registration.installing;
27+
if (installingWorker) {
28+
installingWorker.onstatechange = function () {
29+
if (
30+
installingWorker.state === "installed" &&
31+
navigator.serviceWorker.controller
32+
) {
33+
window.location.reload();
34+
}
35+
};
36+
}
37+
};
2438
},
2539
(error) => {
2640
console.error(

src/sw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const version = "v0.0.2";
1+
const version = "v0.0.3";
22
const assets = ["simulator.html", "build/simulator.js", "build/firmware.js"];
33
const cacheName = `simulator-${version}`;
44

0 commit comments

Comments
 (0)