From abbc639e0d362ae90b52c8b5c6922d9e610777f8 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 7 May 2019 12:36:40 +0800 Subject: [PATCH] FIX: Only unregister service workers that belongs to Discourse. --- .../admin/controllers/admin-web-hooks-show.js.es6 | 11 ++++++----- .../initializers/register-service-worker.js.es6 | 10 ++++++++-- app/helpers/application_helper.rb | 3 +-- test/javascripts/test_helper.js | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) 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 1f28c967236..a2bbd6149ce 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 5248a76e272..b78044e11f3 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 df7237c9aad..723d77a0a81 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 de9f4a2d4c4..dfd5d08d3da 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);