Skip to content

Instantly share code, notes, and snippets.

@dhAlcojor
Last active May 9, 2020 18:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhAlcojor/c8f1c3f1e602836a3545e747bf7b0394 to your computer and use it in GitHub Desktop.
Save dhAlcojor/c8f1c3f1e602836a3545e747bf7b0394 to your computer and use it in GitHub Desktop.
PWAs snippets
/**
* It can be useful to know that the user has installed the app succesfully
*/
window.addEventListener('appinstalled', e => {
// Do your analytics stuff
});
/**
* 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