mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 08:23:43 +08:00
052c841550
Before this fix if your forum was set up with a subfolder and you clicked on a link to a different subfolder it would not work. For example: subfolder: /cool link is: /about-us Previously it would try to resolve /about-us as /cool/about-us. With this fix it redirects to /about-us correctly.
79 lines
1.6 KiB
JavaScript
79 lines
1.6 KiB
JavaScript
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") || "");
|
|
}
|
|
|
|
if (!url) {
|
|
return baseUri === "/" ? "" : baseUri;
|
|
}
|
|
|
|
// if it's a non relative URL, return it.
|
|
if (url !== "/" && !/^\/[^\/]/.test(url)) {
|
|
return url;
|
|
}
|
|
|
|
const found = baseUriMatcher.test(url);
|
|
|
|
if (found) {
|
|
return url;
|
|
}
|
|
if (url[0] !== "/") {
|
|
url = "/" + url;
|
|
}
|
|
|
|
return baseUri + url;
|
|
}
|
|
|
|
export function getURLWithCDN(url) {
|
|
url = getURL(url);
|
|
// only relative urls
|
|
if (cdn && /^\/[^\/]/.test(url)) {
|
|
url = cdn + url;
|
|
} else if (S3CDN) {
|
|
url = url.replace(S3BaseUrl, S3CDN);
|
|
}
|
|
return url;
|
|
}
|
|
|
|
export function getAbsoluteURL(path) {
|
|
return baseUrl + path;
|
|
}
|
|
|
|
export function isAbsoluteURL(url) {
|
|
return url.startsWith(baseUrl);
|
|
}
|
|
|
|
export function withoutPrefix(path) {
|
|
if (!baseUri) {
|
|
return path;
|
|
} else {
|
|
return path.replace(baseUriMatcher, "$1");
|
|
}
|
|
}
|
|
|
|
export function setPrefix(configBaseUri) {
|
|
baseUri = configBaseUri;
|
|
baseUriMatcher = new RegExp(`^${baseUri}(/|$)`);
|
|
}
|
|
|
|
export function setupURL(configCdn, configBaseUrl, configBaseUri) {
|
|
cdn = configCdn;
|
|
baseUrl = configBaseUrl;
|
|
setPrefix(configBaseUri);
|
|
}
|
|
|
|
export function setupS3CDN(configS3BaseUrl, configS3CDN) {
|
|
S3BaseUrl = configS3BaseUrl;
|
|
S3CDN = configS3CDN;
|
|
}
|