feat: implement PWA update prompt and manual reload
This commit is contained in:
@@ -9,6 +9,7 @@ import GuidePanel from './components/GuidePanel.vue';
|
|||||||
import WinModal from './components/WinModal.vue';
|
import WinModal from './components/WinModal.vue';
|
||||||
import CustomGameModal from './components/CustomGameModal.vue';
|
import CustomGameModal from './components/CustomGameModal.vue';
|
||||||
import FixedBar from './components/FixedBar.vue';
|
import FixedBar from './components/FixedBar.vue';
|
||||||
|
import ReloadPrompt from './components/ReloadPrompt.vue';
|
||||||
|
|
||||||
// Main App Entry
|
// Main App Entry
|
||||||
const store = usePuzzleStore();
|
const store = usePuzzleStore();
|
||||||
@@ -173,6 +174,7 @@ onUnmounted(() => {
|
|||||||
<Teleport to="body">
|
<Teleport to="body">
|
||||||
<WinModal v-if="store.isGameWon" />
|
<WinModal v-if="store.isGameWon" />
|
||||||
<CustomGameModal v-if="showCustomModal" @close="showCustomModal = false" />
|
<CustomGameModal v-if="showCustomModal" @close="showCustomModal = false" />
|
||||||
|
<ReloadPrompt />
|
||||||
</Teleport>
|
</Teleport>
|
||||||
</main>
|
</main>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
110
src/components/ReloadPrompt.vue
Normal file
110
src/components/ReloadPrompt.vue
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
<script setup>
|
||||||
|
import { useRegisterSW } from 'virtual:pwa-register/vue'
|
||||||
|
import { useI18n } from '@/composables/useI18n'
|
||||||
|
|
||||||
|
const { t } = useI18n()
|
||||||
|
|
||||||
|
const {
|
||||||
|
offlineReady,
|
||||||
|
needRefresh,
|
||||||
|
updateServiceWorker,
|
||||||
|
} = useRegisterSW()
|
||||||
|
|
||||||
|
const close = async () => {
|
||||||
|
offlineReady.value = false
|
||||||
|
needRefresh.value = false
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div
|
||||||
|
v-if="offlineReady || needRefresh"
|
||||||
|
class="pwa-toast"
|
||||||
|
role="alert"
|
||||||
|
>
|
||||||
|
<div class="message">
|
||||||
|
<span v-if="offlineReady">
|
||||||
|
{{ t('pwa.offlineReady') }}
|
||||||
|
</span>
|
||||||
|
<span v-else>
|
||||||
|
{{ t('pwa.newContent') }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div class="buttons">
|
||||||
|
<button v-if="needRefresh" class="btn-neon small" @click="updateServiceWorker()">
|
||||||
|
{{ t('pwa.reload') }}
|
||||||
|
</button>
|
||||||
|
<button class="close-btn" @click="close">
|
||||||
|
{{ t('pwa.close') }}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.pwa-toast {
|
||||||
|
position: fixed;
|
||||||
|
right: 0;
|
||||||
|
bottom: 60px; /* Above the footer */
|
||||||
|
margin: 16px;
|
||||||
|
padding: 15px;
|
||||||
|
border: 1px solid var(--banner-border);
|
||||||
|
background: var(--banner-bg);
|
||||||
|
border-radius: 12px;
|
||||||
|
z-index: 2000;
|
||||||
|
text-align: left;
|
||||||
|
box-shadow: var(--banner-shadow);
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 10px;
|
||||||
|
backdrop-filter: blur(10px);
|
||||||
|
color: var(--text-color);
|
||||||
|
max-width: 320px;
|
||||||
|
animation: slideIn 0.3s ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes slideIn {
|
||||||
|
from {
|
||||||
|
transform: translateY(20px);
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
transform: translateY(0);
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.message {
|
||||||
|
font-size: 0.95rem;
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.buttons {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: flex-end;
|
||||||
|
margin-top: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.close-btn {
|
||||||
|
background: transparent;
|
||||||
|
border: 1px solid var(--text-muted);
|
||||||
|
color: var(--text-muted);
|
||||||
|
padding: 6px 12px;
|
||||||
|
border-radius: 20px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
transition: all 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.close-btn:hover {
|
||||||
|
border-color: var(--text-color);
|
||||||
|
color: var(--text-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-neon.small {
|
||||||
|
padding: 6px 16px;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -42,6 +42,10 @@ const messages = {
|
|||||||
'pwa.installTitle': 'Zainstaluj aplikację i graj offline',
|
'pwa.installTitle': 'Zainstaluj aplikację i graj offline',
|
||||||
'pwa.installMobile': 'Dodaj do ekranu głównego',
|
'pwa.installMobile': 'Dodaj do ekranu głównego',
|
||||||
'pwa.installDesktop': 'Zainstaluj na komputerze',
|
'pwa.installDesktop': 'Zainstaluj na komputerze',
|
||||||
|
'pwa.offlineReady': 'Aplikacja gotowa do pracy offline',
|
||||||
|
'pwa.newContent': 'Dostępna nowa wersja, odśwież aby zaktualizować',
|
||||||
|
'pwa.reload': 'Odśwież',
|
||||||
|
'pwa.close': 'Zamknij',
|
||||||
'language.label': 'Wybór języka',
|
'language.label': 'Wybór języka',
|
||||||
'language.pl': 'Polski',
|
'language.pl': 'Polski',
|
||||||
'language.en': 'Angielski',
|
'language.en': 'Angielski',
|
||||||
@@ -137,6 +141,10 @@ const messages = {
|
|||||||
'pwa.installTitle': 'Install the app and play offline',
|
'pwa.installTitle': 'Install the app and play offline',
|
||||||
'pwa.installMobile': 'Add to home screen',
|
'pwa.installMobile': 'Add to home screen',
|
||||||
'pwa.installDesktop': 'Install on desktop',
|
'pwa.installDesktop': 'Install on desktop',
|
||||||
|
'pwa.offlineReady': 'App ready to work offline',
|
||||||
|
'pwa.newContent': 'New content available, click on reload button to update',
|
||||||
|
'pwa.reload': 'Reload',
|
||||||
|
'pwa.close': 'Close',
|
||||||
'language.label': 'Language selection',
|
'language.label': 'Language selection',
|
||||||
'language.pl': 'Polish',
|
'language.pl': 'Polish',
|
||||||
'language.en': 'English',
|
'language.en': 'English',
|
||||||
|
|||||||
20
src/main.js
20
src/main.js
@@ -31,23 +31,3 @@ app.directive('cell-hover', vCellHover)
|
|||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
|
|
||||||
if ('serviceWorker' in navigator) {
|
|
||||||
let refreshing = false
|
|
||||||
const triggerReload = () => {
|
|
||||||
if (refreshing) return
|
|
||||||
refreshing = true
|
|
||||||
window.location.reload()
|
|
||||||
}
|
|
||||||
navigator.serviceWorker.addEventListener('controllerchange', triggerReload)
|
|
||||||
const checkForUpdate = () => {
|
|
||||||
navigator.serviceWorker.getRegistration().then((registration) => {
|
|
||||||
if (registration) {
|
|
||||||
registration.update()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
window.addEventListener('visibilitychange', () => {
|
|
||||||
if (document.visibilityState === 'visible') checkForUpdate()
|
|
||||||
})
|
|
||||||
window.addEventListener('focus', checkForUpdate)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -10,12 +10,11 @@ export default defineConfig({
|
|||||||
plugins: [
|
plugins: [
|
||||||
vue(),
|
vue(),
|
||||||
VitePWA({
|
VitePWA({
|
||||||
registerType: 'autoUpdate',
|
registerType: 'prompt',
|
||||||
injectRegister: 'auto',
|
injectRegister: 'auto',
|
||||||
workbox: {
|
workbox: {
|
||||||
cleanupOutdatedCaches: true,
|
cleanupOutdatedCaches: true,
|
||||||
skipWaiting: true,
|
globPatterns: ['**/*.{js,css,html,ico,png,svg,json,vue,txt,woff2}']
|
||||||
clientsClaim: true,
|
|
||||||
},
|
},
|
||||||
devOptions: {
|
devOptions: {
|
||||||
enabled: true
|
enabled: true
|
||||||
|
|||||||
Reference in New Issue
Block a user