Skip to content

Commit da77078

Browse files
authored
fix(solid-router): setting search params with 2 parallel navigate calls (#5797)
1 parent cfe7ee9 commit da77078

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/router-core/src/router.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ export class RouterCore<
896896
rewrite?: LocationRewrite
897897
origin?: string
898898
latestLocation!: ParsedLocation<FullSearchSchema<TRouteTree>>
899+
pendingBuiltLocation?: ParsedLocation<FullSearchSchema<TRouteTree>>
899900
basepath!: string
900901
routeTree!: TRouteTree
901902
routesById!: RoutesById<TRouteTree>
@@ -1593,7 +1594,8 @@ export class RouterCore<
15931594
} = {},
15941595
): ParsedLocation => {
15951596
// We allow the caller to override the current location
1596-
const currentLocation = dest._fromLocation || this.latestLocation
1597+
const currentLocation =
1598+
dest._fromLocation || this.pendingBuiltLocation || this.latestLocation
15971599

15981600
const allCurrentLocationMatches = this.matchRoutes(currentLocation, {
15991601
_buildLocation: true,
@@ -1956,14 +1958,28 @@ export class RouterCore<
19561958
_includeValidateSearch: true,
19571959
})
19581960

1959-
return this.commitLocation({
1961+
this.pendingBuiltLocation = location as ParsedLocation<
1962+
FullSearchSchema<TRouteTree>
1963+
>
1964+
1965+
const commitPromise = this.commitLocation({
19601966
...location,
19611967
viewTransition,
19621968
replace,
19631969
resetScroll,
19641970
hashScrollIntoView,
19651971
ignoreBlocker,
19661972
})
1973+
1974+
// Clear pending location after commit starts
1975+
// We do this on next microtask to allow synchronous navigate calls to chain
1976+
Promise.resolve().then(() => {
1977+
if (this.pendingBuiltLocation === location) {
1978+
this.pendingBuiltLocation = undefined
1979+
}
1980+
})
1981+
1982+
return commitPromise
19671983
}
19681984

19691985
/**

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ test('when navigating to /posts/$postId/info which is imperatively masked as /po
12531253
expect(window.location.pathname).toEqual('/posts/id1')
12541254
})
12551255

1256-
test.skip('when setting search params with 2 parallel navigate calls', async () => {
1256+
test('when setting search params with 2 parallel navigate calls', async () => {
12571257
const rootRoute = createRootRoute()
12581258

12591259
const IndexComponent = () => {
@@ -1301,6 +1301,9 @@ test.skip('when setting search params with 2 parallel navigate calls', async ()
13011301

13021302
render(() => <RouterProvider router={router} />)
13031303

1304+
// Wait for router to initialize with defaults
1305+
await screen.findByTestId('param1')
1306+
13041307
// console.log(router.state.location)
13051308
expect(router.state.location.search).toEqual({
13061309
param1: 'param1-default',

0 commit comments

Comments
 (0)