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'
|
2018-09-27 19:10:53 +00:00
|
|
|
import { execOnce, getLocationOrigin } from './utils'
|
2016-10-06 07:07:41 +00:00
|
|
|
|
2018-08-07 03:23:28 +00:00
|
|
|
function isLocal (href) {
|
|
|
|
const url = parse(href, false, true)
|
|
|
|
const origin = parse(getLocationOrigin(), false, true)
|
|
|
|
|
|
|
|
return !url.host ||
|
|
|
|
(url.protocol === origin.protocol && url.host === origin.host)
|
|
|
|
}
|
|
|
|
|
2018-08-07 05:44:18 +00:00
|
|
|
function memoizedFormatUrl (formatUrl) {
|
|
|
|
let lastHref = null
|
|
|
|
let lastAs = null
|
|
|
|
let lastResult = null
|
|
|
|
return (href, as) => {
|
|
|
|
if (href === lastHref && as === lastAs) {
|
|
|
|
return lastResult
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = formatUrl(href, as)
|
|
|
|
lastHref = href
|
|
|
|
lastAs = as
|
|
|
|
lastResult = result
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 03:23:28 +00:00
|
|
|
class Link extends Component {
|
2018-08-07 03:53:06 +00:00
|
|
|
componentDidMount () {
|
|
|
|
this.prefetch()
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate (prevProps) {
|
|
|
|
if (JSON.stringify(this.props.href) !== JSON.stringify(prevProps.href)) {
|
|
|
|
this.prefetch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 05:44:18 +00:00
|
|
|
// The function is memoized so that no extra lifecycles are needed
|
|
|
|
// as per https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
|
|
|
|
formatUrls = memoizedFormatUrl((href, asHref) => {
|
|
|
|
return {
|
|
|
|
href: href && typeof href === 'object'
|
|
|
|
? format(href)
|
|
|
|
: href,
|
|
|
|
as: asHref && typeof asHref === 'object'
|
|
|
|
? format(asHref)
|
|
|
|
: asHref
|
|
|
|
}
|
|
|
|
})
|
2018-08-07 03:53:06 +00:00
|
|
|
|
2018-08-07 03:20:01 +00:00
|
|
|
linkClicked = e => {
|
2018-07-11 22:03:25 +00:00
|
|
|
const { nodeName, target } = e.currentTarget
|
|
|
|
if (nodeName === 'A' &&
|
|
|
|
((target && target !== '_self') || 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
|
|
|
|
}
|
|
|
|
|
2018-08-07 05:44:18 +00:00
|
|
|
let { href, as } = this.formatUrls(this.props.href, this.props.as)
|
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
|
2018-08-07 05:44:18 +00:00
|
|
|
Router[changeMethod](href, as, { shallow: this.props.shallow })
|
2016-12-19 14:40:26 +00:00
|
|
|
.then((success) => {
|
|
|
|
if (!success) return
|
2018-01-25 14:44:57 +00:00
|
|
|
if (scroll) {
|
|
|
|
window.scrollTo(0, 0)
|
|
|
|
document.body.focus()
|
|
|
|
}
|
2016-12-19 14:40:26 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
if (this.props.onError) this.props.onError(err)
|
|
|
|
})
|
2018-08-07 03:20:01 +00:00
|
|
|
};
|
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
|
2018-08-07 05:44:18 +00:00
|
|
|
const {href: parsedHref} = this.formatUrls(this.props.href, this.props.as)
|
|
|
|
const href = resolve(pathname, parsedHref)
|
2017-02-15 14:01:03 +00:00
|
|
|
Router.prefetch(href)
|
|
|
|
}
|
|
|
|
|
2016-10-06 07:07:41 +00:00
|
|
|
render () {
|
2017-02-15 14:01:03 +00:00
|
|
|
let { children } = this.props
|
2018-08-07 05:44:18 +00:00
|
|
|
let { href, as } = this.formatUrls(this.props.href, this.props.as)
|
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 = {
|
2018-05-27 18:47:02 +00:00
|
|
|
onClick: (e) => {
|
|
|
|
if (child.props && typeof child.props.onClick === 'function') {
|
|
|
|
child.props.onClick(e)
|
|
|
|
}
|
|
|
|
if (!e.defaultPrevented) {
|
|
|
|
this.linkClicked(e)
|
|
|
|
}
|
|
|
|
}
|
2017-02-03 20:27:12 +00:00
|
|
|
}
|
2016-10-06 07:07:41 +00:00
|
|
|
|
2017-07-09 05:09:02 +00:00
|
|
|
// If child is an <a> tag and doesn't have a href attribute, or if the 'passHref' property is
|
|
|
|
// defined, we specify the current 'href', so that repetition is not needed by the user
|
|
|
|
if (this.props.passHref || (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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 09:34:08 +00:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
2018-09-27 19:10:53 +00:00
|
|
|
const warn = execOnce(console.error)
|
|
|
|
|
2018-09-14 09:34:08 +00:00
|
|
|
// This module gets removed by webpack.IgnorePlugin
|
|
|
|
const exact = require('prop-types-exact')
|
|
|
|
Link.propTypes = exact({
|
|
|
|
href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
|
|
|
|
as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
|
|
|
prefetch: PropTypes.bool,
|
|
|
|
replace: PropTypes.bool,
|
|
|
|
shallow: PropTypes.bool,
|
|
|
|
passHref: PropTypes.bool,
|
|
|
|
scroll: PropTypes.bool,
|
|
|
|
children: PropTypes.oneOfType([
|
|
|
|
PropTypes.element,
|
|
|
|
(props, propName) => {
|
|
|
|
const value = props[propName]
|
|
|
|
|
|
|
|
if (typeof value === 'string') {
|
2018-09-27 19:10:53 +00:00
|
|
|
warn(`Warning: You're using a string directly inside <Link>. This usage has been deprecated. Please add an <a> tag as child of <Link>`)
|
2018-09-14 09:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
]).isRequired
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-07 03:23:28 +00:00
|
|
|
export default Link
|