diff --git a/packages/router/src/__tests__/router.test.tsx b/packages/router/src/__tests__/router.test.tsx
index f9d20079a826..5a3470322949 100644
--- a/packages/router/src/__tests__/router.test.tsx
+++ b/packages/router/src/__tests__/router.test.tsx
@@ -1191,6 +1191,33 @@ test('jump to new route, then go back', async () => {
await waitFor(() => screen.getByText('Home Page'))
})
+test('redirect replacing route', async () => {
+ const ListWithDefaultParamsPage = (props) => {
+ if (props['_limit']) {
+ return
List Page
+ }
+ return
+ }
+ const TestRouter = () => (
+
+
+
+
+ )
+ const screen = render()
+
+ // starts on home page
+ await waitFor(() => screen.getByText('Home Page'))
+
+ // This will navigate to /list, which will then redirect to /list?_limit=10
+ // which will render `List Page
`
+ act(() => navigate(routes.list()))
+ await waitFor(() => screen.getByText('List Page'))
+ act(() => back())
+ // without options.replace = true in Redirect, back would go to List Page
+ await waitFor(() => screen.getByText('Home Page'))
+})
+
describe('trailing slashes', () => {
const TSNeverRouter = () => (
diff --git a/packages/router/src/links.tsx b/packages/router/src/links.tsx
index 3132f2dac68b..d836624fd16f 100644
--- a/packages/router/src/links.tsx
+++ b/packages/router/src/links.tsx
@@ -1,6 +1,6 @@
import { forwardRef, useEffect } from 'react'
-import { navigate } from './history'
+import { navigate, NavigateOptions } from './history'
import { useLocation } from './location'
import { flattenSearchParams, matchPath } from './util'
@@ -151,13 +151,14 @@ const NavLink = forwardRef<
interface RedirectProps {
/** The path to redirect to */
to: string
+ options?: NavigateOptions
}
/**
* A declarative way to redirect to a route name
*/
-const Redirect = ({ to }: RedirectProps) => {
- useEffect(() => navigate(to), [to])
+const Redirect = ({ to, options }: RedirectProps) => {
+ useEffect(() => navigate(to, options), [to, options])
return null
}