mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
fix prefetch url (#1154)
This commit is contained in:
parent
141ab99888
commit
540a86f007
31
lib/link.js
31
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 (<Link>example</Link>) we wrap it in an <a> tag
|
||||
if (typeof children === 'string') {
|
||||
children = <a>{children}</a>
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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} />)
|
||||
|
|
Loading…
Reference in a new issue