Giter VIP home page Giter VIP logo

Comments (2)

vlukashov avatar vlukashov commented on June 13, 2024

The following snippet might be helpful to resolve non-root URLs:

let isUrlAvailable;
let urlDocument, urlBase, urlAnchor;

function ensureUrlAvailableOrPolyfilled() {
  if (isUrlAvailable === undefined) {
    try {
      const url = new URL('b', 'http://a');
      url.pathname = 'c';
      isUrlAvailable = (url.href === 'http://a/c');
    } catch (e) {
      isUrlAvailable = false;
    }

    if (!isUrlAvailable) {
      // The URL constructor is not available in IE11. Polyfil it by creating an
      // HTMLAnchorElement in an in-memory HTML document.
      urlDocument = document.implementation.createHTMLDocument('url');
      urlBase = urlDocument.createElement('base');
      urlDocument.head.appendChild(urlBase);
      urlAnchor = urlDocument.createElement('a');

      if (!urlAnchor.origin) {
        // IE11: HTMLAnchorElement does not have the `origin` property
        Object.defineProperty(urlAnchor, 'origin', {
          get: () => {
            // IE11: on HTTP and HTTPS the default port is not included into
            // window.location.origin, so won't include it here either.
            const port = urlAnchor.port;
            const protocol = urlAnchor.protocol;
            const defaultHttp = protocol === 'http:' && port === '80';
            const defaultHttps = protocol === 'https:' && port === '443';
            const host = (defaultHttp || defaultHttps)
              ? urlAnchor.hostname
              : urlAnchor.host;
            return `${protocol}//${host}`;
          }
        });
      }
    }
  }
}

function resolveURL(path, base) {
  ensureUrlAvailableOrPolyfilled();
  if (isUrlAvailable) {
    return new URL(path, base);
  }

  urlBase.href = base;
  urlAnchor.href = path.replace(/ /g, '%20');
  return urlAnchor;
}

from router.

noncototient avatar noncototient commented on June 13, 2024

Hey @vlukashov has this been added to the router?

If not, where should the code snippet above be added?

from router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.