FIX: Some absolute links were causing full page reloads (#13377)

FIX: Some absolute links were causing full page reloads (#13377)

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 ed6e455..fa1ba6b 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 e930f2e..72d7516 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 3327e94..43cf438 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) {
             <a href="https://google.com">bar</a>
           </aside>
           <a class="prefix-url" href="/forum/thing">prefix link</a>
+          <a class="abs-prefix-url" href="${window.location.origin}/forum/thing">prefix link</a>
           <a class="diff-prefix-url" href="/thing">diff prefix link</a>
         </article>
       </div>`
@@ -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");

GitHub sha: 96c14c196826ade44c3adb936ac94d293f896872

This commit appears in #13377 which was approved by davidtaylorhq. It was merged by SamSaffron.