Commit 540a86f0 authored by Naoyuki Kanezawa's avatar Naoyuki Kanezawa Committed by Tim Neutkens

fix prefetch url (#1154)

parent 141ab998
...@@ -61,8 +61,28 @@ export default class Link extends Component { ...@@ -61,8 +61,28 @@ export default class Link extends Component {
}) })
} }
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
const href = resolve(pathname, this.props.href)
Router.prefetch(href)
}
componentDidMount () {
this.prefetch()
}
componentDidUpdate (prevProps) {
if (this.props.href !== prevProps.href) {
this.prefetch()
}
}
render () { render () {
let { children, prefetch } = this.props let { children } = this.props
// Deprecated. Warning shown by propType check. If the childen provided is a string (<Link>example</Link>) we wrap it in an <a> tag // 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') { if (typeof children === 'string') {
children = <a>{children}</a> children = <a>{children}</a>
...@@ -79,15 +99,6 @@ export default class Link extends Component { ...@@ -79,15 +99,6 @@ export default class Link extends Component {
props.href = this.props.as || this.props.href props.href = this.props.as || this.props.href
} }
// Prefetch the JSON page if asked (only in the client)
if (prefetch) {
if (typeof window !== 'undefined') {
const { pathname } = window.location
const href = resolve(pathname, props.href)
Router.prefetch(href)
}
}
return React.cloneElement(child, props) return React.cloneElement(child, props)
} }
} }
......
...@@ -25,7 +25,7 @@ export default class LinkPrefetch extends React.Component { ...@@ -25,7 +25,7 @@ export default class LinkPrefetch extends React.Component {
wantLinkPrefetch() wantLinkPrefetch()
const props = { const props = {
...this.props, ...this.props,
prefetch: this.props.prefetch === false ? this.props.prefetch : true prefetch: this.props.prefetch !== false
} }
return (<Link {...props} />) return (<Link {...props} />)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment