Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add PWA caching and cache updater #1421

Merged
merged 8 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@
<router-view></router-view>
</v-container>
</v-main>
<the-update-dialog></the-update-dialog>
<the-editor></the-editor>
<the-timelapse-rendering-snackbar></the-timelapse-rendering-snackbar>
<the-fullscreen-upload></the-fullscreen-upload>
<the-upload-snackbar></the-upload-snackbar>
<the-service-worker />
<the-update-dialog />
<the-editor />
<the-timelapse-rendering-snackbar />
<the-fullscreen-upload />
<the-upload-snackbar />
<the-manual-probe-dialog />
<the-bed-screws-dialog />
<the-screws-tilt-adjust-dialog />
Expand Down
60 changes: 60 additions & 0 deletions src/components/TheServiceWorker.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<template>
<v-dialog v-model="showDialog" persistent max-width="400" class="mx-0">
<panel
:title="$t('App.TheServiceWorker.TitleNeedUpdate')"
card-class="service-worker-dialog"
:margin-bottom="false">
<v-card-text>
<p>{{ $t('App.TheServiceWorker.DescriptionNeedUpdate') }}</p>
</v-card-text>
<v-card-actions>
<v-spacer />
<v-btn text color="primary" @click="update">update</v-btn>
meteyou marked this conversation as resolved.
Show resolved Hide resolved
</v-card-actions>
</panel>
</v-dialog>
</template>
<script lang="ts">
import Component from 'vue-class-component'
import { Mixins } from 'vue-property-decorator'
import BaseMixin from '@/components/mixins/base'

@Component
export default class TheServiceWorker extends Mixins(BaseMixin) {
showDialog = false
updateSW: ((reloadPage?: boolean | undefined) => Promise<void>) | null = null

onOfflineReady() {
window.console.info('PWA is offline ready')
}

onNeedRefresh() {
window.console.warn('PWA needs to refresh')
this.showDialog = true
}

onRegistered() {
window.console.debug('PWA is registered')
}

onRegisterError(error: any) {
window.console.error('PWA registration error:', error)
}

update() {
this.updateSW?.(true)
this.showDialog = false
}

async mounted() {
const { registerSW } = await import('virtual:pwa-register')
this.updateSW = registerSW({
immediate: true,
onOfflineReady: this.onOfflineReady,
onNeedRefresh: this.onNeedRefresh,
onRegistered: this.onRegistered,
onRegisterError: this.onRegisterError,
})
}
}
</script>
4 changes: 4 additions & 0 deletions src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"NoEmptyAllowedError": "Feld darf nicht leer sein!"
},
"Printers": "Drucker",
"TheServiceWorker": {
"TitleNeedUpdate": "PWA benötigt ein Update",
"DescriptionNeedUpdate": "Der lokale Cache ist veraltet und muss aktualisiert werden. Bitte klicke auf den Button unten, um den Cache zu aktualisieren."
},
"ThrottledStates": {
"DescriptionCurrentlyThrottled": "rPi ARM-Kern(e) sind derzeit gedrosselt.",
"DescriptionFrequencyCapped": "rPi ARM max Frequenz ist derzeit auf 1,2 GHz begrenzt.",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"NoEmptyAllowedError": "Input must not be empty!"
},
"Printers": "Printers",
"TheServiceWorker": {
"TitleNeedUpdate": "PWA needs update",
"DescriptionNeedUpdate": "The local cache is outdated and needs to be updated. Please click on the button below to update the cache."
},
"ThrottledStates": {
"DescriptionCurrentlyThrottled": "rPi ARM core(s) are currently throttled down.",
"DescriptionFrequencyCapped": "rPi ARM max frequency is currently limited to 1.2 GHz.",
Expand Down
6 changes: 0 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import i18n from '@/plugins/i18n'
import store from '@/store'
import router from '@/plugins/router'
import { WebSocketPlugin } from '@/plugins/webSocketClient'
import { registerSW } from 'virtual:pwa-register'

// noinspection JSUnusedGlobalSymbols
registerSW({
onOfflineReady() {},
})

Vue.config.productionTip = false

Expand Down
14 changes: 14 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,23 @@ const PWAConfig: Partial<VitePWAOptions> = {
},
],
},
workbox: {
globPatterns: ['**/*.{js,css,html,woff,woff2,png,svg}'],
navigateFallbackDenylist: [/^\/(access|api|printer|server|websocket)/, /^\/webcam[2-4]?/],
runtimeCaching: [
{
urlPattern: (options) => options.url.pathname.startsWith('/config.json'),
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'config.json',
},
},
],
},
/* enable sw on development */
devOptions: {
enabled: true,
type: 'module',
},
}

Expand Down