/** * PWA Installation and Service Worker Registration */ (function () { 'use strict'; // ==================== SERVICE WORKER REGISTRATION ==================== if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/service-worker.js') .then((registration) => { console.log('✅ Service Worker registered:', registration.scope); // Check for updates registration.addEventListener('updatefound', () => { const newWorker = registration.installing; console.log('🔄 Service Worker update found'); newWorker.addEventListener('statechange', () => { if (newWorker.state === 'installed' && navigator.serviceWorker.controller) { // New version available showUpdateNotification(); } }); }); }) .catch((error) => { console.error('❌ Service Worker registration failed:', error); }); }); } // ==================== PWA INSTALL PROMPT ==================== let deferredPrompt; let installButton; // Listen for install prompt event window.addEventListener('beforeinstallprompt', (e) => { console.log('💾 Install prompt triggered'); // Prevent Chrome 67 and earlier from automatically showing the prompt e.preventDefault(); // Store the event for later use deferredPrompt = e; // Show install button showInstallButton(); }); // Create and show install button function showInstallButton() { // Check if already installed if (window.matchMedia('(display-mode: standalone)').matches) { console.log('Already installed as PWA'); return; } // Check if button already exists if (document.getElementById('pwa-install-btn')) return; // Create install button installButton = document.createElement('button'); installButton.id = 'pwa-install-btn'; installButton.className = 'pwa-install-button'; installButton.innerHTML = ` Install App `; installButton.addEventListener('click', handleInstallClick); // Add to page document.body.appendChild(installButton); // Fade in animation setTimeout(() => { installButton.classList.add('visible'); }, 100); } // Handle install button click async function handleInstallClick() { if (!deferredPrompt) return; // Show install prompt deferredPrompt.prompt(); // Wait for user choice const { outcome } = await deferredPrompt.userChoice; console.log(`User response to install prompt: ${outcome}`); if (outcome === 'accepted') { console.log('✅ PWA installed'); hideInstallButton(); } else { console.log('❌ PWA installation declined'); } // Clear the deferredPrompt deferredPrompt = null; } // Hide install button function hideInstallButton() { if (installButton) { installButton.classList.remove('visible'); setTimeout(() => { if (installButton && installButton.parentNode) { installButton.remove(); } }, 300); } } // ==================== DETECT PWA MODE ==================== window.addEventListener('DOMContentLoaded', () => { // Check if running as installed PWA const isStandalone = window.matchMedia('(display-mode: standalone)').matches || window.navigator.standalone || document.referrer.includes('android-app://'); if (isStandalone) { console.log('🚀 Running as PWA'); document.body.classList.add('pwa-mode'); // Add iOS status bar spacing if (navigator.userAgent.match(/iPhone|iPad|iPod/)) { document.body.classList.add('ios-pwa'); } } else { console.log('🌐 Running in browser'); } }); // ==================== UPDATE NOTIFICATION ==================== function showUpdateNotification() { // Check if notification already exists if (document.getElementById('pwa-update-notification')) return; const notification = document.createElement('div'); notification.id = 'pwa-update-notification'; notification.className = 'pwa-update-notification'; notification.innerHTML = `
Click to update and get the latest features
Install this app on your iPhone: