FIX: Only unregister service workers that belongs to Discourse.

FIX: Only unregister service workers that belongs to Discourse.

diff --git a/app/assets/javascripts/admin/controllers/admin-web-hooks-show.js.es6 b/app/assets/javascripts/admin/controllers/admin-web-hooks-show.js.es6
index 1f28c96..a2bbd61 100644
--- a/app/assets/javascripts/admin/controllers/admin-web-hooks-show.js.es6
+++ b/app/assets/javascripts/admin/controllers/admin-web-hooks-show.js.es6
@@ -82,7 +82,8 @@ export default Ember.Controller.extend({
   actions: {
     save() {
       this.set("saved", false);
-      const url = extractDomainFromUrl(this.get("model.payload_url"));
+      const url = this.get("model.payload_url");
+      const domain = extractDomainFromUrl(url);
       const model = this.get("model");
       const isNew = model.get("isNew");
 
@@ -103,10 +104,10 @@ export default Ember.Controller.extend({
       };
 
       if (
-        url === "localhost" ||
-        url.match(/192\.168\.\d+\.\d+/) ||
-        url.match(/127\.\d+\.\d+\.\d+/) ||
-        url === Discourse.BaseUrl
+        domain === "localhost" ||
+        domain.match(/192\.168\.\d+\.\d+/) ||
+        domain.match(/127\.\d+\.\d+\.\d+/) ||
+        url.startsWith(Discourse.BaseUrl)
       ) {
         return bootbox.confirm(
           I18n.t("admin.web_hooks.warn_local_payload_url"),
diff --git a/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6 b/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6
index 5248a76..b78044e 100644
--- a/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6
+++ b/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6
@@ -20,7 +20,7 @@ export default {
                 Discourse.ServiceWorkerURL
               )
             ) {
-              registration.unregister();
+              this.unregister(registration);
             }
           }
         });
@@ -34,10 +34,16 @@ export default {
       } else {
         navigator.serviceWorker.getRegistrations().then(registrations => {
           for (let registration of registrations) {
-            registration.unregister();
+            this.unregister(registration);
           }
         });
       }
     }
+  },
+
+  unregister(registration) {
+    if (registration.scope.startsWith(Discourse.BaseUrl)) {
+      registration.unregister();
+    }
   }
 };
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index df7237c..723d77a 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -443,11 +443,10 @@ module ApplicationHelper
 
   def client_side_setup_data
     service_worker_url = Rails.env.development? ? 'service-worker.js' : Rails.application.assets_manifest.assets['service-worker.js']
-    current_hostname_without_port = RailsMultisite::ConnectionManagement.current_hostname.sub(/:[\d]*$/, '')
 
     setup_data = {
       cdn: Rails.configuration.action_controller.asset_host,
-      base_url: current_hostname_without_port,
+      base_url: Discourse.base_url,
       base_uri: Discourse::base_uri,
       environment: Rails.env,
       letter_avatar_version: LetterAvatar.version,
diff --git a/test/javascripts/test_helper.js b/test/javascripts/test_helper.js
index de9f4a2..dfd5d08 100644
--- a/test/javascripts/test_helper.js
+++ b/test/javascripts/test_helper.js
@@ -115,7 +115,7 @@ QUnit.testStart(function(ctx) {
   // Allow our tests to change site settings and have them reset before the next test
   Discourse.SiteSettings = dup(Discourse.SiteSettingsOriginal);
   Discourse.BaseUri = "";
-  Discourse.BaseUrl = "localhost";
+  Discourse.BaseUrl = "http://localhost:3000";
   Discourse.Session.resetCurrent();
   Discourse.User.resetCurrent();
   resetSite(Discourse.SiteSettings);

GitHub sha: abbc639e

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

1 Like