Skip to content

Commit

Permalink
fix prefetch url (vercel#1154)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkzawa authored and timneutkens committed Feb 15, 2017
1 parent 141ab99 commit 540a86f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
31 changes: 21 additions & 10 deletions lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<Link>example</Link>) we wrap it in an <a> tag
if (typeof children === 'string') {
children = <a>{children}</a>
Expand All @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/prefetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<Link {...props} />)
Expand Down

0 comments on commit 540a86f

Please sign in to comment.