-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwa-handler.js
38 lines (32 loc) · 1.22 KB
/
pwa-handler.js
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
// PWAs need HTTPS
if (location.protocol != "https:") location.protocol == "https";
// Reference the serviceWorker.
const serviceWorker = navigator.serviceWorker;
// Register our ServiceWorker script, if serviceWorker is available.
if (serviceWorker) {
serviceWorker
.register("./service-worker.js")
.then(() => console.log("ServiceWorker Registered to the Application!"))
.catch(() => console.log("Failed to Register the ServiceWorker."));
}
// Create a variable to defer the beforeinstallprompt event.
let beforeInstallEvent;
// Reference the install button from DOM.
const installButton = document.getElementById("installPWA");
// Watch for the beforeinstallevent and defer it.
window.addEventListener("beforeinstallprompt", (event) => {
event.preventDefault();
beforeInstallEvent = event;
installButton.style.display = "block";
});
// Prompt for Installation when install button is clicked.
installButton.addEventListener("click", () => {
beforeInstallEvent
.prompt()
.then((choice) => {
// Hide the install button as its purpose is over.
if (choice.outcome == "accepted") {
installButton.style.display = "none";
}
});
});