3369d67bd1
Replace `url.parse` and `url.resolve` logic with whatwg `URL`, Bring in a customized `format` function to handle the node url objects that can be passed to router methods. This eliminates the need for `url` (and thus `native-url`) in core. Looks like it shaves off about 2.5Kb, according to the `size-limits` integration tests.
24 lines
666 B
TypeScript
24 lines
666 B
TypeScript
const DUMMY_BASE = new URL('http://n')
|
|
|
|
/**
|
|
* Parses path-relative urls (e.g. `/hello/world?foo=bar`). If url isn't path-relative
|
|
* (e.g. `./hello`) then at least base must be.
|
|
* Absolute urls are rejected.
|
|
*/
|
|
export function parseRelativeUrl(url: string, base?: string) {
|
|
const resolvedBase = base ? new URL(base, DUMMY_BASE) : DUMMY_BASE
|
|
const { pathname, searchParams, search, hash, href, origin } = new URL(
|
|
url,
|
|
resolvedBase
|
|
)
|
|
if (origin !== DUMMY_BASE.origin) {
|
|
throw new Error('invariant: invalid relative URL')
|
|
}
|
|
return {
|
|
pathname,
|
|
searchParams,
|
|
search,
|
|
hash,
|
|
href: href.slice(DUMMY_BASE.origin.length),
|
|
}
|
|
}
|