-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (77 loc) · 2.97 KB
/
app.js
File metadata and controls
86 lines (77 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// ============================================================
// EasySorting — app.js
// Service Worker registration + PWA install prompt
// ============================================================
// --- Service Worker Registration ---
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/service-worker.js")
.then((registration) => {
console.log("[App] Service Worker registered:", registration.scope);
// When a new SW is waiting (e.g. after a site update),
// it will skipWaiting automatically; reload to apply it.
registration.addEventListener("updatefound", () => {
const newWorker = registration.installing;
newWorker.addEventListener("statechange", () => {
if (
newWorker.state === "activated" &&
navigator.serviceWorker.controller
) {
// New version activated — optionally inform user
console.log("[App] New version activated.");
}
});
});
})
.catch((err) => console.error("[App] SW registration failed:", err));
// Reload the page when the SW controller changes (new SW took over)
let refreshing = false;
navigator.serviceWorker.addEventListener("controllerchange", () => {
if (!refreshing) {
refreshing = true;
if (isStandalone()) {
// Only prompt in standalone (installed) mode to avoid jarring reloads
alert(
"EasySorting has been updated! Reloading for the latest version.",
);
window.location.reload();
}
}
});
}
// --- PWA Install Prompt ---
let deferredPrompt;
let installButton = document.getElementById("installButton");
let installButtonMobile = document.getElementById("install-button-mobile");
window.addEventListener("beforeinstallprompt", (e) => {
e.preventDefault();
deferredPrompt = e;
if (installButton) installButton.style.display = "block";
if (installButtonMobile) installButtonMobile.style.display = "block";
});
window.addEventListener("appinstalled", () => {
console.log("[App] PWA was installed.");
if (installButton) installButton.style.display = "none";
if (installButtonMobile) installButtonMobile.style.display = "none";
deferredPrompt = null;
});
// --- Install button click handlers ---
async function triggerInstall() {
if (!deferredPrompt) return;
deferredPrompt.prompt();
const { outcome } = await deferredPrompt.userChoice;
console.log(`[App] Install prompt outcome: ${outcome}`);
deferredPrompt = null;
if (installButton) installButton.style.display = "none";
if (installButtonMobile) installButtonMobile.style.display = "none";
}
if (installButton) installButton.addEventListener("click", triggerInstall);
if (installButtonMobile)
installButtonMobile.addEventListener("click", triggerInstall);
// --- Utility: detect standalone (installed) mode ---
function isStandalone() {
return (
window.matchMedia("(display-mode: standalone)").matches ||
window.navigator.standalone === true
);
}