sw-fe/public/pwaInstall.js

26 lines
896 B
JavaScript

// src/pwaInstall.js
let deferredPrompt = null;
let promptShown = false;
// Stocke l'événement natif (se déclenche après interactions)
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
deferredPrompt = e;
console.log('PWA: Install prompt ready - button can trigger it now');
});
// Fonction appelée par le bouton "Install"
export function promptInstall() {
if (deferredPrompt && !promptShown) {
console.log('PWA: Triggering native prompt');
deferredPrompt.prompt();
promptShown = true;
deferredPrompt.userChoice.then((choiceResult) => {
console.log('PWA Install outcome:', choiceResult.outcome);
deferredPrompt = null;
});
} else {
console.log('PWA: No prompt ready - scroll/click more on the site');
alert('PWA not ready yet. Try scrolling the map, clicking filters or markers, then click Install again.');
}
}