Skip to content

Provisioning: Stop doubling the app sub path after a provisioned dashboard save - #136808

Merged
Clarity-89 merged 3 commits into
grafana:mainfrom
Vivek1898:fix/136789-provisioned-save-subpath
Nov 3, 2026
Merged

Clarity-89 merged 3 commits into
grafana:mainfrom
Vivek1898:fix/136789-provisioned-save-subpath

Conversation

@Vivek1898

@Vivek1898 Vivek1898 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

What is this feature?

Fixes #136789.

When Grafana is served from a sub path (root_url = http://host/graf1, serve_from_sub_path = true), saving a dashboard into a folder backed by a local file-provisioning repository with the write workflow redirects to /graf1/graf1/d/<uid>/<slug> and shows "Page not found". The save itself succeeds.

The router history is created with config.appSubUrl as its basename (LocationService.tsx), so useNavigate() already resolves every path under the sub path. The provisioned save flows wrapped the paths they hand to navigate() in locationUtil.assureBaseUrl(...), which prefixes the sub path a second time:

  • SaveProvisionedDashboardForm.handleNewDashboard (new dashboard saved to a file-provisioned folder, the case in the issue)
  • useImportProvisionedSave (the dashboard redirect after a configured-branch import save, and the folder fallback when sync is disabled)

This change passes the bare paths to navigate() in those three places. The dashboard path is built by a new getSavedDashboardPath(uid, title) in utils/redirect.ts, next to the other post-save redirect builders, so both flows share it and the "why bare" lives in one doc comment. The url field of the save response DTO in createSaveResponseFromResource keeps assureBaseUrl, since that value is a link, not a router target, and the same applies to the href in DashboardPreviewBanner.

Why do we need this feature?

Same class of bug as #129581 and #128972: anything that goes through the router must not carry the sub path itself.

Who is this feature for?

Anyone running Grafana under a sub path who saves dashboards into Git Sync / file-provisioned folders.

Which issue(s) does this PR fix?

Fixes #136789

Special notes for your reviewer:

  • Tests: the existing save tests in SaveProvisionedDashboardForm.test.tsx and useImportProvisionedSave.test.ts now run with appSubUrl: '/grafana' and assert the exact path handed to navigate() (/d/new-dashboard/new-dashboard, /d/new-dash-uid/my-dashboard). They used stringContaining('/d/...') before, which a doubled sub path also satisfied. appSubUrl: '/grafana' is set in each file's beforeEach, so every navigate() assertion in both files runs under a sub path.
  • redirect.test.ts covers the one decision getSavedDashboardPath makes: it keeps the current query params.

Please check that:

  • It works as expected from a user's perspective.
  • If this is a pre-GA feature, it is behind a feature toggle. (N/A: bug fix)
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc. (N/A)

…board save

The router history is created with config.appSubUrl as its basename, so
navigate() already resolves under the sub path. The provisioned save flows
wrapped their redirect paths in locationUtil.assureBaseUrl first, which
sent users to /<subpath>/<subpath>/d/... and a "Page not found" page when
Grafana runs with serve_from_sub_path. Pass the bare paths instead in
SaveProvisionedDashboardForm.handleNewDashboard and in both redirects of
useImportProvisionedSave. The save response URL and the preview banner
href keep the prefix: they are links, not router targets.

Fixes grafana#136789
@Vivek1898
Vivek1898 requested a review from a team as a code owner November 3, 2026 02:41
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Signed commits report

All 3 commits between main and fix/136789-provisioned-save-subpath have verified signatures. ✅

@github-actions github-actions Bot added this to the 13.4.x milestone Nov 3, 2026
@github-actions github-actions Bot added the pr/external This PR is from external contributor label Nov 3, 2026
Comment on lines +213 to +220
// Navigation for new dashboards. The router history already carries the app
// sub path as its basename, so navigate() must get the bare path: prefixing
// it here lands on /<subpath>/<subpath>/d/... (#136789).
const url = getDashboardUrl({
uid: upsert.metadata.name,
slug: kbn.slugifyForUrl(upsert.spec.title ?? ''),
currentQueryParams: window.location.search,
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same block as useImportProvisionedSave.ts:123-127, and this PR edits both. Consider one helper next to the other post-save redirect builders in utils/redirect.ts:

/** Router path of a dashboard saved through provisioning. Bare: the router basename already carries appSubUrl. */
export function getSavedDashboardPath(uid: string, title = '') {
  return getDashboardUrl({ uid, slug: kbn.slugifyForUrl(title), currentQueryParams: window.location.search });
}

handleNewDashboard then becomes navigate(getSavedDashboardPath(upsert.metadata.name, upsert.spec.title)), and the "why bare" lives in one doc comment. The call-site comments and the #136789 references can go: bare paths to navigate() are already the norm in this file (lines 235 and 264), and issue numbers in comments go stale.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 9a9f974: added getSavedDashboardPath(uid, title = '') to utils/redirect.ts with the one doc comment, and handleNewDashboard is now the single navigate(getSavedDashboardPath(...)) call. Both call-site comments and the issue references are gone. Added three small unit tests for the helper in redirect.test.ts (slug, no title, query params kept).

Comment on lines +118 to +127
// navigate() resolves against the router basename, which is already the
// app sub path, so the paths below must stay bare (#136789).
navigate(targetFolderUid ? `/dashboards/f/${targetFolderUid}/` : '/dashboards');
return;
}
const url = locationUtil.assureBaseUrl(
getDashboardUrl({
uid: dashboardUid,
slug: kbn.slugifyForUrl(title),
currentQueryParams: window.location.search,
})
);
const url = getDashboardUrl({
uid: dashboardUid,
slug: kbn.slugifyForUrl(title),
currentQueryParams: window.location.search,
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same builder as SaveProvisionedDashboardForm.tsx:216-220; see the helper suggestion there. Also, this comment sits inside the !dashboardUid branch but describes both navigate() calls. With the helper it can go.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to the helper here too, so kbn and getDashboardUrl are no longer imported in this hook, and the comment inside the !dashboardUid branch is removed.

expect(dashboardWatcher.clearIgnoreSave).toHaveBeenCalled();
});

it('navigates to the new dashboard without repeating the app sub path', async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug got past the existing test because line 367 uses expect.stringContaining('/d/new-dashboard'), which /grafana/d/new-dashboard also satisfies. Rather than a second copy of that test, consider setting appSubUrl: '/grafana' inside the existing test (reset in a file-level beforeEach, as DashboardPreviewBanner.test.tsx:176-185 does) and changing line 367 to the exact path. That catches the regression with fewer lines, and the stringMatching plus not.toHaveBeenCalledWith pair here becomes unnecessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that is a better test. Dropped the copy; should save a new dashboard successfully now sets appSubUrl: '/grafana' and asserts '/d/new-dashboard/new-dashboard' exactly, with the locationUtil reset in the file-level beforeEach as in DashboardPreviewBanner.test.tsx.

expect(result.current.error).toBeUndefined();
});

describe('when Grafana is served from a sub path', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same idea as the form test: line 252 uses stringContaining, which also matches the prefixed path. Consider setting the sub path for the save outcomes describe and changing line 252 to the exact path. Line 274 already asserts the exact folder path, so it covers the fallback case with no new test. This describe can then go.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: the save outcomes describe sets the sub path in its own beforeEach (reset at file level), line 252 asserts '/d/new-dash-uid/my-dashboard' exactly, and the extra describe is removed. The folder fallback stays covered by the existing exact assertion.

Both provisioned save flows built the same dashboard path by hand. Move it
into getSavedDashboardPath next to the other post-save redirect builders,
where one doc comment explains why the path stays bare, and drop the
call-site comments.

Tighten the existing save tests instead of adding copies: they now run with
appSubUrl set and assert the exact path, which is what let the doubled sub
path through before.
Comment on lines +272 to +277
// locationUtil keeps module-level config, so reset it between tests
locationUtil.initialize({
config: { appSubUrl: '' } as GrafanaConfig,
getTimeRangeForUrl: jest.fn(),
getVariablesUrlParams: jest.fn(),
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No test in this file needs the sub path back at '': nothing asserts a prefixed href or the saveCompleted url, and Jest gives each file its own module registry, so nothing leaks across files. Consider setting /grafana once here and dropping the block in should save a new dashboard successfully. One block instead of two, and the other navigate assertions further down (/d/test-dashboard, the preview path) get sub-path coverage too. Worth a run of the file to confirm nothing else depends on the empty base.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in db03585: the file-level beforeEach now sets /grafana and the block in should save a new dashboard successfully is gone, so /d/test-dashboard and the preview path assertions run under the sub path too. I checked the file for anything reading a prefixed value (no href assertions, saveCompleted is only checked with expect.anything() for the url) and found none; I could not run Jest locally, so CI is the confirmation here.

Comment on lines +51 to +56
// locationUtil keeps module-level config, so reset it between tests
locationUtil.initialize({
config: { appSubUrl: '' } as GrafanaConfig,
getTimeRangeForUrl: jest.fn(),
getVariablesUrlParams: jest.fn(),
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: set /grafana in this beforeEach and drop the one in save outcomes. The remaining tests only inspect the request body or error, so they never read the sub path.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: /grafana is set in the file-level beforeEach and the save outcomes block is removed.

Comment on lines +100 to +106
it('builds the dashboard path from the uid and the slugified title', () => {
expect(getSavedDashboardPath('abc', 'My Dashboard')).toBe('/d/abc/my-dashboard');
});

it('omits the slug when there is no title', () => {
expect(getSavedDashboardPath('abc')).toBe('/d/abc');
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: these two cases re-test kbn.slugifyForUrl and getDashboardUrl's slug handling. The query-param case below is the one decision this helper makes (window.location.search rather than '', which the DTO builder in the form passes). Consider keeping only that one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, dropped the two slug cases; only the query-param case remains.

Set appSubUrl once per file instead of per test, so every navigate()
assertion in the two files runs under a sub path, and keep only the
query-param case for getSavedDashboardPath: the slug cases re-tested
getDashboardUrl.
@Clarity-89
Clarity-89 merged commit b182a4f into grafana:main Nov 3, 2026
125 checks passed
fastfrwrd pushed a commit that referenced this pull request Nov 11, 2026
…board save (#136808)

* Provisioning: Stop doubling the app sub path after a provisioned dashboard save

The router history is created with config.appSubUrl as its basename, so
navigate() already resolves under the sub path. The provisioned save flows
wrapped their redirect paths in locationUtil.assureBaseUrl first, which
sent users to /<subpath>/<subpath>/d/... and a "Page not found" page when
Grafana runs with serve_from_sub_path. Pass the bare paths instead in
SaveProvisionedDashboardForm.handleNewDashboard and in both redirects of
useImportProvisionedSave. The save response URL and the preview banner
href keep the prefix: they are links, not router targets.

Fixes #136789

* Provisioning: Build the saved-dashboard path in one place

Both provisioned save flows built the same dashboard path by hand. Move it
into getSavedDashboardPath next to the other post-save redirect builders,
where one doc comment explains why the path stays bare, and drop the
call-site comments.

Tighten the existing save tests instead of adding copies: they now run with
appSubUrl set and assert the exact path, which is what let the doubled sub
path through before.

* Provisioning: Run both save test files under a sub path

Set appSubUrl once per file instead of per test, so every navigate()
assertion in the two files runs under a sub path, and keep only the
query-param case for getSavedDashboardPath: the slug cases re-tested
getDashboardUrl.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/frontend pr/external This PR is from external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provisioning: saving a dashboard into a local file-provisioned folder duplicates the sub path in the redirect when served from a sub path

2 participants