Skip to content

Commit 3b9b4c7

Browse files
fix: compare rewritten url to decide if server needs to redirect
1 parent bcce02c commit 3b9b4c7

5 files changed

Lines changed: 25 additions & 31 deletions

File tree

‎packages/react-router/src/link.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ export function useLinkProps<
127127
if (disabled) {
128128
return undefined
129129
}
130-
let href = next.maskedLocation ? next.maskedLocation.url : next.url
130+
let href = next.maskedLocation
131+
? next.maskedLocation.url.href
132+
: next.url.href
131133

132134
let external = false
133135
if (router.origin) {

‎packages/router-core/src/location.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ export interface ParsedLocation<TSearchObj extends AnySchema = {}> {
3838
unmaskOnReload?: boolean
3939
/**
4040
* @private
41-
* @description The public href of the location, including the origin before any rewrites.
41+
* @description The public href of the location.
4242
* If a rewrite is applied, the `href` property will be the rewritten URL.
4343
*/
4444
publicHref: string
4545
/**
4646
* @private
47-
* @description The full URL of the location, including the origin.
47+
* @description The full URL of the location.
4848
* @private
4949
*/
50-
url: string
50+
url: URL
5151
}

‎packages/router-core/src/router.ts‎

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ export class RouterCore<
11811181
return {
11821182
href: fullPath,
11831183
publicHref: href,
1184-
url: url.href,
1184+
url: url,
11851185
pathname: decodePath(pathname),
11861186
searchStr,
11871187
search: replaceEqualDeep(previousLocation?.search, parsedSearch) as any,
@@ -1765,7 +1765,7 @@ export class RouterCore<
17651765
publicHref:
17661766
rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash,
17671767
href: fullPath,
1768-
url: rewrittenUrl.href,
1768+
url: rewrittenUrl,
17691769
pathname: nextPathname,
17701770
search: nextSearch,
17711771
searchStr,
@@ -2000,7 +2000,7 @@ export class RouterCore<
20002000
if (reloadDocument) {
20012001
if (!href) {
20022002
const location = this.buildLocation({ to, ...rest } as any)
2003-
href = location.url
2003+
href = location.url.href
20042004
}
20052005

20062006
// Check blockers for external URLs unless ignoreBlocker is true
@@ -2056,24 +2056,11 @@ export class RouterCore<
20562056
_includeValidateSearch: true,
20572057
})
20582058

2059-
// Normalize URLs for comparison to handle encoding differences
2060-
// Browser history always stores encoded URLs while buildLocation may produce decoded URLs
2061-
const normalizeUrl = (url: string) => {
2062-
try {
2063-
return encodeURI(decodeURI(url))
2064-
} catch {
2065-
return url
2066-
}
2067-
}
2068-
20692059
if (
2070-
trimPath(normalizeUrl(this.latestLocation.href)) !==
2071-
trimPath(normalizeUrl(nextLocation.href))
2060+
this.latestLocation.publicHref !== nextLocation.publicHref ||
2061+
nextLocation.url.origin !== this.origin
20722062
) {
2073-
let href = nextLocation.url
2074-
if (this.origin && href.startsWith(this.origin)) {
2075-
href = href.replace(this.origin, '') || '/'
2076-
}
2063+
const href = this.getParsedLocationHref(nextLocation)
20772064

20782065
throw redirect({ href })
20792066
}
@@ -2395,13 +2382,18 @@ export class RouterCore<
23952382
return this.load({ sync: opts?.sync })
23962383
}
23972384

2385+
getParsedLocationHref = (location: ParsedLocation) => {
2386+
let href = location.url.href
2387+
if (this.origin && location.url.origin !== this.origin) {
2388+
href = href.replace(this.origin, '') || '/'
2389+
}
2390+
return href
2391+
}
2392+
23982393
resolveRedirect = (redirect: AnyRedirect): AnyRedirect => {
23992394
if (!redirect.options.href) {
24002395
const location = this.buildLocation(redirect.options)
2401-
let href = location.url
2402-
if (this.origin && href.startsWith(this.origin)) {
2403-
href = href.replace(this.origin, '') || '/'
2404-
}
2396+
const href = this.getParsedLocationHref(location)
24052397
redirect.options.href = location.href
24062398
redirect.headers.set('Location', href)
24072399
}

‎packages/solid-router/src/link.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ export function useLinkProps<
139139
let href
140140
const maskedLocation = next().maskedLocation
141141
if (maskedLocation) {
142-
href = maskedLocation.url
142+
href = maskedLocation.url.href
143143
} else {
144-
href = next().url
144+
href = next().url.href
145145
}
146146
let external = false
147147
if (router.origin) {

‎packages/vue-router/src/link.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,9 @@ export function useLinkProps<
431431

432432
let hrefValue: string
433433
if (maskedLocation) {
434-
hrefValue = maskedLocation.url
434+
hrefValue = maskedLocation.url.href
435435
} else {
436-
hrefValue = nextLocation?.url
436+
hrefValue = nextLocation?.url.href
437437
}
438438

439439
// Handle origin stripping like Solid does

0 commit comments

Comments
 (0)