-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.vue
64 lines (55 loc) · 1.42 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<template>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
<style>
.page-enter-active,
.page-leave-active {
transition: all 0.075s;
}
.page-enter-from,
.page-leave-to {
opacity: 0;
}
</style>
<script setup>
let deferredPrompt;
const userPrefs = useUserPrefsStore();
const installPWA = () => {
if (deferredPrompt) {
deferredPrompt.prompt();
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the A2HS prompt');
} else {
console.log('User dismissed the A2HS prompt');
userPrefs.installDeniedCount++;
}
deferredPrompt = null;
});
}
};
const handleFirstClick = () => {
installPWA();
window.removeEventListener('click', handleFirstClick); // Remove listener after first click
};
onMounted(() => {
if (window.matchMedia('(display-mode: standalone)').matches) {
console.log('Launched: Installed');
} else {
console.log('Launched: Browser');
if ('serviceWorker' in navigator && userPrefs.installDeniedCount < 3) {
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
deferredPrompt = e;
// Bind the install prompt to the first click
window.addEventListener('click', handleFirstClick);
});
}
}
});
onBeforeUnmount(() => {
window.removeEventListener('click', handleFirstClick); // Clean up on unmount
});
</script>