Skip to content

Commit a76ee41

Browse files
authored
Dedup relative path logic in resolveTo (#11097)
1 parent 149ad65 commit a76ee41

File tree

3 files changed

+11
-29
lines changed

3 files changed

+11
-29
lines changed

.changeset/smooth-ligers-give.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
De-dup relative path logic in `resolveTo`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
},
111111
"filesize": {
112112
"packages/router/dist/router.umd.min.js": {
113-
"none": "50.1 kB"
113+
"none": "50.0 kB"
114114
},
115115
"packages/react-router/dist/react-router.production.min.js": {
116116
"none": "14.6 kB"

packages/router/utils.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,37 +1209,16 @@ export function resolveTo(
12091209
// to the current location's pathname and *not* the route pathname.
12101210
if (toPathname == null) {
12111211
from = locationPathname;
1212-
} else if (isPathRelative) {
1213-
let fromSegments =
1214-
routePathnames.length === 0
1215-
? []
1216-
: routePathnames[routePathnames.length - 1]
1217-
.replace(/^\//, "")
1218-
.split("/");
1219-
1220-
if (toPathname.startsWith("..")) {
1221-
let toSegments = toPathname.split("/");
1222-
1223-
// With relative="path", each leading .. segment means "go up one URL segment"
1224-
while (toSegments[0] === "..") {
1225-
toSegments.shift();
1226-
fromSegments.pop();
1227-
}
1228-
1229-
to.pathname = toSegments.join("/");
1230-
}
1231-
1232-
from = "/" + fromSegments.join("/");
12331212
} else {
12341213
let routePathnameIndex = routePathnames.length - 1;
12351214

1236-
if (toPathname.startsWith("..")) {
1215+
// With relative="route" (the default), each leading .. segment means
1216+
// "go up one route" instead of "go up one URL segment". This is a key
1217+
// difference from how <a href> works and a major reason we call this a
1218+
// "to" value instead of a "href".
1219+
if (!isPathRelative && toPathname.startsWith("..")) {
12371220
let toSegments = toPathname.split("/");
12381221

1239-
// With relative="route" (the default), each leading .. segment means
1240-
// "go up one route" instead of "go up one URL segment". This is a key
1241-
// difference from how <a href> works and a major reason we call this a
1242-
// "to" value instead of a "href".
12431222
while (toSegments[0] === "..") {
12441223
toSegments.shift();
12451224
routePathnameIndex -= 1;
@@ -1248,8 +1227,6 @@ export function resolveTo(
12481227
to.pathname = toSegments.join("/");
12491228
}
12501229

1251-
// If there are more ".." segments than parent routes, resolve relative to
1252-
// the root / URL.
12531230
from = routePathnameIndex >= 0 ? routePathnames[routePathnameIndex] : "/";
12541231
}
12551232

0 commit comments

Comments
 (0)