Skip to content

Commit 22223a3

Browse files
Sheraffautofix-ci[bot]
authored andcommitted
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 a9c734b commit 22223a3

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
@@ -2116,54 +2116,50 @@ export class RouterCore<
21162116

21172117
const handleRedirectAndNotFound = (
21182118
match: AnyRouteMatch | undefined,
2119-
err: any,
2119+
err: unknown,
21202120
) => {
2121-
if (isRedirect(err) || isNotFound(err)) {
2122-
if (isRedirect(err)) {
2123-
if (err.redirectHandled) {
2124-
if (!err.options.reloadDocument) {
2125-
throw err
2126-
}
2127-
}
2128-
}
2121+
if (!isRedirect(err) && !isNotFound(err)) return
21292122

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

2148-
if (!(err as any).routeId) {
2149-
;(err as any).routeId = match.routeId
2150-
}
2131+
// in case of a redirecting match during preload, the match does not exist
2132+
if (match) {
2133+
match._nonReactive.beforeLoadPromise?.resolve()
2134+
match._nonReactive.loaderPromise?.resolve()
2135+
match._nonReactive.beforeLoadPromise = undefined
2136+
match._nonReactive.loaderPromise = undefined
21512137

2152-
match._nonReactive.loadPromise?.resolve()
2153-
}
2138+
const status = isRedirect(err) ? 'redirected' : 'notFound'
21542139

2155-
if (isRedirect(err)) {
2156-
rendered = true
2157-
err.options._fromLocation = location
2158-
err.redirectHandled = true
2159-
err = this.resolveRedirect(err)
2160-
throw err
2161-
} else if (isNotFound(err)) {
2162-
this._handleNotFound(matches, err, {
2163-
updateMatch,
2164-
})
2165-
throw err
2140+
updateMatch(match.id, (prev) => ({
2141+
...prev,
2142+
status,
2143+
isFetching: false,
2144+
error: err,
2145+
}))
2146+
2147+
if (isNotFound(err) && !err.routeId) {
2148+
err.routeId = match.routeId
21662149
}
2150+
2151+
match._nonReactive.loadPromise?.resolve()
2152+
}
2153+
2154+
if (isRedirect(err)) {
2155+
rendered = true
2156+
err.options._fromLocation = location
2157+
err.redirectHandled = true
2158+
err = this.resolveRedirect(err)
2159+
throw err
2160+
} else {
2161+
this._handleNotFound(matches, err, updateMatch)
2162+
throw err
21672163
}
21682164
}
21692165

@@ -3056,14 +3052,10 @@ export class RouterCore<
30563052
_handleNotFound = (
30573053
matches: Array<AnyRouteMatch>,
30583054
err: NotFoundError,
3059-
{
3060-
updateMatch = this.updateMatch,
3061-
}: {
3062-
updateMatch?: (
3063-
id: string,
3064-
updater: (match: AnyRouteMatch) => AnyRouteMatch,
3065-
) => void
3066-
} = {},
3055+
updateMatch: (
3056+
id: string,
3057+
updater: (match: AnyRouteMatch) => AnyRouteMatch,
3058+
) => void = this.updateMatch,
30673059
) => {
30683060
// Find the route that should handle the not found error
30693061
// First check if a specific route is requested to show the error
@@ -3109,9 +3101,7 @@ export class RouterCore<
31093101

31103102
if ((err as any).routerCode === 'BEFORE_LOAD' && routeCursor.parentRoute) {
31113103
err.routeId = routeCursor.parentRoute.id
3112-
this._handleNotFound(matches, err, {
3113-
updateMatch,
3114-
})
3104+
this._handleNotFound(matches, err, updateMatch)
31153105
}
31163106
}
31173107

0 commit comments

Comments
 (0)