diff --git a/lib/link.js b/lib/link.js index f903a8387c58a..4132f1b051f32 100644 --- a/lib/link.js +++ b/lib/link.js @@ -1,3 +1,4 @@ +import { resolve } from 'url' import React, { Component, Children, PropTypes } from 'react' import Router from './router' @@ -21,13 +22,17 @@ export default class Link extends Component { return } - const { href, as = href } = this.props + let { href, as } = this.props if (!isLocal(href)) { // ignore click if it's outside our scope return } + const { pathname } = window.location + href = resolve(pathname, href) + as = as ? resolve(pathname, as) : href + e.preventDefault() // avoid scroll for urls with anchor refs @@ -74,6 +79,6 @@ export default class Link extends Component { export function isLocal (href) { const origin = window.location.origin - return !/^https?:\/\//.test(href) || + return !/^(https?:)?\/\//.test(href) || origin === href.substr(0, origin.length) } diff --git a/lib/router/router.js b/lib/router/router.js index 768874346a579..f576f01bb97a7 100644 --- a/lib/router/router.js +++ b/lib/router/router.js @@ -32,7 +32,7 @@ export default class Router extends EventEmitter { async onPopState (e) { this.abortComponentLoad() - const { url, as } = e.state + const { url = getURL(), as = url } = e.state || {} const { pathname, query } = parse(url, true) if (!this.urlIsNew(pathname, query)) {