1

I am coding a PWA and also using state management. The problem I'm facing is that the first time the user access the page, I am able to store the beforeinstallprompt event and display my customized install button.

The problem is, if the user ignores the install, and reload the pages, the event is not longer caught. Code below:

  private deferAppInstall() {
    const subscription = this.actions$
      .pipe(ofActionDispatched(PwaStateActions.DeferAppInstall))
      .subscribe(_ => {
        console.log('pipe console ');
        window.addEventListener('beforeinstallprompt', event => {
          console.log('addEventListener console');
          this.pwaStateService.setInstall(true);
          this.deferredInstallPrompt = event;
          event.preventDefault();

          subscription.unsubscribe();
          console.log('unsubscribe console');
        });
      });
  }

Basically, the first time everything works, subsequent visits will just present the pipe console log message and not get into the addEventListerner and the rest of the code.

O noticed that when using Chrome Dev Tools, if I go to the Application tab under Application > Clear Storage, and I check ONLY the Unregistered service workers option and clean the cache, and access again the page, everything will work as expected.

At first, I assumed it could be related to cache since I am using state management, but I have no ideia if an event is stored on the cache and if yes, I would not know how to get the beforeinstallprompt event from that cache.

As cleaning the service worker options seemed to "reestablish" the expected behavior, I am looking for a solution where I am able to verify if the event was previously triggered so I can trigger the logic of the above code (or maybe improve it to consider that verification).

Community
  • 1
  • 1
Estevao Santiago
  • 793
  • 2
  • 7
  • 30
  • You say "Ignores the prompt" - does that mean they closed the browser prompt? If they closed it, then no means no. They rejected the prompt and will not be shown it again. Well perhaps months later, but that I have never seen. (Chrome & Probably Edge) – Mathias Aug 20 '19 at 12:03
  • By ignores I mean clicked some where else on the page, consequently no Yes or No was provided back, it is `undefined`. And yes, according to Chrome, the prompt will be only displayed again in 3 months for desktop and apparently never again for Android. – Estevao Santiago Aug 20 '19 at 18:17
  • I'm having this same issue using Gatsby.js - did you ever find a solution? – allicarn Mar 15 '21 at 22:43
  • @allicarn not really. It is browser-based, so you may find a solution that fits on Chrome, but the same may not work on other browsers and vice-versa. I forgot now, but on Chrome you will have the option to install the app on the options after the page pass the PWA checks. – Estevao Santiago Apr 01 '21 at 14:06

0 Answers0