Commit 16e0ff9d authored by Henrik Wenz's avatar Henrik Wenz Committed by Joe Haddad

Update with-prefetching example (#9101)

## Changelog

- Remove `getInitialProps`
- Explain variants
- Reflect latest changes in the prefetch api
- Update UI
parent c5caa892
...@@ -42,5 +42,7 @@ now ...@@ -42,5 +42,7 @@ now
This example features: This example features:
- An app with four simple pages - An app with four simple pages
- The "about" page uses the imperative (i.e.: "manual") prefetching API to prefetch on hover
- It will prefetch all the pages in the background except the "contact" page - Home/Features: Default api
- About: Imperative api
- Contact: Disable api
import Router from 'next/router'
import Link from 'next/link'
export default () => (
<div>
{/* Prefetch using the declarative API */}
<Link prefetch href='/'>
<a>Home</a>
</Link>
<Link prefetch href='/features'>
<a>Features</a>
</Link>
{/* we imperatively prefetch on hover */}
<Link href='/about'>
<a
onMouseEnter={() => {
Router.prefetch('/about')
console.log('prefetching /about!')
}}
>
About
</a>
</Link>
<Link href='/contact'>
<a>
Contact (<small>NO-PREFETCHING</small>)
</a>
</Link>
<style jsx>{`
a {
margin-right: 10px;
}
`}</style>
</div>
)
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function Nav () {
const router = useRouter()
return (
<div className='root'>
<h2>Default</h2>
<p>
Automatically prefetch pages in the background as soon the Link appears
in the view:
</p>
<Link href='/'>
<a>Home</a>
</Link>{' '}
<Link href='/features'>
<a>Features</a>
</Link>
<h2>Imperative</h2>
<p>Prefetch on onMouseEnter or on other events:</p>
<Link prefetch={false} href='/about'>
<a
onMouseEnter={() => {
router.prefetch('/about')
console.log('prefetching /about!')
}}
>
About
</a>
</Link>
<h2>Disable</h2>
<p>Disable prefetching</p>
<Link prefetch={false} href='/contact'>
<a>Contact</a>
</Link>
<style jsx>{`
.root {
border-bottom: 1px solid grey;
padding-bottom: 8px;
}
a {
margin-right: 10px;
}
`}</style>
</div>
)
}
import App from 'next/app' import App from 'next/app'
import React from 'react' import React from 'react'
import Header from '../components/Header' import Nav from '../components/Nav'
export default class MyApp extends App { export default class MyApp extends App {
static async getInitialProps ({ Component, router, ctx }) {
let pageProps = {}
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
return { pageProps }
}
render () { render () {
const { Component, pageProps } = this.props const { Component, pageProps } = this.props
return ( return (
<> <>
<Header /> <Nav />
<Component {...pageProps} /> <Component {...pageProps} />
</> </>
) )
......
export default () => <p>This is the ABOUT page.</p> export default () => <h1>This is the ABOUT page.</h1>
export default () => <p>This is the CONTACT page.</p> export default () => <h1>This is the CONTACT page.</h1>
export default () => <p>This is the FEATURES page.</p> export default () => <h1>This is the FEATURES page.</h1>
export default () => <p>This is the HOME page</p> export default () => <h1>This is the HOME page</h1>
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