feat: Implement comprehensive PWA functionality
- Add full PWA configuration with manifest and service worker - Create PWAInstallBanner component with mobile detection - Implement install banner on login page (shows below login form) - Add usePWA composable for reusable PWA functionality - Configure offline support with Workbox caching strategies - Add PWA initialization plugin - Update app name to 'Port Nimara Portal' throughout - Use circular logo in install banner and instructions - Banner shows only once and hides if already installed - Support both Android (direct install) and iOS (manual instructions) - Add comprehensive documentation for PWA implementation Features: - Mobile-only install banner with dismissal tracking - Standalone mode detection to hide banner when installed - Platform-specific installation instructions - Offline functionality with API caching - Auto-updating service worker - Native app-like experience when installed
This commit is contained in:
101
composables/usePWA.ts
Normal file
101
composables/usePWA.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
export const usePWA = () => {
|
||||
const isInstalled = ref(false);
|
||||
const isInstallable = ref(false);
|
||||
const deferredPrompt = ref<any>(null);
|
||||
|
||||
// Check if app is installed (running in standalone mode)
|
||||
const checkInstallStatus = () => {
|
||||
if (process.client) {
|
||||
isInstalled.value = window.matchMedia('(display-mode: standalone)').matches ||
|
||||
(window.navigator as any).standalone ||
|
||||
document.referrer.includes('android-app://');
|
||||
}
|
||||
};
|
||||
|
||||
// Check if device is mobile
|
||||
const isMobile = computed(() => {
|
||||
if (process.client) {
|
||||
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
// Check if device is iOS
|
||||
const isIOS = computed(() => {
|
||||
if (process.client) {
|
||||
return /iPad|iPhone|iPod/.test(navigator.userAgent);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
// Check if device is Android
|
||||
const isAndroid = computed(() => {
|
||||
if (process.client) {
|
||||
return /Android/i.test(navigator.userAgent);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
// Install the app
|
||||
const install = async () => {
|
||||
if (deferredPrompt.value) {
|
||||
deferredPrompt.value.prompt();
|
||||
const choiceResult = await deferredPrompt.value.userChoice;
|
||||
|
||||
if (choiceResult.outcome === 'accepted') {
|
||||
console.log('User accepted the install prompt');
|
||||
deferredPrompt.value = null;
|
||||
isInstallable.value = false;
|
||||
return true;
|
||||
} else {
|
||||
console.log('User dismissed the install prompt');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// Check if app can be installed
|
||||
const canInstall = computed(() => {
|
||||
return isMobile.value && !isInstalled.value && (isInstallable.value || isIOS.value);
|
||||
});
|
||||
|
||||
// Initialize PWA functionality
|
||||
const init = () => {
|
||||
if (process.client) {
|
||||
checkInstallStatus();
|
||||
|
||||
// Listen for the beforeinstallprompt event
|
||||
window.addEventListener('beforeinstallprompt', (e) => {
|
||||
console.log('beforeinstallprompt event fired');
|
||||
e.preventDefault();
|
||||
deferredPrompt.value = e;
|
||||
isInstallable.value = true;
|
||||
});
|
||||
|
||||
// Listen for successful app installation
|
||||
window.addEventListener('appinstalled', () => {
|
||||
console.log('PWA was installed');
|
||||
isInstalled.value = true;
|
||||
isInstallable.value = false;
|
||||
deferredPrompt.value = null;
|
||||
});
|
||||
|
||||
// Listen for display mode changes
|
||||
window.matchMedia('(display-mode: standalone)').addEventListener('change', (e) => {
|
||||
isInstalled.value = e.matches;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
isInstalled: readonly(isInstalled),
|
||||
isInstallable: readonly(isInstallable),
|
||||
isMobile,
|
||||
isIOS,
|
||||
isAndroid,
|
||||
canInstall,
|
||||
install,
|
||||
init
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user