Handle beforeinstallprompt event in your PWA.
npm install pwa-install-handlerHandling PWA installation prompt made easier. MDN docs
``sh`
npm install pwa-install-handler
`html`
`javascript
import { pwaInstallHandler } from 'pwa-install-handler'
const $button = document.querySelector('#installButton')
pwaInstallHandler.addListener((canInstall) => {
$button.style.display = canInstall ? 'inline-block' : 'none'
})
$button.addEventListener('click', () => {
pwaInstallHandler.install()
})
`
For more information see react-use-pwa-install.
`typescript`
pwaInstallHandler.install: () => Promise
`typescript`
pwaInstallHandler.addListener: (
callback: (canInstall: boolean, install?: () => Promise
) => void
`typescript`
pwaInstallHandler.removeListener: (
callback: (canInstall: boolean, install?: () => Promise
) => void
`typescript`
pwaInstallHandler.canInstall: () => boolean
`typescript`
pwaInstallHandler.getEvent: () => BeforeInstallPromptEvent | null
You PWA must meet some requirements to be installable. Without that the canInstall will always be false. The requirements are browser specific. You can read more about it here (MDN) and here (web.dev).
Some browsers don't support custom install button. These will have canInstall always set to false` too. For more information check BeforeInstallPromptEvent support.