Skip to content

Commit

Permalink
feat: allow Fluidd to run fully offline (#986)
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <[email protected]>
  • Loading branch information
pedrolamas authored Dec 23, 2022
1 parent 78f2780 commit e91af48
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
1 change: 1 addition & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ declare module 'vue' {
FlashMessage: typeof import('./src/components/common/FlashMessage.vue')['default']
KlippyStatusCard: typeof import('./src/components/common/KlippyStatusCard.vue')['default']
ManualProbeDialog: typeof import('./src/components/common/ManualProbeDialog.vue')['default']
RegisterServiceWorker: typeof import('./src/components/common/RegisterServiceWorker.vue')['default']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
SocketDisconnected: typeof import('./src/components/common/SocketDisconnected.vue')['default']
Expand Down
2 changes: 2 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
(!authenticated && apiConnected)
"
/>

<register-service-worker />
</v-container>

<socket-disconnected
Expand Down
79 changes: 79 additions & 0 deletions src/components/common/RegisterServiceWorker.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<template>
<div>
<v-snackbar
v-model="needRefresh"
timeout="-1"
multi-line
elevation="24"
bottom
right
>
<span v-html="$t('app.general.msg.needs_refresh')" />
<template #action="{ attrs }">
<app-btn
v-bind="attrs"
@click="updateServiceWorker"
>
{{ $t('app.general.btn.reload') }}
</app-btn>
</template>
</v-snackbar>
</div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import consola from 'consola'
import { EventBus } from '@/eventBus'
@Component({})
export default class RegisterServiceWorker extends Vue {
updateSW: ((reloadPage?: boolean) => Promise<void>) | null = null
needRefresh = false
onOfflineReady () {
consola.debug('[PWA] ready for offline work')
EventBus.$emit(this.$tc('app.general.msg.offline_ready'), { timeout: 5000 })
}
onNeedRefresh () {
consola.debug('[PWA] needs refresh')
this.needRefresh = true
}
onRegistered (registration?: ServiceWorkerRegistration) {
consola.debug('[PWA] registered', registration)
}
onRegisterError (e: any) {
consola.error('[PWA] registration error', e)
}
updateServiceWorker () {
this.updateSW && this.updateSW(true)
}
async mounted () {
try {
const { registerSW } = await import('virtual:pwa-register')
this.updateSW = registerSW({
immediate: true,
onOfflineReady: this.onOfflineReady,
onNeedRefresh: this.onNeedRefresh,
onRegistered: this.onRegistered,
onRegisterError: this.onRegisterError
})
} catch {
consola.error('[PWA] disabled')
}
}
}
</script>

<style lang="scss" scoped>
:deep(.v-snack__wrapper .v-snack__content) {
overflow: hidden;
overflow-wrap: break-word;
}
</style>
3 changes: 3 additions & 0 deletions src/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ app:
reboot: Reboot
recover: Recover
refresh: Refresh
reload: Reload
remove: Remove
remove_all: Remove All
rename: Rename
Expand Down Expand Up @@ -308,6 +309,8 @@ app:
Click <b>Adjusted</b> if a significant adjustment is necessary on the current screw; otherwise, click <b>Accept</b> to continue.
welcome_back: >-
Welcome back.<br>Sign in below to stay in touch with your printer.
offline_ready: Fluidd is now ready to work offline.
needs_refresh: New content available, please click the <b>Reload</b> button to update.
simple_form:
error:
arrayofnums: Only numbers
Expand Down
21 changes: 18 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import version from './vite.config.inject-version'
export default defineConfig({
plugins: [
VitePWA({
registerType: 'autoUpdate',
includeAssets: [
'**/*.svg',
'**/*.png',
Expand All @@ -23,13 +22,28 @@ export default defineConfig({
],
workbox: {
globPatterns: [
'**/*.{js,css,html,ttf,woff,woff2}'
'**/*.{js,css,html,ttf,woff,woff2,wasm}'
],
maximumFileSizeToCacheInBytes: 4 * 1024 ** 2,
navigateFallbackDenylist: [
/^\/websocket/,
/^\/(printer|api|access|machine|server)\//,
/^\/webcam[2-4]?\//
],
runtimeCaching: [
{
urlPattern: (options) => (options.sameOrigin && options.url.pathname.startsWith('/config.json')),
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'config',
matchOptions: {
ignoreSearch: true
},
precacheFallback: {
fallbackURL: 'config.json'
}
}
}
]
},
manifest: {
Expand Down Expand Up @@ -64,7 +78,8 @@ export default defineConfig({
]
},
devOptions: {
enabled: true
enabled: true,
type: 'module'
}
}),
vue(),
Expand Down

0 comments on commit e91af48

Please sign in to comment.