diff --git a/lib/link.js b/lib/link.js index 93f7b07e..235bd982 100644 --- a/lib/link.js +++ b/lib/link.js @@ -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 () { - let { children, prefetch } = this.props + let { children } = this.props // Deprecated. Warning shown by propType check. If the childen provided is a string (example) we wrap it in an tag if (typeof children === 'string') { children = {children} @@ -79,15 +99,6 @@ export default class Link extends Component { 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) } } diff --git a/lib/prefetch.js b/lib/prefetch.js index 0970ecfd..ef667173 100644 --- a/lib/prefetch.js +++ b/lib/prefetch.js @@ -25,7 +25,7 @@ export default class LinkPrefetch extends React.Component { wantLinkPrefetch() const props = { ...this.props, - prefetch: this.props.prefetch === false ? this.props.prefetch : true + prefetch: this.props.prefetch !== false } return ()