relative = (referrer, location) => {
  referrer = String(referrer || '');
  location = String(location || '');

  if (referrer === location) {
    return '';
  }

  const referrerURL = new URL(referrer);
  const locationURL = new URL(location);

  if (
    referrerURL.host !== locationURL.host ||
    referrerURL.port !== locationURL.port ||
    referrerURL.protocol !== locationURL.protocol
  ) {
    return location;
  }

  // left location right, look for closest common path segment
  const referrerParts = referrerURL.pathname.substr(1).split('/');
  const locationParts = locationURL.pathname.substr(1).split('/');

  if (referrerURL.pathname === locationURL.pathname) {
    if (locationURL.pathname[locationURL.pathname.length - 1] === '/') {
      return '.';
    }
    return locationParts[locationParts.length - 1];
  }

  while (referrerParts[0] === locationParts[0]) {
    referrerParts.shift();
    locationParts.shift();
  }

  let length = referrerParts.length - locationParts.length;
  if (length > 0) {
    if (referrer.endsWith('/')) {
      locationParts.unshift('..');
    }
    while (length > 0) {
      length -= 1;
      locationParts.unshift('..');
    }
    return locationParts.join('/');
  }
  if (length < 0) {
    return locationParts.join('/');
  }
  length = locationParts.length - 1;
  while (length > 0) {
    length -= 1;
    locationParts.unshift('..');
  }
  return locationParts.join('/');
}
Computes the relative URL from the referrer to the location.

@param {string} referrer
@param {string} location
@returns {string}