Resolving Urls

Resolve URLs to absolute URI/IRI.

Version

This library resolves relative IRIs to absolute IRIs given a base IRI, conforming to RFC3986. The code was borrowed from relative-to-absolute-iri . ~There is an open issue to make the library compatible with IE11: Issue #5~

Installation#

NPM#

npm install @availity/resolve-url

Yarn#

yarn add @availity/resolve-url

relative()#

Params#

  • relative: Relative url to be converted to full url
  • base (optional): Base url used to convert the relative url. If base URL is not provided it is calculated from window.location.href.

Usage#

import resolveUrl from '@availity/resolve-url';
resolveUrl({ relative: '/a/b', base: 'https://example.com/' });
// Outputs https://example.com/a/b

URLs#

When base option is not provided, this package will calculate the base from window.location.href. The example below returns server relative url if hostname was https://example.com

import resolveUrl from '@availity/resolve-url';
resolveUrl({ relative: '/a/b' });
// Outputs https://example.com/a/b

The following examples were adapted from relative-to-absolute-iri

Hashes#

Fragments/hashes in relative URIs are also taken into account.

resolve('#abc', 'http://base.org/'); // Outputs 'http://base.org/#abc'

Invalid base URI#

Invalid base URIs cause an error to be thrown.

resolve('abc', 'def'); // Error

Protocol Relative#

When a relative IRI starts with a //, then the scheme of the base IRI will be used.

resolve('//abc', 'http://base.org/'); // Outputs 'http://abc'

Root-Relative#

Relative URIs that starts with a / erase the path of the base IRI.

resolve('/abc/def/', 'http://base.org/123/456/'); // Outputs 'http://base.org/abc/def/'

Relative Directory Traversal#

Relative URIs that point to the current directory (.) or parent directory (..) are collapsed.

resolve('xyz', 'http://aa/parent/parent/../../a'); // Outputs 'http://aa/xyz'
resolve('xyz', 'http://aa/././a'); // Outputs 'http://aa/xyz'

Notes#

  • URI - Uniform Resource Identifier allows ASCII characters
  • IRI - Internationalized Resource Identifier allows Unicode typeset