Wrong destination path calculated on navigation when "basepath" is set for Router
Which project does this relate to?
Router
Describe the bug
The issue reproduces when creating Router instance with basepath router option specified.
When navigating via <Link> or using the function from useNavigate hook, it does not work properly if the to property/ option is not specified.
In this case, the core router logic takes fullPath value from the latest route match which does not include the base path part: https://github.com/TanStack/router/blob/bab29d0e88945fa72f55ca3284461bb47c550f50/packages/router-core/src/router.ts#L1438-L1441
The navigation works as expected if the to property/option is specified, as a full or relative path.
Your Example Website or App
https://stackblitz.com/edit/github-cwqd7wap?file=src%2Fmain.tsx
Steps to Reproduce the Bug or Issue
Create a Router instance with basepath option specified.
const router = createRouter({
routeTree,
basepath: '/base',
// ...
});
Navigate using <Link> or useNavigate hook. Do not specify to property/option.
// Navigated to /base/some-page
// Navigates to "/some-page?foo=bar" instead of "/base/some-page?foo-bar"
<Link search={{ foo: 'bar' }}>Update search params</Link>
// Also navigates to "/some-page?foo=bar" instead of "/base/some-page?foo-bar"
const navigate = useNavigate();
navigate({ search: { foo: 'bar' } });
Expected behavior
When the to property/option is not specified, the destination path is expected be calculated properly including base path.
Screenshots or Videos
No response
Platform
- OS: any
- Browser: any
- Version: "@tanstack/react-router": "^1.121.0"
Additional context
No response
also running into this
Same. Using to="." doesn't change the behavior.
// Navigates to "/some-page?foo=bar" instead of "/base/some-page?foo-bar"
<Link to={"."} search={{ foo: 'bar' }}>Update search params</Link>
Did anyone find out in what version the regression happened? Using i.e. useNavigate this way worked for me before.
probably caused by 1.121.0
@vasiliy-l - Testing the fixes by @jvllmr in #4472 against the test sandbox, the issues described looks to be fixed.
The fix for this should be available in this release - https://github.com/TanStack/router/releases/tag/v1.121.33
@gynekolog I couldn't reproduce an error/incorrect navigation state using your comment.
Same. Using
to="."doesn't change the behavior.// Navigates to "/some-page?foo=bar" instead of "/base/some-page?foo-bar" <Link to={"."} search={{ foo: 'bar' }}>Update search params</Link>
Please open a new issue if the fixes from #4472 in release 1.121.33 does not fix the issue you are facing.