mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Support relative urls on Link component (#614)
* link: support relative urls * link: support protocol-relative urls * router: fix error for clicking anchor tag
This commit is contained in:
parent
0cce0962d2
commit
c1a620a7c4
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)) {
|
||||
|
|
Loading…
Reference in a new issue