FIX: Service Worker URL was broken

FIX: Service Worker URL was broken

Behavior was changed in #9966, which made the URL be relative.

If the user landed in a topic, for example, the browser was given a service worker URL under that specific topic URL, which was a 404.

Fixes broken PWA install and broken push notifications

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 c22b7e9..6b5927d 100644
--- a/app/assets/javascripts/discourse/app/initializers/register-service-worker.js
+++ b/app/assets/javascripts/discourse/app/initializers/register-service-worker.js
@@ -1,5 +1,5 @@
 import { isAbsoluteURL } from "discourse-common/lib/get-url";
-import getURL from "discourse-common/lib/get-url";
+import getAbsoluteURL from "discourse-common/lib/get-url";
 
 export default {
   name: "register-service-worker",
@@ -33,7 +33,7 @@ export default {
         });
 
         navigator.serviceWorker
-          .register(`${getURL(Discourse.ServiceWorkerURL)}`)
+          .register(getAbsoluteURL(`/${Discourse.ServiceWorkerURL}`))
           .catch(error => {
             // eslint-disable-next-line no-console
             console.info(`Failed to register Service Worker: ${error}`);

GitHub sha: f00e5515

2 Likes