discourse/app/assets/javascripts/discourse-common/addon/lib/get-url.js
Krzysztof Kotlarek 5cbb522c41
FIX: broken URL when username contains subfolder. (#11786)
The bug was mentioned on [meta](https://meta.discourse.org/t/two-bugs-with-usernames-starting-with-subfolder-name/169505)

When discourse is installed on `/subfolder` and username is containing subfolder name like for example `subfolderadmin` - user URLs were incorrect.

Instead of having `/subfolder/u/subfolderadmin/summary/` we were leading to `/subfolder/uadmin/summary`.

The reason for that was incorrect check in `getUrl` helper:

```javascript
  const found = url.indexOf(baseUri);
  if (found >= 0 && found < 3) {
    return url;
  }
  return baseUri + url;
```
baseUri is `/subfolder`, url is `/u/subfolderadmin` and indexOf returned position which in the end returned incorrect URL.

I think that we should check if the URL starts with baseUri and not if contains baseUri.
2021-01-22 08:43:14 +11:00

68 lines
1.3 KiB
JavaScript

let cdn, baseUrl, baseUri;
let S3BaseUrl, S3CDN;
export default function getURL(url) {
if (baseUri === undefined) {
baseUri = $('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 = url.startsWith(baseUri);
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) {
const rootURL = (!baseUri ? "/" : baseUri).replace(/\/$/, "");
return path.replace(rootURL, "");
}
export function setPrefix(configBaseUri) {
baseUri = configBaseUri;
}
export function setupURL(configCdn, configBaseUrl, configBaseUri) {
cdn = configCdn;
baseUrl = configBaseUrl;
baseUri = configBaseUri;
}
export function setupS3CDN(configS3BaseUrl, configS3CDN) {
S3BaseUrl = configS3BaseUrl;
S3CDN = configS3CDN;
}