chore(pwa): improve DX for $pwa (#2498)

This commit is contained in:
Joaquín Sánchez 2023-12-09 17:04:41 +01:00 committed by GitHub
parent 3dbdb99118
commit 9155c32ece
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 17 deletions

View file

@ -1,7 +0,0 @@
export default defineNuxtPlugin(() => {
return {
provide: {
pwa: {},
},
}
})

View file

@ -1,4 +1,6 @@
import { useRegisterSW } from 'virtual:pwa-register/vue'
import type { UnwrapNestedRefs } from 'vue'
import type { PwaInjection } from './types'
import { STORAGE_KEY_PWA_HIDE_INSTALL } from '~/constants'
export default defineNuxtPlugin(() => {
@ -115,7 +117,7 @@ export default defineNuxtPlugin(() => {
needRefresh,
updateServiceWorker,
close,
}),
}) satisfies UnwrapNestedRefs<PwaInjection>,
},
}
})

28
modules/pwa/runtime/types.d.ts vendored Normal file
View file

@ -0,0 +1,28 @@
import type { Ref } from 'vue'
import type { UnwrapNestedRefs } from 'vue'
export interface PwaInjection {
isInstalled: boolean
showInstallPrompt: Ref<boolean>
cancelInstall: () => void
install: () => Promise<void>
swActivated: Ref<boolean>
registrationError: Ref<boolean>
needRefresh: Ref<boolean>
updateServiceWorker: (reloadPage?: boolean | undefined) => Promise<void>
close: () => Promise<void>
}
declare module '#app' {
interface NuxtApp {
$pwa?: UnwrapNestedRefs<PwaInjection>
}
}
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$pwa?: UnwrapNestedRefs<PwaInjection>
}
}
export {}