DEV: Export register service worker logic (#14598)

DEV: Export register service worker logic (#14598)

diff --git a/app/assets/javascripts/discourse/app/initializers/register-service-worker.js b/app/assets/javascripts/discourse/app/initializers/register-service-worker.js
index 22b54a6..3255a81 100644
--- a/app/assets/javascripts/discourse/app/initializers/register-service-worker.js
+++ b/app/assets/javascripts/discourse/app/initializers/register-service-worker.js
@@ -1,50 +1,10 @@
-import getAbsoluteURL, { isAbsoluteURL } from "discourse-common/lib/get-url";
+import { registerServiceWorker } from "discourse/lib/register-service-worker";
 
 export default {
   name: "register-service-worker",
 
   initialize(container) {
-    const isSecured = document.location.protocol === "https:";
-
-    if (isSecured && "serviceWorker" in navigator) {
-      let { serviceWorkerURL } = container.lookup("session:main");
-      const caps = container.lookup("capabilities:main");
-      const isAppleBrowser =
-        caps.isSafari ||
-        (caps.isIOS &&
-          !window.matchMedia("(display-mode: standalone)").matches);
-
-      if (serviceWorkerURL && !isAppleBrowser) {
-        navigator.serviceWorker.getRegistrations().then((registrations) => {
-          for (let registration of registrations) {
-            if (
-              registration.active &&
-              !registration.active.scriptURL.includes(serviceWorkerURL)
-            ) {
-              this.unregister(registration);
-            }
-          }
-        });
-
-        navigator.serviceWorker
-          .register(getAbsoluteURL(`/${serviceWorkerURL}`))
-          .catch((error) => {
-            // eslint-disable-next-line no-console
-            console.info(`Failed to register Service Worker: ${error}`);
-          });
-      } else {
-        navigator.serviceWorker.getRegistrations().then((registrations) => {
-          for (let registration of registrations) {
-            this.unregister(registration);
-          }
-        });
-      }
-    }
-  },
-
-  unregister(registration) {
-    if (isAbsoluteURL(registration.scope)) {
-      registration.unregister();
-    }
+    let { serviceWorkerURL } = container.lookup("session:main");
+    registerServiceWorker(container, serviceWorkerURL);
   },
 };
diff --git a/app/assets/javascripts/discourse/app/lib/register-service-worker.js b/app/assets/javascripts/discourse/app/lib/register-service-worker.js
new file mode 100644
index 0000000..3cd2200
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/lib/register-service-worker.js
@@ -0,0 +1,44 @@
+import getAbsoluteURL, { isAbsoluteURL } from "discourse-common/lib/get-url";
+
+export function registerServiceWorker(container, serviceWorkerURL) {
+  const isSecured = document.location.protocol === "https:";
+
+  if (isSecured && "serviceWorker" in navigator) {
+    const caps = container.lookup("capabilities:main");
+    const isAppleBrowser =
+      caps.isSafari ||
+      (caps.isIOS && !window.matchMedia("(display-mode: standalone)").matches);
+
+    if (serviceWorkerURL && !isAppleBrowser) {
+      navigator.serviceWorker.getRegistrations().then((registrations) => {
+        for (let registration of registrations) {
+          if (
+            registration.active &&
+            !registration.active.scriptURL.includes(serviceWorkerURL)
+          ) {
+            unregister(registration);
+          }
+        }
+      });
+
+      navigator.serviceWorker
+        .register(getAbsoluteURL(`/${serviceWorkerURL}`))
+        .catch((error) => {
+          // eslint-disable-next-line no-console
+          console.info(`Failed to register Service Worker: ${error}`);
+        });
+    } else {
+      navigator.serviceWorker.getRegistrations().then((registrations) => {
+        for (let registration of registrations) {
+          unregister(registration);
+        }
+      });
+    }
+  }
+}
+
+function unregister(registration) {
+  if (isAbsoluteURL(registration.scope)) {
+    registration.unregister();
+  }
+}

GitHub sha: 8b25aaed146f8dce7ed73558181e98303576cc68

This commit appears in #14598 which was approved by eviltrout. It was merged by markvanlan.