vite-pwa / vite-plugin-pwa

Zero-config PWA for Vite
https://vite-pwa-org.netlify.app/
MIT License
3.08k stars 201 forks source link

Is the install function supposed to do anything? #748

Closed Dobermensch closed 1 day ago

Dobermensch commented 2 weeks ago

I'm using this plugin with nuxt. Everything works as expected.

However, I wanted to to create a button for the user to add the PWA to the homescreen so I stumbled upon the $pwa.install method.

I'm calling it on button click but it does nothing.

What is it supposed to do or why isn't it working?

I have client: { installPrompt: true } in pwa object in nuxt.config.

userquin commented 2 weeks ago

You should check if showInstallPrompt is true, the install function does nothing if not enabled.

Dobermensch commented 2 weeks ago

I checked the value of showInstallPrompt. It is false.

I tried to set it to true and then use the $pwa.install method. Nothing happened.

Here's a snippet of my code:

const addClicked = async () => {
  $pwa.showInstallPrompt = true
  await $pwa.install()
}

How do I use the $pwa.install method?

userquin commented 2 weeks ago

That flag must be set by the browser on before install prompt event listener: the sw must installed and activated and the browser must support the feature. You cannot change browser behavior.

You can check the logic here: https://github.com/vite-pwa/nuxt/blob/main/src/runtime/plugins/pwa.client.ts#L86

Dobermensch commented 2 weeks ago

So if I understand correctly, since I cannot change the browser behaviour, it means it either works or it doesn't. In my case, since it doesn't work, there's nothing I can do?

Dobermensch commented 5 days ago

bump

userquin commented 5 days ago

If the browser doesn't have that feature we cannot do anything, check: https://caniuse.com/?search=beforeinstallprompt