diff --git a/packages/grafana-data/src/utils/location.test.ts b/packages/grafana-data/src/utils/location.test.ts index d297507b554..174a505601d 100644 --- a/packages/grafana-data/src/utils/location.test.ts +++ b/packages/grafana-data/src/utils/location.test.ts @@ -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/'); }); }); diff --git a/packages/grafana-data/src/utils/location.ts b/packages/grafana-data/src/utils/location.ts index 55f9c658f0a..ac07baa4d94 100644 --- a/packages/grafana-data/src/utils/location.ts +++ b/packages/grafana-data/src/utils/location.ts @@ -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; }; diff --git a/public/app/core/navigation/patch/interceptLinkClicks.ts b/public/app/core/navigation/patch/interceptLinkClicks.ts index bbb509df3f3..a7a3e6db78f 100644 --- a/public/app/core/navigation/patch/interceptLinkClicks.ts +++ b/public/app/core/navigation/patch/interceptLinkClicks.ts @@ -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); }