From 96c14c196826ade44c3adb936ac94d293f896872 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 14 Jun 2021 22:30:36 -0400 Subject: [PATCH] FIX: Some absolute links were causing full page reloads (#13377) --- .../discourse-common/addon/lib/get-url.js | 18 +++++++++++------- .../discourse/app/lib/click-track.js | 7 ++----- .../tests/unit/lib/click-track-test.js | 12 ++++++++++++ 3 files changed, 25 insertions(+), 12 deletions(-) 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 ed6e455a609..fa1ba6b5c1d 100644 --- a/app/assets/javascripts/discourse-common/addon/lib/get-url.js +++ b/app/assets/javascripts/discourse-common/addon/lib/get-url.js @@ -1,13 +1,6 @@ let cdn, baseUrl, baseUri, baseUriMatcher; let S3BaseUrl, S3CDN; -export function getBaseURI() { - if (baseUri === undefined) { - setPrefix($('meta[name="discourse-base-uri"]').attr("content") || ""); - } - return baseUri || "/"; -} - export default function getURL(url) { if (baseUri === undefined) { setPrefix($('meta[name="discourse-base-uri"]').attr("content") || ""); @@ -76,3 +69,14 @@ export function setupS3CDN(configS3BaseUrl, configS3CDN) { S3BaseUrl = configS3BaseUrl; S3CDN = configS3CDN; } + +// We can use this to identify when navigating on the same host but outside of the +// prefix directory. For example from `/forum` to `/about-us` which is not discourse +export function samePrefix(url) { + if (baseUri === undefined) { + setPrefix($('meta[name="discourse-base-uri"]').attr("content") || ""); + } + let origin = window.location.origin; + let cmp = url[0] === "/" ? baseUri || "/" : origin + baseUri || origin; + return url.indexOf(cmp) === 0; +} diff --git a/app/assets/javascripts/discourse/app/lib/click-track.js b/app/assets/javascripts/discourse/app/lib/click-track.js index e930f2e489a..72d75169dbb 100644 --- a/app/assets/javascripts/discourse/app/lib/click-track.js +++ b/app/assets/javascripts/discourse/app/lib/click-track.js @@ -4,7 +4,7 @@ import { Promise } from "rsvp"; import User from "discourse/models/user"; import { ajax } from "discourse/lib/ajax"; import bootbox from "bootbox"; -import getURL, { getBaseURI } from "discourse-common/lib/get-url"; +import getURL, { samePrefix } from "discourse-common/lib/get-url"; import { isTesting } from "discourse-common/config/environment"; import { later } from "@ember/runloop"; import { selectedText } from "discourse/lib/utilities"; @@ -162,10 +162,7 @@ export default { openLinkInNewTab($link[0]); } else { trackPromise.finally(() => { - if ( - DiscourseURL.isInternal(href) && - href.indexOf(getBaseURI()) === 0 - ) { + if (DiscourseURL.isInternal(href) && samePrefix(href)) { DiscourseURL.routeTo(href); } else { DiscourseURL.redirectAbsolute(href); diff --git a/app/assets/javascripts/discourse/tests/unit/lib/click-track-test.js b/app/assets/javascripts/discourse/tests/unit/lib/click-track-test.js index 3327e9451bb..43cf438d816 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/click-track-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/click-track-test.js @@ -49,6 +49,7 @@ module("Unit | Utility | click-track", function (hooks) { bar prefix link + prefix link diff prefix link ` @@ -97,6 +98,17 @@ module("Unit | Utility | click-track", function (hooks) { assert.ok(DiscourseURL.routeTo.calledWith("/forum/thing")); }); + test("routes to absolute internal urls", async function (assert) { + setPrefix("/forum"); + pretender.post("/clicks/track", () => [200, {}, ""]); + await track(generateClickEventOn(".abs-prefix-url"), null, { + returnPromise: true, + }); + assert.ok( + DiscourseURL.routeTo.calledWith(window.location.origin + "/forum/thing") + ); + }); + test("redirects to internal urls with a different prefix", async function (assert) { setPrefix("/forum"); sinon.stub(DiscourseURL, "redirectAbsolute");