mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Routing: Remove subPath only at start of href (#35416)
* fix(routing): remove baseUrl only if at start of url * test(routing): add subdirectory suburl tests for stripBaseFromUrl * test(routing): add absolute url check to fix tests, add extra tests
This commit is contained in:
parent
83b6df738e
commit
8b2ee06f3c
@ -34,12 +34,30 @@ describe('locationUtil', () => {
|
||||
});
|
||||
});
|
||||
test('relative url', () => {
|
||||
const urlWithoutMaster = locationUtil.stripBaseFromUrl('/subUrl/grafana/');
|
||||
expect(urlWithoutMaster).toBe('/grafana/');
|
||||
const urlWithoutMaster = locationUtil.stripBaseFromUrl('/subUrl/thisShouldRemain/');
|
||||
expect(urlWithoutMaster).toBe('/thisShouldRemain/');
|
||||
});
|
||||
test('absolute url url', () => {
|
||||
const urlWithoutMaster = locationUtil.stripBaseFromUrl('http://www.domain.com:9877/subUrl/grafana/');
|
||||
expect(urlWithoutMaster).toBe('/grafana/');
|
||||
test('relative url with multiple subUrl in path', () => {
|
||||
const urlWithoutMaster = locationUtil.stripBaseFromUrl('/subUrl/thisShouldRemain/subUrl/');
|
||||
expect(urlWithoutMaster).toBe('/thisShouldRemain/subUrl/');
|
||||
});
|
||||
test('relative url with subdirectory subUrl', () => {
|
||||
const urlWithoutMaster = locationUtil.stripBaseFromUrl('/thisShouldRemain/subUrl/');
|
||||
expect(urlWithoutMaster).toBe('/thisShouldRemain/subUrl/');
|
||||
});
|
||||
test('absolute url', () => {
|
||||
const urlWithoutMaster = locationUtil.stripBaseFromUrl('http://www.domain.com:9877/subUrl/thisShouldRemain/');
|
||||
expect(urlWithoutMaster).toBe('/thisShouldRemain/');
|
||||
});
|
||||
test('absolute url with multiple subUrl in path', () => {
|
||||
const urlWithoutMaster = locationUtil.stripBaseFromUrl(
|
||||
'http://www.domain.com:9877/subUrl/thisShouldRemain/subUrl/'
|
||||
);
|
||||
expect(urlWithoutMaster).toBe('/thisShouldRemain/subUrl/');
|
||||
});
|
||||
test('absolute url with subdirectory subUrl', () => {
|
||||
const urlWithoutMaster = locationUtil.stripBaseFromUrl('http://www.domain.com:9877/thisShouldRemain/subUrl/');
|
||||
expect(urlWithoutMaster).toBe('/thisShouldRemain/subUrl/');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -17,13 +17,17 @@ const stripBaseFromUrl = (url: string): string => {
|
||||
const isAbsoluteUrl = url.startsWith('http');
|
||||
let segmentToStrip = appSubUrl;
|
||||
|
||||
if (isAbsoluteUrl || !url.startsWith('/')) {
|
||||
if (!url.startsWith('/')) {
|
||||
segmentToStrip = `${window.location.origin}${appSubUrl}`;
|
||||
}
|
||||
|
||||
return url.length > 0 && url.indexOf(segmentToStrip) !== -1
|
||||
? url.slice(segmentToStrip.length - stripExtraChars)
|
||||
: url;
|
||||
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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user