FIX: Subfolder replace should only affect URL prefix (#12135)
* FIX: Subfolder replace should only affect URL prefix Issue was reported in https://meta.discourse.org/t/-/179504 * DEV: Test subfolder handling in get-url when called twice on the same path
This commit is contained in:
parent
b22ea7911c
commit
5117f0a3e8
|
@ -1,9 +1,10 @@
|
||||||
let cdn, baseUrl, baseUri;
|
let cdn, baseUrl, baseUri, baseUriMatcher;
|
||||||
let S3BaseUrl, S3CDN;
|
let S3BaseUrl, S3CDN;
|
||||||
|
|
||||||
export default function getURL(url) {
|
export default function getURL(url) {
|
||||||
if (baseUri === undefined) {
|
if (baseUri === undefined) {
|
||||||
baseUri = $('meta[name="discourse-base-uri"]').attr("content") || "";
|
baseUri = $('meta[name="discourse-base-uri"]').attr("content") || "";
|
||||||
|
baseUriMatcher = new RegExp(`^${baseUri}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!url) {
|
if (!url) {
|
||||||
|
@ -47,18 +48,23 @@ export function isAbsoluteURL(url) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function withoutPrefix(path) {
|
export function withoutPrefix(path) {
|
||||||
const rootURL = (!baseUri ? "/" : baseUri).replace(/\/$/, "");
|
if (!baseUri) {
|
||||||
return path.replace(rootURL, "");
|
return path;
|
||||||
|
} else {
|
||||||
|
return path.replace(baseUriMatcher, "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setPrefix(configBaseUri) {
|
export function setPrefix(configBaseUri) {
|
||||||
baseUri = configBaseUri;
|
baseUri = configBaseUri;
|
||||||
|
baseUriMatcher = new RegExp(`^${baseUri}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setupURL(configCdn, configBaseUrl, configBaseUri) {
|
export function setupURL(configCdn, configBaseUrl, configBaseUri) {
|
||||||
cdn = configCdn;
|
cdn = configCdn;
|
||||||
baseUrl = configBaseUrl;
|
baseUrl = configBaseUrl;
|
||||||
baseUri = configBaseUri;
|
baseUri = configBaseUri;
|
||||||
|
baseUriMatcher = new RegExp(`^${baseUri}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setupS3CDN(configS3BaseUrl, configS3CDN) {
|
export function setupS3CDN(configS3BaseUrl, configS3CDN) {
|
||||||
|
|
|
@ -39,6 +39,43 @@ module("Unit | Utility | get-url", function () {
|
||||||
assert.equal(withoutPrefix("/"), "/");
|
assert.equal(withoutPrefix("/"), "/");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("withoutPrefix called multiple times on the same path", function (assert) {
|
||||||
|
setPrefix("/eviltrout");
|
||||||
|
assert.equal(withoutPrefix(withoutPrefix("/eviltrout/hello")), "/hello");
|
||||||
|
assert.equal(withoutPrefix(withoutPrefix("/eviltrout/")), "/");
|
||||||
|
assert.equal(withoutPrefix(withoutPrefix("/eviltrout")), "");
|
||||||
|
|
||||||
|
setPrefix("");
|
||||||
|
assert.equal(
|
||||||
|
withoutPrefix(withoutPrefix("/eviltrout/hello")),
|
||||||
|
"/eviltrout/hello"
|
||||||
|
);
|
||||||
|
assert.equal(withoutPrefix(withoutPrefix("/eviltrout")), "/eviltrout");
|
||||||
|
assert.equal(withoutPrefix(withoutPrefix("/")), "/");
|
||||||
|
|
||||||
|
setPrefix(null);
|
||||||
|
assert.equal(
|
||||||
|
withoutPrefix(withoutPrefix("/eviltrout/hello")),
|
||||||
|
"/eviltrout/hello"
|
||||||
|
);
|
||||||
|
assert.equal(withoutPrefix(withoutPrefix("/eviltrout")), "/eviltrout");
|
||||||
|
assert.equal(withoutPrefix(withoutPrefix("/")), "/");
|
||||||
|
|
||||||
|
setPrefix("/f");
|
||||||
|
assert.equal(
|
||||||
|
withoutPrefix(withoutPrefix("/f/t/falco-says-hello")),
|
||||||
|
"/t/falco-says-hello"
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
withoutPrefix(withoutPrefix("/f/tag/fast-chain-food")),
|
||||||
|
"/tag/fast-chain-food"
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
withoutPrefix(withoutPrefix("/f/u/falco/summary")),
|
||||||
|
"/u/falco/summary"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test("getURL with empty paths", function (assert) {
|
test("getURL with empty paths", function (assert) {
|
||||||
setupURL(null, "https://example.com", "/");
|
setupURL(null, "https://example.com", "/");
|
||||||
assert.equal(getURL("/"), "/");
|
assert.equal(getURL("/"), "/");
|
||||||
|
|
Loading…
Reference in New Issue