Skip to content

Commit

Permalink
Refactor with-loading example (vercel#8560)
Browse files Browse the repository at this point in the history
* Remove inline styles in favor of styled-jsx

* Remove getInitialProps from app

Allow automatic static optimization

* Remove _document.js

We really don’t need it in this example

* Migrate Class to Function Components
  • Loading branch information
HaNdTriX authored and Luis Alvarez D committed Sep 2, 2019
1 parent a09036e commit 4a1215b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 71 deletions.
39 changes: 16 additions & 23 deletions examples/with-loading/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import App from 'next/app'
import Link from 'next/link'
import NProgress from 'nprogress'
import Router from 'next/router'

const linkStyle = {
margin: '0 10px 0 0'
}
import Head from 'next/head'

Router.events.on('routeChangeStart', url => {
console.log(`Loading: ${url}`)
Expand All @@ -16,35 +13,31 @@ Router.events.on('routeChangeComplete', () => NProgress.done())
Router.events.on('routeChangeError', () => NProgress.done())

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 (
<>
<div style={{ marginBottom: 20 }}>
<Head>
{/* Import CSS for nprogress */}
<link rel='stylesheet' type='text/css' href='/static/nprogress.css' />
</Head>
<nav>
<style jsx>{`
a {
margin: 0 10px 0 0;
}
`}</style>
<Link href='/'>
<a style={linkStyle}>Home</a>
<a>Home</a>
</Link>
<Link href='/about'>
<a style={linkStyle}>About</a>
<a>About</a>
</Link>
<Link href='/forever'>
<a style={linkStyle}>Forever</a>
<a>Forever</a>
</Link>
<a href='/non-existing' style={linkStyle}>
Non Existing Page
</a>
</div>

<a href='/non-existing'>Non Existing Page</a>
</nav>
<Component {...pageProps} />
</>
)
Expand Down
23 changes: 0 additions & 23 deletions examples/with-loading/pages/_document.js

This file was deleted.

21 changes: 9 additions & 12 deletions examples/with-loading/pages/about.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import React, { Component } from 'react'
import React from 'react'

export default class About extends Component {
// Add some delay
static async getInitialProps () {
await new Promise(resolve => {
setTimeout(resolve, 500)
})
return {}
}
const AboutPage = () => <p>This is about Next.js!</p>

render () {
return <p>This is about Next!</p>
}
AboutPage.getInitialProps = async () => {
await new Promise(resolve => {
setTimeout(resolve, 500)
})
return {}
}

export default AboutPage
21 changes: 9 additions & 12 deletions examples/with-loading/pages/forever.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import React, { Component } from 'react'
import React from 'react'

export default class Forever extends Component {
// Add some delay
static async getInitialProps () {
await new Promise(resolve => {
setTimeout(resolve, 3000)
})
return {}
}
const ForeverPage = () => <p>This page was rendered for a while!</p>

render () {
return <p>This page was rendered for a while!</p>
}
ForeverPage.getInitialProps = async () => {
await new Promise(resolve => {
setTimeout(resolve, 3000)
})
return {}
}

export default ForeverPage
4 changes: 3 additions & 1 deletion examples/with-loading/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'

export default () => <p>Hello Next!</p>
const IndexPage = () => <p>Hello Next.js!</p>

export default IndexPage

0 comments on commit 4a1215b

Please sign in to comment.