Repository navigation
Please un-deprecate url.resolve #37492
Description
Activity
The challenge is not that URL does not support relative URLs, it's that it is designed to work for more than just HTTP URLs and needs to know the protocol to properly resolve the URL (since different URL protocol schemes use different resolve and serialization algorithms). You can achieve what you're looking for by appending a prefix to the
/bar, then ignoring that prefix in the result:const { pathname, search } = new URL('?foo=bar', 'http://example/bar'); console.log(pathname + search); // Outputs: /bar?foo=bar
Reacted by akhil marsonyaInteresting, that makes sense! It's just tricky to handle. I work a lot on HATEOAS-style systems, where people just generally want to use relative urls.
What I need is some way for the origin to be preserved if it was supplied in any of the 2 components, but removed if both were relative. I could solve this by supplying a domain that will never be registered, but it's a bit clumsy.
I do appreciate the reasoning here though, so feel free to close,
The
example.org/example.comdomains are safe to use. They are officially registered by IANA/IETF and used for examples in IETF standards.What I need is some way for the origin to be preserved if it was supplied in any of the 2 components, but removed if both were relative.
The way to handle this is to use two resolves:
function resolve(path, base) { const baseUrl = new URL(base, 'http://example.com'); const { pathname, search } = new URL(path, baseUrl); return pathname + search; } resolve('?foo=bar', '/bar'); resolve('?foo=bar', 'https://example.org/bar');
PR to add the suggested replacement function: #37501
- added a commit that references this issue
on Feb 26, 2021 - added a commit that references this issue
on Feb 28, 2021 - added a commit that references this issue
on May 1, 2021
What steps will reproduce the bug?
We use
url.resolvea LOT. It's a great function that also inspired me to write a PHP version with 8 million downloads =)I recently discovered that
url.resolve()was marked as deprecated, and that users should useURLinstead.Aside from the fact that WhatWG URL is not a real RFC3986 uri, there's also a a very simple feature missing. We use
url.resolveall the time to do things like this:The URL object does not let us work with relative urls: