Skip to content

Commit

Permalink
fix: from was being ignored when the pathname included params or a …
Browse files Browse the repository at this point in the history
…pathless route (#1703)

* fix: `from` was being ignored when the pathname included params or a pathless route

* tests: change description of test to match current location

* fix: apply changes to useNavigate and redirect, also add more tests

* chore: inline looseRoutesByPath
  • Loading branch information
chorobin authored Jun 4, 2024
1 parent cde4ca9 commit 56a3100
Show file tree
Hide file tree
Showing 5 changed files with 1,450 additions and 9 deletions.
1 change: 0 additions & 1 deletion packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,6 @@ export function useLinkProps<
// null for LinkUtils
const dest = {
...(options.to && { from: matchPathname }),
...options,
}
Expand Down
23 changes: 17 additions & 6 deletions packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1102,14 +1102,27 @@ export class Router<
): ParsedLocation => {
let fromPath = this.latestLocation.pathname
let fromSearch = dest.fromSearch || this.latestLocation.search
const looseRoutesByPath = this.routesByPath as Record<string, AnyRoute>

const fromRoute =
dest.from !== undefined
? looseRoutesByPath[trimPathRight(dest.from)]
: undefined

const fromMatches = this.matchRoutes(
this.latestLocation.pathname,
fromSearch,
)

fromPath =
fromMatches.find((d) => d.id === dest.from)?.pathname || fromPath
const fromMatch = fromMatches.find((d) => d.routeId === fromRoute?.id)

fromPath = fromMatch?.pathname || fromPath

invariant(
dest.from == null || fromMatch != null,
'Could not find match for from: ' + dest.from,
)

fromSearch = last(fromMatches)?.search || this.latestLocation.search

const stayingMatches = matches?.filter((d) =>
Expand Down Expand Up @@ -1773,8 +1786,7 @@ export class Router<
preload: !!preload,
context: parentContext,
location,
navigate: (opts: any) =>
this.navigate({ ...opts, from: match.pathname }),
navigate: (opts: any) => this.navigate({ ...opts }),
buildLocation: this.buildLocation,
cause: preload ? 'preload' : match.cause,
})) ?? ({} as any)
Expand Down Expand Up @@ -1827,8 +1839,7 @@ export class Router<
abortController: match.abortController,
context: match.context,
location,
navigate: (opts) =>
this.navigate({ ...opts, from: match.pathname } as any),
navigate: (opts) => this.navigate({ ...opts } as any),
cause: preload ? 'preload' : match.cause,
route,
}
Expand Down
2 changes: 0 additions & 2 deletions packages/react-router/src/useNavigate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export function useNavigate<
(options: NavigateOptions) => {
return router.navigate({
...options,
from: options.to ? router.state.resolvedLocation.pathname : undefined,
})
},
[router],
Expand Down Expand Up @@ -63,7 +62,6 @@ export function Navigate<

React.useEffect(() => {
navigate({
from: props.to ? match.pathname : undefined,
...props,
} as any)
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
Loading

0 comments on commit 56a3100

Please sign in to comment.