Created
August 19, 2020 14:25
-
-
Save yeknava/d182332da43d941ceb511f1f86534e2d to your computer and use it in GitHub Desktop.
pwa install
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let deferredPrompt = null; | |
| let installed = false; | |
| let initiated = false; | |
| export default { | |
| init() { | |
| initiated = true; | |
| window.addEventListener('beforeinstallprompt', (e) => { | |
| // Prevent Chrome 67 and earlier from automatically showing the prompt | |
| e.preventDefault(); | |
| deferredPrompt = e; | |
| if (window.matchMedia('(display-mode: standalone)').matches) { | |
| installed = true; | |
| } else { | |
| installed = false; | |
| } | |
| deferredPrompt.userChoice.then((choiceResult) => { | |
| if (choiceResult.outcome !== 'accepted') { | |
| installed = false; | |
| } | |
| deferredPrompt = null; | |
| }); | |
| }); | |
| window.addEventListener('appinstalled', () => { | |
| installed = true; | |
| }); | |
| }, | |
| isInstalled() { | |
| return initiated && installed; | |
| }, | |
| install() { | |
| if (!installed) { | |
| deferredPrompt.prompt(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment