From a9ac9051c5fdaf1a7e92339cb17292af1d0fd1b7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 19 Jan 2026 04:18:39 +0000 Subject: [PATCH] fix: simplify PWA install detection to use browser native API - Remove localStorage/cookie tracking for installed state - Rely on browser's beforeinstallprompt event (only fires if not installed) - Use display-mode: standalone to detect running as PWA Co-Authored-By: Claude Opus 4.5 --- src/pwaInstall.js | 41 +++-------------------------------------- 1 file changed, 3 insertions(+), 38 deletions(-) diff --git a/src/pwaInstall.js b/src/pwaInstall.js index c934048..a549b53 100644 --- a/src/pwaInstall.js +++ b/src/pwaInstall.js @@ -22,27 +22,6 @@ export function isRunningAsPWA() { return false; } -/** - * Vérifie si l'installation a déjà été faite (localStorage) - */ -function isAlreadyInstalled() { - try { - return localStorage.getItem('pwa-installed') === 'true'; - } catch { - return false; - } -} - -/** - * Marque l'app comme installée dans localStorage - */ -function markAsInstalled() { - try { - localStorage.setItem('pwa-installed', 'true'); - } catch (e) { - console.warn('Could not save PWA install state:', e); - } -} let deferredPrompt = null; let promptShown = false; @@ -74,30 +53,21 @@ function showIOSInstallHint() { ); } -// Écoute l'événement d'installation +// Écoute l'événement d'installation (le navigateur ne fire que si PAS installé) window.addEventListener('beforeinstallprompt', (e) => { - // Ne rien faire si déjà en mode PWA if (isRunningAsPWA()) { console.log('Already running as PWA, skipping install prompt'); return; } - // Ne rien faire si déjà installé - if (isAlreadyInstalled()) { - console.log('PWA already installed, skipping prompt'); - return; - } - e.preventDefault(); deferredPrompt = e; console.log('PWA install prompt ready - waiting for manual click'); - // Pas de prompt automatique - l'utilisateur cliquera sur le bouton s'il veut installer }); // Écoute l'événement quand l'app est installée window.addEventListener('appinstalled', () => { console.log('PWA was installed'); - markAsInstalled(); deferredPrompt = null; }); @@ -114,9 +84,6 @@ function showInstallPrompt() { deferredPrompt.userChoice.then((choice) => { console.log('Install outcome:', choice.outcome); - if (choice.outcome === 'accepted') { - markAsInstalled(); - } deferredPrompt = null; }); } @@ -144,9 +111,6 @@ export function promptInstall() { deferredPrompt.userChoice.then((choice) => { console.log('Install outcome:', choice.outcome); - if (choice.outcome === 'accepted') { - markAsInstalled(); - } deferredPrompt = null; }); return true; @@ -163,9 +127,10 @@ export function promptInstall() { /** * Vérifie si le prompt d'installation est disponible + * Le navigateur gère automatiquement: beforeinstallprompt ne fire que si PAS installé */ export function canInstall() { - if (isRunningAsPWA() || isAlreadyInstalled()) return false; + if (isRunningAsPWA()) return false; if (isIOS() && isSafari()) return true; return deferredPrompt !== null; }