add PWA utility functions
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
// Fichier d'initialisation PWA à inclure dans toutes les pages
|
||||
function addPWAHeaders() {
|
||||
// Meta tags PWA
|
||||
echo '<meta name="mobile-web-app-capable" content="yes">' . "\n";
|
||||
echo '<meta name="apple-mobile-web-app-capable" content="yes">' . "\n";
|
||||
echo '<meta name="apple-mobile-web-app-status-bar-style" content="default">' . "\n";
|
||||
echo '<meta name="apple-mobile-web-app-title" content="kaubuntu.re">' . "\n";
|
||||
echo '<meta name="application-name" content="kaubuntu.re">' . "\n";
|
||||
echo '<meta name="msapplication-TileColor" content="#FF0000">' . "\n";
|
||||
echo '<meta name="msapplication-config" content="browserconfig.xml">' . "\n";
|
||||
echo '<meta name="theme-color" content="#FF0000">' . "\n";
|
||||
|
||||
// Manifest
|
||||
echo '<link rel="manifest" href="site.webmanifest">' . "\n";
|
||||
}
|
||||
|
||||
function addPWAScripts() {
|
||||
?>
|
||||
<!-- PWA Service Worker -->
|
||||
<script>
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', function() {
|
||||
navigator.serviceWorker.register('/sw.js')
|
||||
.then(function(registration) {
|
||||
console.log('Service Worker enregistré avec succès:', registration.scope);
|
||||
|
||||
// Écouter les mises à jour
|
||||
registration.addEventListener('updatefound', function() {
|
||||
const newWorker = registration.installing;
|
||||
newWorker.addEventListener('statechange', function() {
|
||||
if (newWorker.state === 'installed' && navigator.serviceWorker.controller) {
|
||||
// Nouvelle version disponible
|
||||
console.log('Nouvelle version disponible');
|
||||
if (confirm('Une nouvelle version est disponible. Voulez-vous recharger la page ?')) {
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log('Échec de l\'enregistrement du Service Worker:', err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Gestion de l'installation PWA
|
||||
let deferredPrompt;
|
||||
const installButton = document.getElementById('install-pwa');
|
||||
|
||||
window.addEventListener('beforeinstallprompt', function(e) {
|
||||
e.preventDefault();
|
||||
deferredPrompt = e;
|
||||
|
||||
// Afficher le bouton d'installation s'il existe
|
||||
if (installButton) {
|
||||
installButton.style.display = 'block';
|
||||
installButton.addEventListener('click', function() {
|
||||
deferredPrompt.prompt();
|
||||
deferredPrompt.userChoice.then(function(choiceResult) {
|
||||
if (choiceResult.outcome === 'accepted') {
|
||||
console.log('PWA installée');
|
||||
}
|
||||
deferredPrompt = null;
|
||||
installButton.style.display = 'none';
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Masquer le bouton après installation
|
||||
window.addEventListener('appinstalled', function() {
|
||||
console.log('PWA installée avec succès');
|
||||
if (installButton) {
|
||||
installButton.style.display = 'none';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user