Last active
May 9, 2020 18:10
-
-
Save dhAlcojor/c8f1c3f1e602836a3545e747bf7b0394 to your computer and use it in GitHub Desktop.
PWAs snippets
This file contains 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
/** | |
* It can be useful to know that the user has installed the app succesfully | |
*/ | |
window.addEventListener('appinstalled', e => { | |
// Do your analytics stuff | |
}); |
This file contains 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
/** | |
* In case you are implementing your own web app install promotion, use this code to hide the browser's default one. | |
* Check out MDN's documentation on this: https://developer.mozilla.org/en-US/docs/Web/API/BeforeInstallPromptEvent | |
*/ | |
/* | |
* First, you have to stop the mini infobar from appearing | |
*/ | |
window.addEventListener('beforeinstallprompt', e => { | |
e.preventDefault(); | |
// BUT you need to have added your own stuff BEFORE hiding the mini infobar or your users won't be able to install | |
yourStuffToPromoteInstallation.show(); | |
capturedInstallEvent = e; | |
}; | |
/* | |
* And then you have to make it appear when the user clicks on your install button | |
*/ | |
yourInstallButton.addEventListener('click', e => { | |
capturedInstallEvent.prompt(); | |
capturedInstallEvent.userChoice.then(choice => { | |
// 'accepted' or 'dismissed' | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment