Skip to content

Commit a1c8cff

Browse files
Move navigator.serviceWorker check
1 parent e35db80 commit a1c8cff

File tree

1 file changed

+35
-39
lines changed

1 file changed

+35
-39
lines changed

Diff for: src/simulator.ts

+35-39
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,44 @@ declare global {
1818

1919
function initServiceWorker() {
2020
window.addEventListener("load", () => {
21-
if ("serviceWorker" in navigator) {
22-
navigator.serviceWorker.register("sw.js").then(
23-
(registration) => {
24-
console.log("Simulator service worker registration successful");
25-
// Reload the page when a new service worker is installed.
26-
registration.onupdatefound = function () {
27-
const installingWorker = registration.installing;
28-
if (installingWorker) {
29-
installingWorker.onstatechange = function () {
30-
if (
31-
installingWorker.state === "installed" &&
32-
navigator.serviceWorker.controller
33-
) {
34-
window.location.reload();
35-
}
36-
};
37-
}
38-
};
39-
},
40-
(error) => {
41-
console.error(
42-
`Simulator service worker registration failed: ${error}`
43-
);
44-
}
45-
);
46-
} else {
47-
console.error("Service workers are not supported.");
48-
}
21+
navigator.serviceWorker.register("sw.js").then(
22+
(registration) => {
23+
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+
};
38+
},
39+
(error) => {
40+
console.error(`Simulator service worker registration failed: ${error}`);
41+
}
42+
);
4943
});
5044
}
5145

52-
if (flags.sw) {
53-
initServiceWorker();
54-
} else {
55-
navigator.serviceWorker.getRegistrations().then(registrations => {
56-
if (registrations.length > 0) {
57-
// We should only have one service worker to unregister.
58-
registrations[0].unregister().then(() => {
59-
window.location.reload();
60-
})
61-
}
62-
})
46+
if ("serviceWorker" in navigator) {
47+
if (flags.sw) {
48+
initServiceWorker();
49+
} else {
50+
navigator.serviceWorker.getRegistrations().then((registrations) => {
51+
if (registrations.length > 0) {
52+
// We should only have one service worker to unregister.
53+
registrations[0].unregister().then(() => {
54+
window.location.reload();
55+
});
56+
}
57+
});
58+
}
6359
}
6460

6561
const fs = new FileSystem();

0 commit comments

Comments
 (0)