mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Links: Fix links to other apps outside Grafana when under sub path (#36498)
This commit is contained in:
parent
3e95c3826a
commit
eed1f36613
@ -57,7 +57,7 @@ describe('locationUtil', () => {
|
||||
});
|
||||
test('absolute url with subdirectory subUrl', () => {
|
||||
const urlWithoutMaster = locationUtil.stripBaseFromUrl('http://www.domain.com:9877/thisShouldRemain/subUrl/');
|
||||
expect(urlWithoutMaster).toBe('/thisShouldRemain/subUrl/');
|
||||
expect(urlWithoutMaster).toBe('http://www.domain.com:9877/thisShouldRemain/subUrl/');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -17,16 +17,10 @@ const stripBaseFromUrl = (url: string): string => {
|
||||
const isAbsoluteUrl = url.startsWith('http');
|
||||
let segmentToStrip = appSubUrl;
|
||||
|
||||
if (!url.startsWith('/')) {
|
||||
if (!url.startsWith('/') || isAbsoluteUrl) {
|
||||
segmentToStrip = `${window.location.origin}${appSubUrl}`;
|
||||
}
|
||||
|
||||
if (isAbsoluteUrl) {
|
||||
segmentToStrip = url.startsWith(`${window.location.origin}${appSubUrl}`)
|
||||
? `${window.location.origin}${appSubUrl}`
|
||||
: `${window.location.origin}`;
|
||||
}
|
||||
|
||||
return url.length > 0 && url.indexOf(segmentToStrip) === 0 ? url.slice(segmentToStrip.length - stripExtraChars) : url;
|
||||
};
|
||||
|
||||
|
@ -18,20 +18,18 @@ export function interceptLinkClicks(e: MouseEvent) {
|
||||
e.preventDefault();
|
||||
|
||||
href = locationUtil.stripBaseFromUrl(href);
|
||||
|
||||
// Ensure old angular urls with no starting '/' are handled the same as before
|
||||
// Make sure external links are handled correctly
|
||||
// That is they where seen as being absolute from app root
|
||||
if (href[0] !== '/') {
|
||||
try {
|
||||
const external = new URL(href);
|
||||
if (external.origin !== window.location.origin) {
|
||||
window.location.href = external.toString();
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
// if still contains protocol it's an absolute link to another domain or web application
|
||||
if (href.indexOf('://')) {
|
||||
window.location.href = href;
|
||||
return;
|
||||
} else {
|
||||
href = `/${href}`;
|
||||
}
|
||||
href = `/${href}`;
|
||||
}
|
||||
locationService.push(href);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user