vue-pwa-install

Vue PWA Install
10,259 17
Install
npm install vue-pwa-install
Latest Version:1.1.0
Vue:2
License:MIT
Last Updated:Mar 4, 2023
Links: GitHub  ·  npm
Maintainer: bartozzz

Vue PWA Install

Default CI/CD Known Vulnerabilities npm version npm dependency Status npm downloads

This library allows you to listen for beforeinstallprompt event painlessly in your Vue.js application. It comes handy when you're building offline-first Progressive Web Apps and want to display a custom "Add to home screen" banner on you web app. Adds canInstall event via a global mixin. Exposes useful TypeScript definitions.

Installation

$ npm install vue-pwa-install

Usage

As a plugin

VuePwaInstallMixin will be injected into every component.

import VuePwaInstallPlugin from "vue-pwa-install";

Vue.use(VuePwaInstallPlugin);

As a mixin

You can inject VuePwaInstallMixin manually directly into your components.

import { VuePwaInstallMixin } from "vue-pwa-install";

export default {
  mixins: [VuePwaInstallMixin],
};

Inside a component

<template>
  <button v-if="deferredPrompt" @onClick="promptInstall">
    Add to home screen
  </button>
</template>

<script lang="ts">
  import { Component, Vue } from "vue-property-decorator";
  import { BeforeInstallPromptEvent } from "vue-pwa-install";

  @Component({})
  export default class App extends Vue {
    deferredPrompt: BeforeInstallPromptEvent | void;

    promptInstall() {
      // Show the prompt:
      this.deferredPrompt.prompt();

      // Wait for the user to respond to the prompt:
      this.deferredPrompt.userChoice.then((choiceResult) => {
        if (choiceResult.outcome === "accepted") {
          console.log("User accepted the install prompt");
        } else {
          console.log("User dismissed the install prompt");
        }

        this.deferredPrompt = null;
      });
    }

    created() {
      this.$on("canInstall", (event: BeforeInstallPromptEvent) => {
        // Prevent Chrome 67 and earlier from automatically showing the prompt:
        event.preventDefault();

        // Stash the event so it can be triggered later:
        this.deferredPrompt = event;
      });
    }
  }
</script>

Related Packages

vue2-ios-pwa-prompt

Port https://github.com/chrisdancee/react-ios-pwa-prompt.git for vue

1,819 9
bpwa

pwa project solution

1,384 5
@vue/cli-plugin-pwa

pwa plugin for vue-cli

2,414,368 29,510
vue-pwa-asset-generator

VueJS asset generator for PWA

25,395 175

Version History

Version Released Vue License
1.1.0 >=2 MIT
1.0.0 ^2.5.17 MIT
0.0.1