Skip to content

Commit d5a3120

Browse files
authored
Add install button for PWA using deferredPrompt (#5406)
* Expose PWA install flow using deferredPrompt * Fix install button icon
1 parent cc3bc70 commit d5a3120

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

index.html

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,12 @@
391391
<i class="material-icons md-48">help</i>
392392
</a>
393393
</li>
394+
<li>
395+
<a id="installButton" class="tooltipped hidden" data-position="bottom"
396+
data-tooltip="Install App" aria-label="Install App">
397+
<i class="material-icons md-48">file_download</i>
398+
</a>
399+
</li>
394400
</ul>
395401
</div>
396402
<div class="nav-wrapper" id="aux-toolbar" style="display: none;" tabindex="-1">
@@ -568,8 +574,10 @@
568574
// Detect if running on localhost for development
569575
const isLocalDev = window.location.hostname === "localhost" ||
570576
window.location.hostname === "127.0.0.1";
577+
// Localhost SW is disabled to avoid caching; set allowLocalPwa to test installs.
578+
const allowLocalPwa = localStorage.getItem("allowLocalPwa") === "true";
571579

572-
if (!isLocalDev) {
580+
if (!isLocalDev || allowLocalPwa) {
573581
// Production: Register service worker for offline mode
574582
if (navigator.serviceWorker.controller) {
575583
console.debug(
@@ -594,6 +602,9 @@
594602
console.log(
595603
"🔥 [DEV MODE] Service worker disabled on localhost for instant code updates"
596604
);
605+
console.log(
606+
"Set localStorage.allowLocalPwa = 'true' to enable PWA install testing on localhost."
607+
);
597608
}
598609
}
599610
</script>
@@ -604,10 +615,33 @@
604615
event.preventDefault(); // Prevent automatic browser prompt
605616
deferredPrompt = event;
606617

607-
// Show your install button here
608-
const divInstall = document.getElementById("installButton");
609-
if (divInstall) {
610-
divInstall.classList.remove("hidden");
618+
// Show the install button
619+
const installButton = document.getElementById("installButton");
620+
if (installButton) {
621+
installButton.classList.remove("hidden");
622+
}
623+
});
624+
625+
// Handle install button click
626+
document.addEventListener("DOMContentLoaded", function () {
627+
const installButton = document.getElementById("installButton");
628+
if (installButton) {
629+
installButton.addEventListener("click", async function () {
630+
if (deferredPrompt) {
631+
// Show the install prompt
632+
deferredPrompt.prompt();
633+
634+
// Wait for user choice
635+
const { outcome } = await deferredPrompt.userChoice;
636+
console.log(`User response to the install prompt: ${outcome}`);
637+
638+
// Clear the deferredPrompt
639+
deferredPrompt = null;
640+
641+
// Hide the button
642+
installButton.classList.add("hidden");
643+
}
644+
});
611645
}
612646
});
613647
</script>
@@ -803,6 +837,12 @@
803837
document.addEventListener('MSFullscreenChange', handleFullscreenChange);
804838
</script>
805839

840+
<style>
841+
#installButton.hidden {
842+
display: none !important;
843+
}
844+
</style>
845+
806846
<script>
807847
document.addEventListener('DOMContentLoaded', function () {
808848
let persistentNotification = null;

0 commit comments

Comments
 (0)