getLink()function

getLink(href: Nullish<PossibleLink>, url?: ImmutableURL, root: ImmutableURL | undefined = url): ImmutableURI | undefined
ParamType
hrefNullish<PossibleLink>
The link to resolve. Strings are classified by shape; URL instances pass through. required
urlImmutableURL
The current page URL — base for relative refs and scheme-prefixed URIs.
    .hrefURLString
A URL string has a protocol and a //. required readonly
    .originURLString
A URL string has a protocol and a //. required readonly
    .pathnameAbsolutePath
Absolute path string starting with a / slash. required readonly
rootImmutableURL
The site root URL — base for absolute paths. Defaults to url. Defaults to url
    .hrefURLString
A URL string has a protocol and a //. required readonly
    .originURLString
A URL string has a protocol and a //. required readonly
    .pathnameAbsolutePath
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:

  • URL instance → returns its .href directly.
  • Absolute path (single leading /, e.g. /schema) → resolved against root with a dot prefix so the resolution honors root's subfolder — /schema against https://x.com/app/ becomes https://x.com/app/schema, not https://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 to getBasedURI() with url as 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:

  • root defaults to url, so passing only url makes 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"