From dac0e5b3ccf474b3b74a591cf6ea399b2b607f53 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Tue, 25 Aug 2020 11:42:50 +0200 Subject: [PATCH] Merge pull request #10519 from gschlager/get-url Fixes for getURL() in subfolders and basePath in translations --- .../admin/controllers/admin-dashboard-general.js | 2 +- .../javascripts/admin/templates/dashboard_general.hbs | 2 +- .../javascripts/discourse-common/addon/helpers/get-url.js | 7 ++++++- .../javascripts/discourse-common/addon/lib/get-url.js | 6 ++++-- .../discourse/app/components/suggested-topics.js | 2 +- .../discourse/app/controllers/forgot-password.js | 2 +- .../javascripts/discourse/app/models/topic-details.js | 2 +- .../javascripts/discourse/app/templates/modal/bookmark.hbs | 2 +- test/javascripts/lib/get-url-test.js | 6 ++++++ 9 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/admin-dashboard-general.js b/app/assets/javascripts/admin/controllers/admin-dashboard-general.js index 2030c1e613a..07a940afc79 100644 --- a/app/assets/javascripts/admin/controllers/admin-dashboard-general.js +++ b/app/assets/javascripts/admin/controllers/admin-dashboard-general.js @@ -107,7 +107,7 @@ export default Controller.extend(PeriodComputationMixin, { @discourseComputed trendingSearchDisabledLabel() { return I18n.t("admin.dashboard.reports.trending_search.disabled", { - basePath: getURL("/") + basePath: getURL("") }); }, diff --git a/app/assets/javascripts/admin/templates/dashboard_general.hbs b/app/assets/javascripts/admin/templates/dashboard_general.hbs index 5d442f876b2..819abd9361a 100644 --- a/app/assets/javascripts/admin/templates/dashboard_general.hbs +++ b/app/assets/javascripts/admin/templates/dashboard_general.hbs @@ -147,7 +147,7 @@ filters=trendingSearchFilters isEnabled=logSearchQueriesEnabled disabledLabel=trendingSearchDisabledLabel}} - {{html-safe (i18n "admin.dashboard.reports.trending_search.more" basePath=(base-url))}} + {{html-safe (i18n "admin.dashboard.reports.trending_search.more" basePath=(base-path))}} {{/if}} diff --git a/app/assets/javascripts/discourse-common/addon/helpers/get-url.js b/app/assets/javascripts/discourse-common/addon/helpers/get-url.js index f221c20c776..2c8c900a1e6 100644 --- a/app/assets/javascripts/discourse-common/addon/helpers/get-url.js +++ b/app/assets/javascripts/discourse-common/addon/helpers/get-url.js @@ -1,5 +1,10 @@ import { registerUnbound } from "discourse-common/lib/helpers"; import getUrl from "discourse-common/lib/get-url"; +import deprecated from "discourse-common/lib/deprecated"; registerUnbound("get-url", value => getUrl(value)); -registerUnbound("base-url", () => getUrl("")); +registerUnbound("base-url", () => { + deprecated("Use `{{base-path}}` instead of `{{base-url}}`"); + return getUrl(""); +}); +registerUnbound("base-path", () => getUrl("")); diff --git a/app/assets/javascripts/discourse-common/addon/lib/get-url.js b/app/assets/javascripts/discourse-common/addon/lib/get-url.js index a74ea5ff64f..7f2a9be1fef 100644 --- a/app/assets/javascripts/discourse-common/addon/lib/get-url.js +++ b/app/assets/javascripts/discourse-common/addon/lib/get-url.js @@ -2,12 +2,14 @@ let cdn, baseUrl, baseUri; let S3BaseUrl, S3CDN; export default function getURL(url) { - if (!url) return url; - if (baseUri === undefined) { baseUri = $('meta[name="discourse-base-uri"]').attr("content") || ""; } + if (!url) { + return baseUri === "/" ? "" : baseUri; + } + // if it's a non relative URL, return it. if (url !== "/" && !/^\/[^\/]/.test(url)) return url; diff --git a/app/assets/javascripts/discourse/app/components/suggested-topics.js b/app/assets/javascripts/discourse/app/components/suggested-topics.js index 38ebaa1a5a8..2bcad1525d2 100644 --- a/app/assets/javascripts/discourse/app/components/suggested-topics.js +++ b/app/assets/javascripts/discourse/app/components/suggested-topics.js @@ -63,7 +63,7 @@ export default Component.extend({ CATEGORY: category ? true : false, latestLink: opts.latestLink, catLink: opts.catLink, - basePath: "" + basePath: getURL("") }); } else if (category) { return I18n.t("topic.read_more_in_category", opts); diff --git a/app/assets/javascripts/discourse/app/controllers/forgot-password.js b/app/assets/javascripts/discourse/app/controllers/forgot-password.js index 334367a370a..1adaa5a8b05 100644 --- a/app/assets/javascripts/discourse/app/controllers/forgot-password.js +++ b/app/assets/javascripts/discourse/app/controllers/forgot-password.js @@ -31,7 +31,7 @@ export default Controller.extend(ModalFunctionality, { help() { this.setProperties({ offerHelp: I18n.t("forgot_password.help", { - basePath: getURL("/") + basePath: getURL("") }), helpSeen: true }); diff --git a/app/assets/javascripts/discourse/app/models/topic-details.js b/app/assets/javascripts/discourse/app/models/topic-details.js index 514db4d9f4d..0c78fd25eb5 100644 --- a/app/assets/javascripts/discourse/app/models/topic-details.js +++ b/app/assets/javascripts/discourse/app/models/topic-details.js @@ -58,7 +58,7 @@ const TopicDetails = RestModel.extend({ } else { return I18n.t(localeString, { username: User.currentProp("username_lower"), - basePath: getURL("/") + basePath: getURL("") }); } }, diff --git a/app/assets/javascripts/discourse/app/templates/modal/bookmark.hbs b/app/assets/javascripts/discourse/app/templates/modal/bookmark.hbs index 5335a6e9b55..ffc50c1ee21 100644 --- a/app/assets/javascripts/discourse/app/templates/modal/bookmark.hbs +++ b/app/assets/javascripts/discourse/app/templates/modal/bookmark.hbs @@ -99,7 +99,7 @@ {{/tap-tile-grid}} {{else}} -
{{html-safe (i18n "bookmarks.no_timezone" basePath=(base-uri))}}
+
{{html-safe (i18n "bookmarks.no_timezone" basePath=(base-path))}}
{{/if}} diff --git a/test/javascripts/lib/get-url-test.js b/test/javascripts/lib/get-url-test.js index 6a97f428943..1d889242109 100644 --- a/test/javascripts/lib/get-url-test.js +++ b/test/javascripts/lib/get-url-test.js @@ -61,6 +61,12 @@ QUnit.test("getURL on subfolder install", assert => { "relative url has subfolder" ); + assert.equal( + getURL(""), + "/forum", + "relative url has subfolder without trailing slash" + ); + assert.equal( getURL("/svg-sprite/forum.example.com/svg-sprite.js"), "/forum/svg-sprite/forum.example.com/svg-sprite.js",