Search code examples
javascriptprogressive-web-apps

how would I reliably detect if my PWA is installed or not on chromeos?


So, my site detects if it's being displayed in display-mode: browser to show an install prompt but I want to allow them to open the app in standalone using URI handlers if the PWA is already installed, and webAPK is not relevant since I'm using ChromeOS.

I was looking for API's only available to PWA's but had no luck with that. I found methods that used the beforeinstallprompt event, but I'd have 2 put all my code into it and even if I used an async function that resolved when the event fires it takes a while for it to fire so the user would notice the change.

I would prefer a synchronous approach but if that's not possible I'd want a fast approach instead of waiting on beforeinstallprompt


Solution

  • so, it turns out you could use the appinstalled event to initially set a localStorage key to tell your site it's installed and have an event listener for beforeinstallprompt that checks if that key exists so you can delete it.

    Here's a snippet

    window.addEventListener("beforeinstallprompt", function() {
      if (typeof localStorage.getItem("installed") == 'string') {
        localStorage.removeItem("installed");
        window.location.reload();
      }
    })
    window.addEventListener("appinstalled", function() {
      localStorage.setItem("installed", "");
    })
    const isInstalled = (typeof localStorage.getItem("installed") == 'string');