@d-gs/vue-pwa-install-prompt
TypeScript icon, indicating that this package has built-in type declarations

0.1.6 • Public • Published

vue-pwa-install-prompt

PWA Install Prompt For Vue3. (Not supported Vue2)

Install

npm i @d-gs/vue-pwa-install-prompt

Usage

In index.html

  <head>
    ...
    <link rel="manifest" href="/manifest.webmanifest" />
  </head>

In app.ts

import { pluginPwaInstallPrompt } from '@d-gs/vue-pwa-install-prompt'
app.use(pluginPwaInstallPrompt)

In App.vue

<script lang="ts" setup>
import { usePwaInstallPrompt } from '@d-gs/vue-pwa-install-prompt'
import type { BeforeInstallPromptEvent } from '@d-gs/vue-pwa-install-prompt'

const pwaInstallPrompt = usePwaInstallPrompt()

// Service Worker
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js')
    .then((reg) => {
      reg.onupdatefound = () => {
        reg.update()
      }
    })

  window.addEventListener('beforeinstallprompt', (event) => {
    event.preventDefault()
    pwaInstallPrompt.actions.setInstallPromptEvent(event as BeforeInstallPromptEvent)
  })
}
</script>

In Comp.vue

<script lang="ts" setup>
import { usePwaInstallPrompt } from '@d-gs/vue-pwa-install-prompt'

const pwaInstallPrompt = usePwaInstallPrompt()
const showPrompt = async () => {
  const outcome = await pwaInstallPrompt.actions.showPrompt()
  // outcome: 'accepted' | 'dismissed' | undefined

  if (outcome === 'accepted') {
    // Do something
    return
  }

  if (outcome === 'dismissed') {
    // Do something
    return
  }
}
</script>

Package Sidebar

Install

npm i @d-gs/vue-pwa-install-prompt

Weekly Downloads

3

Version

0.1.6

License

MIT

Unpacked Size

6.1 kB

Total Files

5

Last publish

Collaborators

  • d-gs