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

This commit is contained in:
Robin Ward 2021-06-14 22:30:36 -04:00 committed by GitHub
parent 7fca7fb7ff
commit 96c14c1968
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 12 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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");