getLink()function
getLink(href: Nullish<PossibleLink>, url?: ImmutableURL, root: ImmutableURL | undefined = url): ImmutableURI | undefined
| Param | Type | |
|---|---|---|
href | Nullish<PossibleLink> | The link to resolve. Strings are classified by shape; URL instances pass through. required |
url | ImmutableURL | The current page URL — base for relative refs and scheme-prefixed URIs. |
.href | URLString | A URL string has a protocol and a //. required readonly |
.origin | URLString | A URL string has a protocol and a //. required readonly |
.pathname | AbsolutePath | Absolute path string starting with a / slash. required readonly |
root | ImmutableURL | The site root URL — base for absolute paths. Defaults to url. Defaults to url |
.href | URLString | A URL string has a protocol and a //. required readonly |
.origin | URLString | A URL string has a protocol and a //. required readonly |
.pathname | AbsolutePath | Absolute path string starting with a / slash. required readonly |
| Return | |
|---|---|
ImmutableURI | undefined | An absolute URI object, or undefined if link is missing, not a string/URL, or cannot be resolved. |
Resolve a possible link to an absolute URI string, or return undefined if resolution fails.
Classification:
URLinstance → returns its.hrefdirectly.- Absolute path (single leading
/, e.g./schema) → resolved againstrootwith a dot prefix so the resolution honorsroot's subfolder —/schemaagainsthttps://x.com/app/becomeshttps://x.com/app/schema, nothttps://x.com/schema. - Anything else — relative ref (
./foo,../foo,foo,#anchor,?q), protocol-relative URL (//host/path), or scheme-prefixed URI (mailto:a@b,tel:123,https://x.com/y) → fed togetBasedURI()withurlas the base. Self-contained URIs ignore the base (their own scheme makes them absolute); protocol-relative and relative refs resolve against the page URL.
Defaults:
rootdefaults tourl, so passing onlyurlmakes absolute paths resolve under the page URL's directory (same coordinate space the page lives in).- When both are omitted, all branches return
undefined(no base to resolve against).
Bases are passed through to getURL() / getBasedURI() lazily — neither url nor root is materialised into a normalised base until the chosen branch needs it.
Examples
getLink("/schema", pageURL, siteRoot) // → "https://x.com/app/schema" when siteRoot is "https://x.com/app/"getLink("./db", new URL("https://x.com/app/schema/")) // → "https://x.com/app/schema/db"getLink("mailto:a@b") // → "mailto:a@b"