2017-05-09 01:20:50 +00:00
|
|
|
/* global __NEXT_DATA__ */
|
|
|
|
|
2017-03-12 03:57:51 +00:00
|
|
|
import { resolve, format, parse } from 'url'
|
2017-04-10 18:35:26 +00:00
|
|
|
import React, { Component, Children } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2017-05-14 00:40:32 +00:00
|
|
|
import Router, { _rewriteUrlForNextExport } from './router'
|
2017-02-15 08:31:19 +00:00
|
|
|
import { warn, execOnce, getLocationOrigin } from './utils'
|
2016-10-06 07:07:41 +00:00
|
|
|
|
|
|
|
export default class Link extends Component {
|
2017-03-12 03:57:51 +00:00
|
|
|
constructor (props, ...rest) {
|
|
|
|
super(props, ...rest)
|
2016-10-06 07:07:41 +00:00
|
|
|
this.linkClicked = this.linkClicked.bind(this)
|
2017-03-12 03:57:51 +00:00
|
|
|
this.formatUrls(props)
|
2016-10-06 07:07:41 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 12:53:20 +00:00
|
|
|
static propTypes = {
|
2017-02-27 05:20:28 +00:00
|
|
|
prefetch: PropTypes.bool,
|
2016-12-29 12:53:20 +00:00
|
|
|
children: PropTypes.oneOfType([
|
2017-02-03 20:27:12 +00:00
|
|
|
PropTypes.element,
|
|
|
|
(props, propName) => {
|
|
|
|
const value = props[propName]
|
|
|
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
warnLink(`Warning: You're using a string directly inside <Link>. This usage has been deprecated. Please add an <a> tag as child of <Link>`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
2017-05-21 02:54:58 +00:00
|
|
|
]).isRequired,
|
|
|
|
shallow: PropTypes.bool
|
2016-12-29 12:53:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 03:57:51 +00:00
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
this.formatUrls(nextProps)
|
|
|
|
}
|
|
|
|
|
2016-10-06 07:07:41 +00:00
|
|
|
linkClicked (e) {
|
2017-02-21 18:08:54 +00:00
|
|
|
if (e.currentTarget.nodeName === 'A' &&
|
2016-12-15 04:33:02 +00:00
|
|
|
(e.metaKey || e.ctrlKey || e.shiftKey || (e.nativeEvent && e.nativeEvent.which === 2))) {
|
2016-10-06 07:07:41 +00:00
|
|
|
// ignore click for new tab / new window behavior
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-21 02:54:58 +00:00
|
|
|
let { shallow } = this.props
|
2017-03-12 03:57:51 +00:00
|
|
|
let { href, as } = this
|
2016-10-06 07:07:41 +00:00
|
|
|
|
|
|
|
if (!isLocal(href)) {
|
|
|
|
// ignore click if it's outside our scope
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-02 06:22:07 +00:00
|
|
|
const { pathname } = window.location
|
|
|
|
href = resolve(pathname, href)
|
|
|
|
as = as ? resolve(pathname, as) : href
|
|
|
|
|
2016-10-06 07:07:41 +00:00
|
|
|
e.preventDefault()
|
|
|
|
|
2016-12-22 08:15:49 +00:00
|
|
|
// avoid scroll for urls with anchor refs
|
|
|
|
let { scroll } = this.props
|
|
|
|
if (scroll == null) {
|
2016-12-28 05:27:52 +00:00
|
|
|
scroll = as.indexOf('#') < 0
|
2016-12-22 08:15:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 23:06:34 +00:00
|
|
|
// replace state instead of push if prop is present
|
|
|
|
const { replace } = this.props
|
|
|
|
const changeMethod = replace ? 'replace' : 'push'
|
|
|
|
|
2016-10-06 07:07:41 +00:00
|
|
|
// straight up redirect
|
2017-05-21 02:54:58 +00:00
|
|
|
Router[changeMethod](href, as, { shallow })
|
2016-12-19 14:40:26 +00:00
|
|
|
.then((success) => {
|
|
|
|
if (!success) return
|
2016-12-22 08:17:50 +00:00
|
|
|
if (scroll) window.scrollTo(0, 0)
|
2016-12-19 14:40:26 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
if (this.props.onError) this.props.onError(err)
|
|
|
|
})
|
2016-10-06 07:07:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 14:01:03 +00:00
|
|
|
prefetch () {
|
|
|
|
if (!this.props.prefetch) return
|
|
|
|
if (typeof window === 'undefined') return
|
|
|
|
|
|
|
|
// Prefetch the JSON page if asked (only in the client)
|
|
|
|
const { pathname } = window.location
|
2017-03-12 03:57:51 +00:00
|
|
|
const href = resolve(pathname, this.href)
|
2017-02-15 14:01:03 +00:00
|
|
|
Router.prefetch(href)
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
this.prefetch()
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate (prevProps) {
|
2017-03-12 03:57:51 +00:00
|
|
|
if (JSON.stringify(this.props.href) !== JSON.stringify(prevProps.href)) {
|
2017-02-15 14:01:03 +00:00
|
|
|
this.prefetch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 03:57:51 +00:00
|
|
|
// We accept both 'href' and 'as' as objects which we can pass to `url.format`.
|
|
|
|
// We'll handle it here.
|
|
|
|
formatUrls (props) {
|
|
|
|
this.href = props.href && typeof props.href === 'object'
|
|
|
|
? format(props.href)
|
|
|
|
: props.href
|
|
|
|
this.as = props.as && typeof props.as === 'object'
|
|
|
|
? format(props.as)
|
|
|
|
: props.as
|
|
|
|
}
|
|
|
|
|
2016-10-06 07:07:41 +00:00
|
|
|
render () {
|
2017-02-15 14:01:03 +00:00
|
|
|
let { children } = this.props
|
2017-03-12 03:57:51 +00:00
|
|
|
let { href, as } = this
|
2017-02-03 20:27:12 +00:00
|
|
|
// Deprecated. Warning shown by propType check. If the childen provided is a string (<Link>example</Link>) we wrap it in an <a> tag
|
|
|
|
if (typeof children === 'string') {
|
|
|
|
children = <a>{children}</a>
|
|
|
|
}
|
2016-10-06 07:07:41 +00:00
|
|
|
|
2017-02-03 20:27:12 +00:00
|
|
|
// This will return the first child, if multiple are provided it will throw an error
|
|
|
|
const child = Children.only(children)
|
|
|
|
const props = {
|
|
|
|
onClick: this.linkClicked
|
|
|
|
}
|
2016-10-06 07:07:41 +00:00
|
|
|
|
2017-02-03 20:27:12 +00:00
|
|
|
// If child is an <a> tag and doesn't have a href attribute we specify it so that repetition is not needed by the user
|
|
|
|
if (child.type === 'a' && !('href' in child.props)) {
|
2017-03-12 03:57:51 +00:00
|
|
|
props.href = as || href
|
2017-02-03 20:27:12 +00:00
|
|
|
}
|
2016-10-06 07:07:41 +00:00
|
|
|
|
2017-05-09 01:20:50 +00:00
|
|
|
// Add the ending slash to the paths. So, we can serve the
|
|
|
|
// "<page>/index.html" directly.
|
|
|
|
if (
|
2017-06-20 08:37:36 +00:00
|
|
|
props.href &&
|
2017-05-09 06:38:36 +00:00
|
|
|
typeof __NEXT_DATA__ !== 'undefined' &&
|
2017-05-14 00:40:32 +00:00
|
|
|
__NEXT_DATA__.nextExport
|
2017-05-09 01:20:50 +00:00
|
|
|
) {
|
2017-05-14 00:40:32 +00:00
|
|
|
props.href = _rewriteUrlForNextExport(props.href)
|
2017-05-09 01:20:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 20:27:12 +00:00
|
|
|
return React.cloneElement(child, props)
|
2016-10-06 07:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 08:52:22 +00:00
|
|
|
function isLocal (href) {
|
2017-03-12 03:57:51 +00:00
|
|
|
const url = parse(href, false, true)
|
|
|
|
const origin = parse(getLocationOrigin(), false, true)
|
2017-04-22 13:07:36 +00:00
|
|
|
return !url.host ||
|
|
|
|
(url.protocol === origin.protocol && url.host === origin.host)
|
2016-10-06 07:07:41 +00:00
|
|
|
}
|
2017-01-16 21:02:11 +00:00
|
|
|
|
|
|
|
const warnLink = execOnce(warn)
|