Skip to content

Commit

Permalink
Move navigator.serviceWorker check
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-robert committed May 28, 2024
1 parent e35db80 commit a1c8cff
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions src/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,44 @@ declare global {

function initServiceWorker() {
window.addEventListener("load", () => {
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("sw.js").then(
(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(
`Simulator service worker registration failed: ${error}`
);
}
);
} else {
console.error("Service workers are not supported.");
}
navigator.serviceWorker.register("sw.js").then(
(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(`Simulator service worker registration failed: ${error}`);
}
);
});
}

if (flags.sw) {
initServiceWorker();
} else {
navigator.serviceWorker.getRegistrations().then(registrations => {
if (registrations.length > 0) {
// We should only have one service worker to unregister.
registrations[0].unregister().then(() => {
window.location.reload();
})
}
})
if ("serviceWorker" in navigator) {
if (flags.sw) {
initServiceWorker();
} else {
navigator.serviceWorker.getRegistrations().then((registrations) => {
if (registrations.length > 0) {
// We should only have one service worker to unregister.
registrations[0].unregister().then(() => {
window.location.reload();
});
}
});
}
}

const fs = new FileSystem();
Expand Down

0 comments on commit a1c8cff

Please sign in to comment.