pwaInstall()
: Promotion and PWA install buttonIf the PWA meets the installation criteria, the browser triggers an event to prompt the user to install it.
For example, on Chrome Android, the browser displays a banner offering the installation.
The user may not know the installation process of a PWA, and we will be able to improve this experience by personalizing it.
For this purpose, the function pwaInstall()
will:
In order for the function to be functional, these elements must be added to the DOM:
pwa-install-promotion
: container of the installation promotion insertpwa-install-promotion-close
: installation promotion inset close buttonpwa-install-btn
: install buttonpwa-install-confirm
: container of the installation confirmation insertpwa-install-confirm-close
: button to close the installation confirmation insertpwa-loader
: Loader
<div class="pwa-install-promotion">
<button class="pwa-install-promotion-close">Close</button>
{{ content }}
<button class="pwa-install-btn">Install</button>
</div>
<div class="pwa-install-confirm">
<button class="pwa-install-confirm-close">Close</button>
{{ content }}
</div>
<div class="pwa-loader">
{{ loader }}
</div>
If you don't use pwabunga-ui.css, here is the CSS to use:
We hide the elements useful to the function
.pwa-install-promotion,
.pwa-install-confirm,
.pwa-install-btn,
.pwa-loader {
display: none;
}
If the element has class is-visible
, we display it
.pwa-install-promotion.is-visible,
.pwa-install-confirm.is-visible,
.pwa-install-btn.is-visible,
.pwa-loader.is-visible {
display: block;
}
const pwaInstall = async () => {
...
}
We set the DOM elements to be manipulated as variables
const pwaInstallPromotion = document.querySelector('.pwa-install-promotion')
const pwaInstallPromotionClose = document.querySelector('.pwa-install-promotion-close')
const pwaInstallBtn = document.querySelector('.pwa-install-btn')
const pwaInstallConfirm = document.querySelector('.pwa-install-confirm')
const pwaInstallConfirmClose = document.querySelector('.pwa-install-confirm-close')
const pwaInstallLoader = document.querySelector('.pwa-loader')
We initialize deferredPrompt
which we will use later to display the installation screen
let deferredPrompt
To display the elements, we use the event beforeinstallprompt
window.addEventListener('beforeinstallprompt', (e) => {
We prevent the appearance of the banner displayed by default by the browser offering the installation
e.preventDefault()
We store the event that we will use later
deferredPrompt = e
The installation promotion insert and the installation button are displayed.
pwaInstallPromotion.classList.add('is-visible')
pwaInstallBtn.classList.add('is-visible')
Now that the install button is displayed if the installation has not been done yet, we make this button functional
Au clic sur le bouton d'installation
pwaInstallBtn.addEventListener('click', () => {
We display the installation request
deferredPrompt.prompt()
We detect if the user has confirmed the installation or not
const { outcome } = await deferredPrompt.userChoice;
deferredPrompt
having been used, it can no longer be used
deferredPrompt = null
If the user has accepted the installation, we hide the installation button
if (outcome === 'accepted') {
pwaInstallBtn.classList.remove('is-visible')
}
We use the appinstalled
event to display the confirmation box.
This event is triggered when the installation process is initiated and not when the installation is finished
window.addEventListener("appinstalled", () => {
We hide the installation promotion insert and display the loader
pwaInstallPromotion.classList.remove('is-visible')
pwaInstallLoader.classList.add('is-visible')
The event being triggered at the beginning of the installation, we cannot know when this installation is finished. We therefore simulate the loading time by displaying the loader for 2.5 seconds and then displaying the installation confirmation insert.
setTimeout(() => {
pwaInstallConfirm.classList.add('is-visible')
pwaInstallLoader.classList.remove('is-visible')
}, "2500");
We hide the promotion insert when the pwaPromotionClose
button is clicked
pwaInstallPromotionClose.addEventListener('click', (e) => {
pwaInstallPromotion.classList.remove('is-visible')
})
We hide the installation confirmation box when you click the button pwaInstallConfirmClose
pwaInstallConfirmClose.addEventListener('click', (e) => {
pwaInstallConfirm.classList.remove('is-visible')
})
Here is the function call used on pwabunga.js
pwaInstall()
pwaInstall()
const pwaInstall = async () => {
const pwaInstallPromotion = document.querySelector('.pwa-install-promotion')
const pwaInstallPromotionClose = document.querySelector('.pwa-install-promotion-close')
const pwaInstallBtn = document.querySelector('.pwa-install-btn')
const pwaInstallConfirm = document.querySelector('.pwa-install-confirm')
const pwaInstallConfirmClose = document.querySelector('.pwa-install-confirm-close')
const pwaInstallLoader = document.querySelector('.pwa-loader')
let deferredPrompt
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault()
deferredPrompt = e
pwaInstallPromotion.classList.add('is-visible')
pwaInstallBtn.classList.add('is-visible')
})
pwaInstallBtn.addEventListener('click', () => {
deferredPrompt.prompt()
const { outcome } = await deferredPrompt.userChoice;
deferredPrompt = null
if (outcome === 'accepted') {
pwaInstallBtn.classList.remove('is-visible')
}
})
window.addEventListener("appinstalled", async () => {
pwaInstallPromotion.classList.remove('is-visible')
pwaInstallLoader.classList.add('is-visible')
setTimeout(() => {
pwaInstallConfirm.classList.add('is-visible')
pwaInstallLoader.classList.remove('is-visible')
}, "2500");
})
pwaInstallPromotionClose.addEventListener('click', () => {
pwaInstallPromotion.classList.remove('is-visible')
})
pwaInstallConfirmClose.addEventListener('click', () => {
pwaInstallConfirm.classList.remove('is-visible')
})
}
pwaInstall()
: Promotion and PWA install buttonpwaUpdate()
: Application updatepwaShare()
: Using device native sharingpwaParams()
: PWA parameters insert