Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dedup relative path logic in resolveTo #11097

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-ligers-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

De-dup relative path logic in `resolveTo`
33 changes: 5 additions & 28 deletions packages/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1209,37 +1209,16 @@ export function resolveTo(
// to the current location's pathname and *not* the route pathname.
if (toPathname == null) {
from = locationPathname;
} else if (isPathRelative) {
let fromSegments =
routePathnames.length === 0
? []
: routePathnames[routePathnames.length - 1]
.replace(/^\//, "")
.split("/");

if (toPathname.startsWith("..")) {
let toSegments = toPathname.split("/");

// With relative="path", each leading .. segment means "go up one URL segment"
while (toSegments[0] === "..") {
toSegments.shift();
fromSegments.pop();
}

to.pathname = toSegments.join("/");
}
Comment on lines -1220 to -1230
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic already happens in resolvePath below - we just need to pick the proper from path


from = "/" + fromSegments.join("/");
} else {
let routePathnameIndex = routePathnames.length - 1;

if (toPathname.startsWith("..")) {
// With relative="route" (the default), each leading .. segment means
// "go up one route" instead of "go up one URL segment". This is a key
// difference from how <a href> works and a major reason we call this a
// "to" value instead of a "href".
if (!isPathRelative && toPathname.startsWith("..")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just need to skip processing any .. segments in to when generating our from pathname when relative="path"

let toSegments = toPathname.split("/");

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

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

Expand Down
Loading