Skip to content

Commit

Permalink
fix(react-router): navigate if redirect is thrown in component
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel committed Jan 3, 2025
1 parent 5c8c38a commit b9c2da2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/react-router/src/CatchBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as React from 'react'
import { useRouter } from './useRouter'
import { isRedirect } from './redirects'
import type { ErrorRouteComponent } from './route'
import type { ErrorInfo } from 'react'

Expand All @@ -9,13 +11,19 @@ export function CatchBoundary(props: {
onCatch?: (error: Error, errorInfo: ErrorInfo) => void
}) {
const errorComponent = props.errorComponent ?? ErrorComponent
const router = useRouter()

return (
<CatchBoundaryImpl
getResetKey={props.getResetKey}
onCatch={props.onCatch}
children={({ error, reset }) => {
if (error) {
if (isRedirect(error)) {
const redirect = router.resolveRedirect(error)
router.navigate({ ...redirect, replace: true, ignoreBlocker: true })
return null
}
return React.createElement(errorComponent, {
error,
reset,
Expand Down
6 changes: 5 additions & 1 deletion packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2696,7 +2696,11 @@ export class Router<
}))
} catch (err) {
if (isResolvedRedirect(err)) {
await this.navigate(err)
await this.navigate({
...err,
replace: true,
ignoreBlocker: true,
})
}
}
})()
Expand Down

0 comments on commit b9c2da2

Please sign in to comment.