Skip to content

Commit 4e959b4

Browse files
refactor(router-core): simplify handleRedirectAndNotFound (#4958)
Minor cleanup of `handleRedirectAndNotFound` in `router-core`. The code looked like there were 3 possible cases, but `err` is only handled if it's a redirect or a notFound. This PR is a no-op, it's just code cleanup without any functional changes. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent e8e7a74 commit 4e959b4

File tree

1 file changed

+43
-53
lines changed

1 file changed

+43
-53
lines changed

packages/router-core/src/router.ts

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,54 +2123,50 @@ export class RouterCore<
21232123

21242124
const handleRedirectAndNotFound = (
21252125
match: AnyRouteMatch | undefined,
2126-
err: any,
2126+
err: unknown,
21272127
) => {
2128-
if (isRedirect(err) || isNotFound(err)) {
2129-
if (isRedirect(err)) {
2130-
if (err.redirectHandled) {
2131-
if (!err.options.reloadDocument) {
2132-
throw err
2133-
}
2134-
}
2135-
}
2128+
if (!isRedirect(err) && !isNotFound(err)) return
21362129

2137-
// in case of a redirecting match during preload, the match does not exist
2138-
if (match) {
2139-
match._nonReactive.beforeLoadPromise?.resolve()
2140-
match._nonReactive.loaderPromise?.resolve()
2141-
match._nonReactive.beforeLoadPromise = undefined
2142-
match._nonReactive.loaderPromise = undefined
2143-
2144-
updateMatch(match.id, (prev) => ({
2145-
...prev,
2146-
status: isRedirect(err)
2147-
? 'redirected'
2148-
: isNotFound(err)
2149-
? 'notFound'
2150-
: 'error',
2151-
isFetching: false,
2152-
error: err,
2153-
}))
2130+
if (
2131+
isRedirect(err) &&
2132+
err.redirectHandled &&
2133+
!err.options.reloadDocument
2134+
) {
2135+
throw err
2136+
}
21542137

2155-
if (!(err as any).routeId) {
2156-
;(err as any).routeId = match.routeId
2157-
}
2138+
// in case of a redirecting match during preload, the match does not exist
2139+
if (match) {
2140+
match._nonReactive.beforeLoadPromise?.resolve()
2141+
match._nonReactive.loaderPromise?.resolve()
2142+
match._nonReactive.beforeLoadPromise = undefined
2143+
match._nonReactive.loaderPromise = undefined
21582144

2159-
match._nonReactive.loadPromise?.resolve()
2160-
}
2145+
const status = isRedirect(err) ? 'redirected' : 'notFound'
21612146

2162-
if (isRedirect(err)) {
2163-
rendered = true
2164-
err.options._fromLocation = location
2165-
err.redirectHandled = true
2166-
err = this.resolveRedirect(err)
2167-
throw err
2168-
} else if (isNotFound(err)) {
2169-
this._handleNotFound(matches, err, {
2170-
updateMatch,
2171-
})
2172-
throw err
2147+
updateMatch(match.id, (prev) => ({
2148+
...prev,
2149+
status,
2150+
isFetching: false,
2151+
error: err,
2152+
}))
2153+
2154+
if (isNotFound(err) && !err.routeId) {
2155+
err.routeId = match.routeId
21732156
}
2157+
2158+
match._nonReactive.loadPromise?.resolve()
2159+
}
2160+
2161+
if (isRedirect(err)) {
2162+
rendered = true
2163+
err.options._fromLocation = location
2164+
err.redirectHandled = true
2165+
err = this.resolveRedirect(err)
2166+
throw err
2167+
} else {
2168+
this._handleNotFound(matches, err, updateMatch)
2169+
throw err
21742170
}
21752171
}
21762172

@@ -3063,14 +3059,10 @@ export class RouterCore<
30633059
_handleNotFound = (
30643060
matches: Array<AnyRouteMatch>,
30653061
err: NotFoundError,
3066-
{
3067-
updateMatch = this.updateMatch,
3068-
}: {
3069-
updateMatch?: (
3070-
id: string,
3071-
updater: (match: AnyRouteMatch) => AnyRouteMatch,
3072-
) => void
3073-
} = {},
3062+
updateMatch: (
3063+
id: string,
3064+
updater: (match: AnyRouteMatch) => AnyRouteMatch,
3065+
) => void = this.updateMatch,
30743066
) => {
30753067
// Find the route that should handle the not found error
30763068
// First check if a specific route is requested to show the error
@@ -3116,9 +3108,7 @@ export class RouterCore<
31163108

31173109
if ((err as any).routerCode === 'BEFORE_LOAD' && routeCursor.parentRoute) {
31183110
err.routeId = routeCursor.parentRoute.id
3119-
this._handleNotFound(matches, err, {
3120-
updateMatch,
3121-
})
3111+
this._handleNotFound(matches, err, updateMatch)
31223112
}
31233113
}
31243114

0 commit comments

Comments
 (0)