Skip to content

Commit 785681f

Browse files
jvllmrSeanCassiere
andauthored
fix(router-core): resolve with basepath if to is not supplied (#4472)
Co-authored-by: Sean Cassiere <[email protected]>
1 parent a211a4d commit 785681f

File tree

8 files changed

+1112
-466
lines changed

8 files changed

+1112
-466
lines changed

packages/react-router/tests/link.test.tsx

Lines changed: 301 additions & 266 deletions
Large diffs are not rendered by default.

packages/react-router/tests/useNavigate.test.tsx

Lines changed: 201 additions & 191 deletions
Large diffs are not rendered by default.

packages/router-core/src/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ export class RouterCore<
14381438
// Resolve the next to
14391439
const nextTo = dest.to
14401440
? this.resolvePathWithBase(fromPath, `${dest.to}`)
1441-
: fromPath
1441+
: this.resolvePathWithBase(fromPath, '.')
14421442

14431443
// Resolve the next params
14441444
let nextParams =

packages/router-core/tests/path.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,14 @@ describe('resolvePath', () => {
178178
['/', '/a/b/c', './d', '/a/b/c/d'],
179179
['/', '/a/b/c', './../d', '/a/b/d'],
180180
['/', '/a/b/c/d', './../d', '/a/b/c/d'],
181-
['/', '/a/b/c', '../d', '/a/b/d'],
182181
['/', '/a/b/c', '../../d', '/a/d'],
182+
['/', '/a/b/c', '../d', '/a/b/d'],
183183
['/', '/a/b/c', '..', '/a/b'],
184184
['/', '/a/b/c', '../..', '/a'],
185185
['/', '/a/b/c', '../../..', '/'],
186186
['/', '/a/b/c/', '../../..', '/'],
187187
['/products', '/', '/products-list', '/products/products-list'],
188+
['/basepath', '/products', '.', '/basepath/products'],
188189
])('resolves correctly', (base, a, b, eq) => {
189190
it(`Base: ${base} - ${a} to ${b} === ${eq}`, () => {
190191
expect(resolvePath({ basepath: base, base: a, to: b })).toEqual(eq)

packages/solid-router/src/link.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { useRouter } from './useRouter'
1515

1616
import { useIntersectionObserver } from './utils'
1717

18-
import { useMatches } from './Matches'
18+
import { useMatch } from './useMatch'
1919
import type {
2020
AnyRouter,
2121
Constrain,
@@ -132,10 +132,11 @@ export function useLinkProps<
132132
select: (s) => s.location.searchStr,
133133
})
134134

135-
// when `from` is not supplied, use the leaf route of the current matches as the `from` location
135+
// when `from` is not supplied, use the route of the current match as the `from` location
136136
// so relative routing works as expected
137-
const from = useMatches({
138-
select: (matches) => options.from ?? matches[matches.length - 1]?.fullPath,
137+
const from = useMatch({
138+
strict: false,
139+
select: (match) => options.from ?? match.fullPath,
139140
})
140141

141142
const _options = () => ({

packages/solid-router/src/useNavigate.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Solid from 'solid-js'
22
import { useRouter } from './useRouter'
3+
import { useMatch } from './useMatch'
34
import type {
45
AnyRouter,
56
FromPathOption,
@@ -14,10 +15,21 @@ export function useNavigate<
1415
>(_defaultOpts?: {
1516
from?: FromPathOption<TRouter, TDefaultFrom>
1617
}): UseNavigateResult<TDefaultFrom> {
17-
const { navigate } = useRouter()
18+
const { navigate, state } = useRouter()
19+
20+
const matchIndex = useMatch({
21+
strict: false,
22+
select: (match) => match.index,
23+
})
1824

1925
return ((options: NavigateOptions) => {
20-
return navigate({ from: _defaultOpts?.from as any, ...options })
26+
return navigate({
27+
...options,
28+
from:
29+
options.from ??
30+
_defaultOpts?.from ??
31+
state.matches[matchIndex()]!.fullPath,
32+
})
2133
}) as UseNavigateResult<TDefaultFrom>
2234
}
2335

0 commit comments

Comments
 (0)