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 data-fetch and analyze-bundles examples with getInitialProps to SSG #11014 #11021

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions examples/analyze-bundles/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ import React from 'react'
import Link from 'next/link'

export default class Index extends React.Component {
static getInitialProps ({ req }) {
if (req) {
// Runs only in the server
const faker = require('faker')
const name = faker.name.findName()
return { name }
}

// Runs only in the client
return { name: 'Arunoda' }
}

render () {
const { name } = this.props
return (
Expand All @@ -29,3 +17,15 @@ export default class Index extends React.Component {
)
}
}

export async function getServerSideProps({req}) {
if (req) {
// Runs only in the server
const faker = require('faker')
const name = faker.name.findName()
return { props : { name } }
}

// Runs only in the client
return { name: 'Arunoda' }
tajpouria marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion examples/data-fetch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ now
Next.js was conceived to make it easy to create universal apps. That's why fetching data
on the server and the client when necessary is so easy with Next.

Using `getInitialProps` fetches data on the server for SSR and then on the client when the component is re-mounted (not on the first paint).
Using `getStaticProps` fetches data at build time.
4 changes: 2 additions & 2 deletions examples/data-fetch/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function Index (props) {
)
}

Index.getInitialProps = async () => {
export async function getStaticProps() {
const res = await fetch('https://api.github.com/repos/zeit/next.js')
const json = await res.json() // better use it inside try .. catch
return { stars: json.stargazers_count }
return { props : { stars: json.stargazers_count } }
}

export default Index
4 changes: 2 additions & 2 deletions examples/data-fetch/pages/preact.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function Preact (props) {
)
}

Preact.getInitialProps = async () => {
export async function getStaticProps() {
const res = await fetch('https://api.github.com/repos/developit/preact')
const json = await res.json() // better use it inside try .. catch
return { stars: json.stargazers_count }
return { props : { stars: json.stargazers_count } }
}

export default Preact