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 <noreply@anthropic.com>
This commit is contained in:
Your Name 2026-01-19 04:18:39 +00:00
parent ba7e40fa20
commit a9ac9051c5
1 changed files with 3 additions and 38 deletions

View File

@ -22,27 +22,6 @@ export function isRunningAsPWA() {
return false; 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 deferredPrompt = null;
let promptShown = false; 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) => { window.addEventListener('beforeinstallprompt', (e) => {
// Ne rien faire si déjà en mode PWA
if (isRunningAsPWA()) { if (isRunningAsPWA()) {
console.log('Already running as PWA, skipping install prompt'); console.log('Already running as PWA, skipping install prompt');
return; return;
} }
// Ne rien faire si déjà installé
if (isAlreadyInstalled()) {
console.log('PWA already installed, skipping prompt');
return;
}
e.preventDefault(); e.preventDefault();
deferredPrompt = e; deferredPrompt = e;
console.log('PWA install prompt ready - waiting for manual click'); 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 // Écoute l'événement quand l'app est installée
window.addEventListener('appinstalled', () => { window.addEventListener('appinstalled', () => {
console.log('PWA was installed'); console.log('PWA was installed');
markAsInstalled();
deferredPrompt = null; deferredPrompt = null;
}); });
@ -114,9 +84,6 @@ function showInstallPrompt() {
deferredPrompt.userChoice.then((choice) => { deferredPrompt.userChoice.then((choice) => {
console.log('Install outcome:', choice.outcome); console.log('Install outcome:', choice.outcome);
if (choice.outcome === 'accepted') {
markAsInstalled();
}
deferredPrompt = null; deferredPrompt = null;
}); });
} }
@ -144,9 +111,6 @@ export function promptInstall() {
deferredPrompt.userChoice.then((choice) => { deferredPrompt.userChoice.then((choice) => {
console.log('Install outcome:', choice.outcome); console.log('Install outcome:', choice.outcome);
if (choice.outcome === 'accepted') {
markAsInstalled();
}
deferredPrompt = null; deferredPrompt = null;
}); });
return true; return true;
@ -163,9 +127,10 @@ export function promptInstall() {
/** /**
* Vérifie si le prompt d'installation est disponible * Vérifie si le prompt d'installation est disponible
* Le navigateur gère automatiquement: beforeinstallprompt ne fire que si PAS installé
*/ */
export function canInstall() { export function canInstall() {
if (isRunningAsPWA() || isAlreadyInstalled()) return false; if (isRunningAsPWA()) return false;
if (isIOS() && isSafari()) return true; if (isIOS() && isSafari()) return true;
return deferredPrompt !== null; return deferredPrompt !== null;
} }