Skip to content

Commit 75a8bfa

Browse files
Sherafftannerlinsley
authored andcommitted
refactor(router-core): redirect skips creating a URL object if href is not a string (#4986)
Creating a `URL` object is a little expensive, and we know it's going to throw if `href` is not a string. So we can skip it entirely in that case (which is probably most cases?). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved redirect handling to avoid errors when a URL is missing or invalid. * Added safeguards to prevent runtime crashes when redirect data is null. * Enhanced URL parsing reliability during redirects. * No changes to public APIs. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 58e813f commit 75a8bfa

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

packages/router-core/src/redirect.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ export function redirect<
6565
): Redirect<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> {
6666
opts.statusCode = opts.statusCode || opts.code || 307
6767

68-
if (!opts.reloadDocument) {
68+
if (!opts.reloadDocument && typeof opts.href === 'string') {
6969
try {
70-
new URL(`${opts.href}`)
70+
new URL(opts.href)
7171
opts.reloadDocument = true
7272
} catch {}
7373
}
@@ -103,7 +103,7 @@ export function isResolvedRedirect(
103103
}
104104

105105
export function parseRedirect(obj: any) {
106-
if (typeof obj === 'object' && obj.isSerializedRedirect) {
106+
if (obj !== null && typeof obj === 'object' && obj.isSerializedRedirect) {
107107
return redirect(obj)
108108
}
109109

0 commit comments

Comments
 (0)