Skip to content

Commit

Permalink
feat(i18n): add i18n aware service worker registriation in plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Monnet committed Jan 18, 2025
1 parent 68db259 commit b0e94d5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions configuration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ declare module 'virtual:nuxt-pwa-configuration' {
export const display: 'fullscreen' | 'standalone' | 'minimal-ui' | 'browser'
export const installPrompt: string | undefined
export const periodicSyncForUpdates: number
export const i18nSplitManifest: boolean
export const i18nSplitServiceWorker: boolean
}
12 changes: 10 additions & 2 deletions src/runtime/plugins/pwa.client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { nextTick, reactive, ref } from 'vue'
import type { UnwrapNestedRefs } from 'vue'
import { useRegisterSW } from 'virtual:pwa-register/vue'
import { display, installPrompt, periodicSyncForUpdates } from 'virtual:nuxt-pwa-configuration'
import { display, installPrompt, periodicSyncForUpdates, i18nSplitServiceWorker } from 'virtual:nuxt-pwa-configuration'
import type { BeforeInstallPromptEvent, PwaInjection, UserChoice } from './types'
import { defineNuxtPlugin } from '#imports'
import type { Plugin } from '#app'

const plugin: Plugin<{
pwa?: UnwrapNestedRefs<PwaInjection>
}> = defineNuxtPlugin(() => {
}> = defineNuxtPlugin(({$router, $config}) => {
const registrationError = ref(false)
const swActivated = ref(false)
const showInstallPrompt = ref(false)
Expand Down Expand Up @@ -50,10 +50,18 @@ const plugin: Plugin<{
}, timeout)
}

let baseUrl = ""
if (i18nSplitServiceWorker) {
const separator = $config.public.i18n.routesNameSeparator
const locale = $router.currentRoute.value.name.split(separator)[1]
baseUrl = $router.resolve({name:`index${separator}${locale}`}).path
}

const {
offlineReady, needRefresh, updateServiceWorker,
} = useRegisterSW({
immediate: true,
baseUrl: baseUrl,
onRegisterError() {
registrationError.value = true
},
Expand Down
2 changes: 2 additions & 0 deletions src/utils/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ export async function doSetup(options: PwaModuleOptions, nuxt: Nuxt) {
export const display = '${display}'
export const installPrompt = ${JSON.stringify(installPrompt)}
export const periodicSyncForUpdates = ${typeof client.periodicSyncForUpdates === 'number' ? client.periodicSyncForUpdates : 0}
export const i18nSplitManifest = ${options.i18n?.splitManifest}
export const i18nSplitServiceWorker = ${options.i18n?.splitServiceWorker}
`
}
},
Expand Down

0 comments on commit b0e94d5

Please sign in to comment.