Commit c40f184a authored by Shaleen Jain's avatar Shaleen Jain Committed by Tim Neutkens

Extend with-data-prefetch to handle advanced use cases (#3525)

parent 8dd8e200
...@@ -5,6 +5,34 @@ import { execOnce, warn } from 'next/dist/lib/utils' ...@@ -5,6 +5,34 @@ import { execOnce, warn } from 'next/dist/lib/utils'
import exact from 'prop-types-exact' import exact from 'prop-types-exact'
import { format, resolve, parse } from 'url' import { format, resolve, parse } from 'url'
export const prefetch = async (href) => {
// if we're running server side do nothing
if (typeof window === 'undefined') return
const url =
typeof href !== 'string'
? format(href)
: href
const { pathname } = window.location
const parsedHref = resolve(pathname, url)
const { query } =
typeof href !== 'string'
? href
: parse(url, true)
const Component = await Router.prefetch(parsedHref)
// if Component exists and has getInitialProps
// fetch the component props (the component should save it in cache)
if (Component && Component.getInitialProps) {
const ctx = { pathname: href, query, isVirtualCall: true }
await Component.getInitialProps(ctx)
}
}
// extend default next/link to customize the prefetch behaviour // extend default next/link to customize the prefetch behaviour
export default class LinkWithData extends Link { export default class LinkWithData extends Link {
// re defined Link propTypes to add `withData` // re defined Link propTypes to add `withData`
...@@ -33,32 +61,16 @@ export default class LinkWithData extends Link { ...@@ -33,32 +61,16 @@ export default class LinkWithData extends Link {
// our custom prefetch method // our custom prefetch method
async prefetch () { async prefetch () {
// if the prefetch prop is not defined or // if the prefetch prop is not defined do nothing
// we're running server side do nothing
if (!this.props.prefetch) return if (!this.props.prefetch) return
if (typeof window === 'undefined') return
const url =
typeof this.props.href !== 'string'
? format(this.props.href)
: this.props.href
const { pathname } = window.location
const href = resolve(pathname, url)
const { query } =
typeof this.props.href !== 'string'
? this.props.href
: parse(url, true)
const Component = await Router.prefetch(href)
// if withData prop is defined, Component exists and has getInitialProps // if withData prop is defined
// fetch the component props (the component should save it in cache) // prefetch with data
if (this.props.withData && Component && Component.getInitialProps) { // otherwise just prefetch the page
const ctx = { pathname: href, query, isVirtualCall: true } if (this.props.withData) {
await Component.getInitialProps(ctx) prefetch(this.props.href)
} else {
super.prefetch()
} }
} }
} }
import Link from '../components/link' import Link, { prefetch } from '../components/link'
// we just render a list of 3 articles having 2 with prefetched data // we just render a list of 3 articles having 2 with prefetched data
export default () => ( export default () => (
...@@ -16,8 +16,8 @@ export default () => ( ...@@ -16,8 +16,8 @@ export default () => (
</Link> </Link>
</li> </li>
<li> <li>
<Link href='/article?id=3' prefetch withData> <Link href='/article?id=3' >
<a>Article 3</a> <a onMouseOver={e => prefetch('/article?id=3')} >Article 3</a>
</Link> </Link>
</li> </li>
</ul> </ul>
......
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