When generating the pubdash url, use the base url from the Grafana config. This allows the url generation to work in the case that Grafana is hosted on a subpath. (#55204)

Support subpaths when generating public dashboard url
This commit is contained in:
owensmallwood 2022-09-15 09:46:11 -06:00 committed by GitHub
parent 00e7324bf6
commit 5121e32722
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View File

@ -1,5 +1,7 @@
import { VariableModel } from 'app/features/variables/types';
import { updateConfig } from '../../../../core/config';
import {
PublicDashboard,
dashboardHasTemplateVariables,
@ -23,9 +25,13 @@ describe('dashboardHasTemplateVariables', () => {
});
describe('generatePublicDashboardUrl', () => {
it('has the right uid', () => {
let pubdash = { accessToken: 'abcd1234' } as PublicDashboard;
expect(generatePublicDashboardUrl(pubdash)).toEqual(`${window.location.origin}/public-dashboards/abcd1234`);
it('uses the grafana config appUrl to generate the url', () => {
const appUrl = 'http://localhost/';
const accessToken = 'abcd1234';
updateConfig({ appUrl });
let pubdash = { accessToken } as PublicDashboard;
expect(generatePublicDashboardUrl(pubdash)).toEqual(`${appUrl}public-dashboards/${accessToken}`);
});
});

View File

@ -1,5 +1,6 @@
import { getBackendSrv } from '@grafana/runtime';
import { notifyApp } from 'app/core/actions';
import { getConfig } from 'app/core/config';
import { createSuccessNotification } from 'app/core/copy/appNotification';
import { VariableModel } from 'app/features/variables/types';
import { dispatch } from 'app/store/store';
@ -61,6 +62,14 @@ export const publicDashboardPersisted = (publicDashboard: PublicDashboard): bool
return publicDashboard.uid !== '' && publicDashboard.uid !== undefined;
};
/**
* Generate the public dashboard url. Uses the appUrl from the Grafana boot config, so urls will also be correct
* when Grafana is hosted on a subpath.
*
* All app urls from the Grafana boot config end with a slash.
*
* @param publicDashboard
*/
export const generatePublicDashboardUrl = (publicDashboard: PublicDashboard): string => {
return `${window.location.origin}/public-dashboards/${publicDashboard.accessToken}`;
return `${getConfig().appUrl}public-dashboards/${publicDashboard.accessToken}`;
};