Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update with-prefetching example #9101

Merged
merged 2 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/with-prefetching/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ now
This example features:

- 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
39 changes: 0 additions & 39 deletions examples/with-prefetching/components/Header.js

This file was deleted.

47 changes: 47 additions & 0 deletions examples/with-prefetching/components/Nav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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>
)
}
14 changes: 2 additions & 12 deletions examples/with-prefetching/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import App from 'next/app'
import React from 'react'
import Header from '../components/Header'
import Nav from '../components/Nav'

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 () {
const { Component, pageProps } = this.props
return (
<>
<Header />
<Nav />
<Component {...pageProps} />
</>
)
Expand Down
2 changes: 1 addition & 1 deletion examples/with-prefetching/pages/about.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default () => <p>This is the ABOUT page.</p>
export default () => <h1>This is the ABOUT page.</h1>
2 changes: 1 addition & 1 deletion examples/with-prefetching/pages/contact.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default () => <p>This is the CONTACT page.</p>
export default () => <h1>This is the CONTACT page.</h1>
2 changes: 1 addition & 1 deletion examples/with-prefetching/pages/features.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default () => <p>This is the FEATURES page.</p>
export default () => <h1>This is the FEATURES page.</h1>
2 changes: 1 addition & 1 deletion examples/with-prefetching/pages/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default () => <p>This is the HOME page</p>
export default () => <h1>This is the HOME page</h1>