From d49da7dc3b565faf0293af1892eae913edba88dc Mon Sep 17 00:00:00 2001 From: Corentin Thomasset Date: Fri, 23 Jul 2021 18:46:09 +0200 Subject: [PATCH] fix(pwa): added reload on new version --- nuxt.config.js | 1 + plugins/pwa-update.plugin.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 plugins/pwa-update.plugin.ts diff --git a/nuxt.config.js b/nuxt.config.js index 48ed11b8..f95c9bdd 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -29,6 +29,7 @@ export default { // Plugins to run before rendering page (https://go.nuxtjs.dev/config-plugins) plugins: [ + {src: '~/plugins/pwa-update.plugin.ts', mode: 'client'}, '~/plugins/vuetify-toast' ], diff --git a/plugins/pwa-update.plugin.ts b/plugins/pwa-update.plugin.ts new file mode 100644 index 00000000..bd5a8d95 --- /dev/null +++ b/plugins/pwa-update.plugin.ts @@ -0,0 +1,26 @@ +import { Plugin } from '@nuxt/types' + +const pwaUpdatePlugin: Plugin = async () => { + // @ts-ignore + const workbox = await window.$workbox + + if (!workbox) { + // eslint-disable-next-line no-console + console.debug("Workbox couldn't be loaded.") + return + } + + workbox.addEventListener('installed', (event: { isUpdate: boolean }) => { + if (!event.isUpdate) { + // eslint-disable-next-line no-console + console.debug('The PWA is on the latest version.') + return + } + + // eslint-disable-next-line no-console + console.debug('There is an update for the PWA, reloading...') + window.location.reload() + }) +} + +export default pwaUpdatePlugin