Abhi
Abhi

Reputation: 65

How to prevent Add to home screen pwa from prompting again on www version of the site?

I am able to get Add to home screen popup on my website example.com but when I open my website with www.example.com, I again get the popup and on clicking on add, it adds the wpa again in the home screen.

How can I prevent this from happening as its useless to have two wpa icons of the same website?

Upvotes: 0

Views: 1281

Answers (2)

mattemyo
mattemyo

Reputation: 99

You could add some JavaScript code to remove the following tag whenever the user visits the wwwsite.

<link rel="manifest" href="/manifest.json">

Example:

if (window.location.host.startsWith('www.') {
    const manifestLink = document.querySelector("link[rel='manifest']")
    manifestLink.parentNode.removeChild(manifestLink)
}

Upvotes: -1

Erndob
Erndob

Reputation: 2612

Easiest option is to simply redirect www calls to non-www calls. So the users never actually stay in that different subdomain.

If for some reason you can't do that, on install of the PWA you could save a cross subdomain cookie just to have a "flag" set that the user installed the PWA. Then, in your code you listen to beforeinstallpromptevent, and do not prompt the install if the cookie exists.

Upvotes: 3

Related Questions