How do I know if pwa has already been installed on another domain?

Asked

Viewed 137 times

0

I have the same application running in 2 different domains, one is examplo.com and the other exemplo2.com

In this application has a button on which the user can click to download the application, this button disappears when the user performs the download. But if I access the same application on a different domain, the button stays there

How to solve?

const buttonAddToHomeScreen = document.querySelector('#addToHomeScreen');

let deferredPrompt;

window.on('beforeinstallprompt', function(event) {
    event.preventDefault();

    deferredPrompt = event;

    buttonAddToHomeScreen.style.display = 'block';
    buttonAddToHomeScreen.style.opacity = '1';
});

buttonAddToHomeScreen.on('click', function() {
    if (!deferredPrompt) return;

    deferredPrompt.prompt();

    deferredPrompt.userChoice.then(function() {
        buttonAddToHomeScreen.style.display = 'none';
        buttonAddToHomeScreen.style.opacity = '0';

        deferredPrompt = null;
    });
});

1 answer

1


You can use a cookie to verify that the button has already been clicked and set this cookie on both domains with the help of an iframe (since the cookie is unique per domain).

That is, you can create a "setcookie" page with the script below and put it in the route examplo.com/setcookie and examplo2.com/setcookie

localStorage.setItem('botaoClicado', 'true')

and then change the button view logic to only show case localStorage.getItem('botaoClicado') not be null.

To set the cookie simply click the two iframes on your page after the user clicks the download button (or you can put it on the "download thank you" page if there is one). An alternative to doing this is the following:

function aoClicarEmDownload(){
let body = document.getElementByTagName('body')[0]
let iframe1 = document.createElement("iframe")
iframe1.src = "http://exemplo.com/setcookie"
iframe1.height = "0"
iframe1.width = "0"
body.appendChild(iframe1)

let iframe2 = document.createElement("iframe")
iframe2.src = "http://exemplo.com/setcookie"
iframe2.height = "0"
iframe2.width = "0"
body.appendChild(iframe2)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.