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:
parent
ba7e40fa20
commit
a9ac9051c5
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue