function addLegacyAppInstalledEventsListeners()

in packages/docusaurus-plugin-pwa/src/registerSw.js [224:289]


function addLegacyAppInstalledEventsListeners() {
  if (typeof window !== 'undefined') {
    if (debug) {
      console.log(
        '[Docusaurus-PWA][registerSw]: addLegacyAppInstalledEventsListeners',
      );
    }

    window.addEventListener('appinstalled', async (event) => {
      if (debug) {
        console.log('[Docusaurus-PWA][registerSw]: event appinstalled', event);
      }

      AppInstalledEventFiredStorage.set('true');
      if (debug) {
        console.log(
          "[Docusaurus-PWA][registerSw]: AppInstalledEventFiredStorage.set('true')",
        );
      }

      // After the app is installed, we register a service worker with the path
      // `/sw?enabled`. Since the previous service worker was `/sw`, it'll be
      // treated as a new one. The previous registration will need to be
      // cleared, otherwise the reload popup will show.
      await clearRegistrations();
    });

    // TODO this event still works in chrome but has been removed from the spec
    // in 2019!!!
    window.addEventListener('beforeinstallprompt', async (event) => {
      if (debug) {
        console.log(
          '[Docusaurus-PWA][registerSw]: event beforeinstallprompt',
          event,
        );
      }
      // TODO instead of default browser install UI, show custom docusaurus
      // prompt?
      // event.preventDefault();
      if (debug) {
        console.log(
          '[Docusaurus-PWA][registerSw]: AppInstalledEventFiredStorage.get()',
          AppInstalledEventFiredStorage.get(),
        );
      }
      if (AppInstalledEventFiredStorage.get()) {
        AppInstalledEventFiredStorage.del();
        if (debug) {
          console.log(
            '[Docusaurus-PWA][registerSw]: AppInstalledEventFiredStorage.del()',
          );
        }
        // After uninstalling the app, if the user doesn't clear all data, then
        // the previous service worker will continue serving cached files. We
        // need to clear registrations and reload, otherwise the popup shows.
        await clearRegistrations();
      }
    });

    if (debug) {
      console.log(
        '[Docusaurus-PWA][registerSw]: legacy appinstalled and beforeinstallprompt event listeners installed',
      );
    }
  }
}