Skip to content

Commit

Permalink
Support relative urls on Link component (#614)
Browse files Browse the repository at this point in the history
* link: support relative urls

* link: support protocol-relative urls

* router: fix error for clicking anchor tag
  • Loading branch information
nkzawa authored and rauchg committed Jan 2, 2017
1 parent 0cce096 commit c1a620a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/link.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from 'url'
import React, { Component, Children, PropTypes } from 'react'
import Router from './router'

Expand All @@ -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
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion lib/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit c1a620a

Please sign in to comment.