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

refactor(joinURL): rewrite with clear syntax and relative ../ support #218

Merged
merged 5 commits into from Mar 11, 2024

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Mar 11, 2024

As a continuation to #217 (to resolve #37), this PR reimplements joinURL utility with more clear syntax and basic support for relative ../ segments

Behavior change: For joinURL and withBase, the trailing slash of base is always respected. It is more of a bug fix.

@pi0 pi0 marked this pull request as draft March 11, 2024 14:46
// Relative with ../
{ input: ["/a", "../b"], out: "/b" },
{ input: ["/a/b/c", "../../d"], out: "/a/d" },
{ input: ["/c", "../../d"], out: "/d" },
Copy link
Member Author

@pi0 pi0 Mar 11, 2024

Choose a reason for hiding this comment

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

Updated behavior from #217 (was ../d). Confirming with two similar references:

new URL("../../d", 'http://test.com/c').pathname
// ~> "/d"

path.join("/c", "../../d")
// ~> "/d"

pathe.join("/c", "../../d")
// ~> "/d"

It is hard to decide if we should make a new 3rd behavior with ufo (which is really hard to break later)

ufo (1.4.0) behavior:

ufo.joinURL("/c", "../../d")
// "/c/../../d"

(with current behavior, we don't resolve, nor loose information but also we don't have a 3rd behavior by returning ../d)

Copy link
Member

Choose a reason for hiding this comment

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

I think existing behaviour is not to coerce a URL segment to be absolute, so joinURL('./a', 'b') resolves to ./a/b. Likewise, I think joinURL('../a', 'b') should also resolve to ../a/b and joinURL('../a', '../b') should resolve to ../b.

The key difference between new URL and joinURL is that joinURL is useable for URL segments and this is quite a useful feature. It's also important when generating relative URLs, which is something that the native URL can't do.

I would advise keeping this behaviour.

@pi0 pi0 requested a review from danielroe March 11, 2024 14:56
@pi0 pi0 marked this pull request as ready for review March 11, 2024 15:18
Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

I still think we should allow 'escaping' out of / if the first segment has / and subsequent segments have ../.

But that shouldn't affect the Nuxt usage.

Really appreciate your work on this ❤️

@pi0 pi0 merged commit f7d4711 into main Mar 11, 2024
2 checks passed
@pi0 pi0 deleted the refactor/join branch March 11, 2024 16:27
@pi0 pi0 mentioned this pull request Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

handle relative URLs for resolveURL
2 participants