IssueRaiser

Support relative URLs

The `new URL()` constructor currently requires at least one of its arguments to be an absolute URL. ```js new URL('./page.html', 'https://site.com/help/'); // OK new URL('./page.html', '/help/'); // Uncaught TypeError: URL constructor: /public_html/ is not a valid URL. ``` That requirement is painful because determining which absolute URL to use as a base can be difficult or impossible in many circumstances. In a regular browser context, `document.baseURI` should be used. In Web Workers, `self.location` should be used. In Deno, `window.location` should be used but only if the `--location` command line option was used. In Node, there is no absolute URL to use. Trying to write isomorphic code that satisfies this requirement is quite error prone. Additionally, in many cases it would be useful to parse and resolve relative URLs against each other without knowing an absolute base URL ahead of time. ```js // Desired output - these currently do not work new URL('/to', '/from').toString(); // '/to' new URL('/to', '//from.com/').toString(); // '//from.com/to' ``` The lack of support for use cases involving only relative URLs is causing me to remove WHATWG URL from Ky, a popular HTTP request library, in favor of our own string replacement. See: https://github.com/sindresorhus/ky/pull/271 ### Desired API and whether to update the existing `new URL()` API or create a new API? From my perspective, updating the `new URL()` constructor so it can handle a relative URL in the `base

Posted by sholladay on Jul 17, 2020, 3:35 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments

ux (Software)

Support relative URLs

sholladayJul 17, 2020, 3:35 AM

The `new URL()` constructor currently requires at least one of its arguments to be an absolute URL. ```js new URL('./page.html', 'https://site.com/help/'); // OK new URL('./page.html', '/help/'); // Uncaught TypeError: URL constructor: /public_html/ is not a valid URL. ``` That requirement is painful because determining which absolute URL to use as a base can be difficult or impossible in many circumstances. In a regular browser context, `document.baseURI` should be used. In Web Workers, `self.location` should be used. In Deno, `window.location` should be used but only if the `--location` command line option was used. In Node, there is no absolute URL to use. Trying to write isomorphic code that satisfies this requirement is quite error prone. Additionally, in many cases it would be useful to parse and resolve relative URLs against each other without knowing an absolute base URL ahead of time. ```js // Desired output - these currently do not work new URL('/to', '/from').toString(); // '/to' new URL('/to', '//from.com/').toString(); // '//from.com/to' ``` The lack of support for use cases involving only relative URLs is causing me to remove WHATWG URL from Ky, a popular HTTP request library, in favor of our own string replacement. See: https://github.com/sindresorhus/ky/pull/271 ### Desired API and whether to update the existing `new URL()` API or create a new API? From my perspective, updating the `new URL()` constructor so it can handle a relative URL in the `base

Comments (0)

No comments yet. Start the discussion.

Comment on this issue