Skip to content

Commit

Permalink
Auto reload the page when new service worker is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-robert committed Apr 25, 2024
1 parent 4c724eb commit 07b703b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@ function initServiceWorker() {
window.addEventListener("load", () => {
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("sw.js").then(
(_registration) => {
(registration) => {
console.log("Simulator service worker registration successful");
// Reload the page when a new service worker is installed.
registration.onupdatefound = function () {
const installingWorker = registration.installing;
if (installingWorker) {
installingWorker.onstatechange = function () {
if (
installingWorker.state === "installed" &&
navigator.serviceWorker.controller
) {
window.location.reload();
}
};
}
};
},
(error) => {
console.error(
Expand Down
2 changes: 1 addition & 1 deletion src/sw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const version = "v0.0.2";
const version = "v0.0.3";
const assets = ["simulator.html", "build/simulator.js", "build/firmware.js"];
const cacheName = `simulator-${version}`;

Expand Down

0 comments on commit 07b703b

Please sign in to comment.