-
Notifications
You must be signed in to change notification settings - Fork 139
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
URL.relative proposal #421
Comments
I think that creating relative URLs is not a common need and belongs in user land. Not just because I've already created one, too 😉. While there are some complexities that might benefit from a standard, it could still be compared to rimraf not being part of the core Node.js platform. |
Do we know if this is used outside the domain of servers? |
With service workers, any URL operations done on a server are entirely likely to be needed in the browser. |
Copying my comment from #136 (comment):
|
@ljharb well, service workers do not really have to deal with a file system and everything they can touch is keyed on URLs. cc @jakearchibald to make sure I'm not wrong. It would be really great to see some evidence this is indeed a somewhat widespread problem for which standardizing on a single library makes sense. E.g., https://www.npmjs.com/package/url-relative does not seem that popular, although it does suggest this might be a rather trivial addition complexity-wise. |
@annevk that library is a piece of crap. Try running most anything from relateurl's ~5000 test fixtures through it and it'll break. |
@stevenvachon please stay respectful, see https://whatwg.org/code-of-conduct. I had missed that https://www.npmjs.com/package/relateurl is much more popular though. Looking at that and its dependencies though, it also seems a good bit more complex. |
Okay, the "url-relative" package was created as lazily as possible and only contributes to the growing number of useless drivel found within the NPM registry. It distracts from real progress by injecting confusion into the discovery phase. It was likely created solely out of pretentiousness. Regarding relateurl's complexity; it's not done so by choice, but out of necessity. There're multiple definitions of a "relative" URL. |
And in case you were reviewing relateurl via its npm page, instead check out the its repository, as its currently unreleased version uses |
There could be many return values that would satisfy the condition that the constructor be an inverse, and each has its use. For example, a relative URL from https://example.com/full/ to https://example.com/full/path/resource could return a string in the form //example.com/full/path/resource, and such relative URLs are commonly used when the scheme of the base is important but everything else must be specified. It could also return /full/path/resource if the full path is important, ../full/path/resource if the relative structure is important, or path/resource if only that part of the structure is important. Many applications will want the shortest version, but some applications will want to specify which form they want. This ambiguity would require a complicated API and specification, and I agree that if someone wants a particular form of this functionality they should write their own library. |
I'd like to explicitly state that the use case of getting The output of the relative URL function remains completely well-defined. The complexities of relateurl can also be done with URL manipulation before running such a function. @annevk the common scenario is basically any generation or template creation of HTML / CSS / JS with references to other resources that need to be relative and not encode the base URL as the URL is independent. While in many cases the URL can be assumed to be the same as the page, if the template being generated needs to run on any other base URL, then URL.relative is needed. |
So what would the processing model look like, for the use cases that are in scope? |
I guess something like -
|
A relative URL of "./#hash" will cause a page refresh. |
One can always remove |
That means special-case behaviour for something unexpected. |
It's |
It's pretty unexpected that you chose that particular relative representation, instead of |
This is the shortest form that works everywhere relative imports are supported, other forms like |
@Jamesernator |
I know this but While Regarding |
|
In my own experience working with module specifiers in the module system, the lack of a reliable I guess relative representations form a sort of hierarchy:
Does that comprehensively cover the forms? So far we've been suggesting (2) falling back to (5) as being the canonical approach. There are certainly arguments that (3) and (4) are useful too. If this were C, I'd suggest flags for these forms, that could be composed as a second argument, which would then apply them in hierarchy and fail otherwise, but given that this is a JS API, I'm not sure how best to capture that. Ideas welcome. |
Search-relative. |
Yes but all "module specifiers" that start with |
I was looking for similar features and opened the following issue with nodejs The spec @ https://url.spec.whatwg.org/#relative-url-with-fragment-string also talks about certain segments of the URL but whatwg/url does not give read access to those fragments. While certain naive user land libraries might be able to work with a subset of valid URLs, actually writing code that that works for all URLs is more involved and I think a good place for that is in whatwg/url nodejs/node#31874 is slightly different then #421, should I create a new issue in whatwg/url for this? |
@dodtsair an issue per feature is best, yes. Evidence that it's needed in terms of library usage / StackOverflow questions would help a lot in evaluating. |
@guybedford Perhaps its best to move this proposal from WHATWG (browser focused) to WinterCG (server focused). Its becoming relevant again now that Node.js has deprecated |
As Node.js has moved to embrace the URL spec over the legacy url API, there is still an API gap for handling relative URLs as discussed in nodejs/node#616.
To follow on from that discussion, and discussion in the Node.js tooling group, it would be worth seeing if a proposal for a
URL.relative
might be considered here.The API might look something like the following:
where it returns the relative url with fragment string of toURL from fromURL, such that
new URL(result, fromURL)
would be the inverse operation.Node.js certainly can go its own way here too, but we thought it would just be good to find out first if WhatWG would consider something along these lines.
The text was updated successfully, but these errors were encountered: