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

feat: update api-routes example to SSG #11019

Merged
merged 2 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion examples/api-routes/components/Person.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Link from 'next/link'

export default ({ person }) => (
<li>
<Link href={`/person?id=${person.id}`}>
<Link href={`/person/${person.id}`}>
lfades marked this conversation as resolved.
Show resolved Hide resolved
<a>{person.name}</a>
</Link>
</li>
Expand Down
2 changes: 1 addition & 1 deletion examples/api-routes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"start": "next start"
},
"dependencies": {
"isomorphic-unfetch": "3.0.0",
"next": "latest",
"node-fetch": "2.6.0",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
Expand Down
6 changes: 3 additions & 3 deletions examples/api-routes/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Person from '../components/Person'
import fetch from 'isomorphic-unfetch'
import fetch from 'node-fetch'

const Index = ({ people }) => (
<ul>
Expand All @@ -9,11 +9,11 @@ const Index = ({ people }) => (
</ul>
)

Index.getInitialProps = async () => {
export async function getStaticProps() {
const response = await fetch('http://localhost:3000/api/people')
const people = await response.json()

return { people }
return { props: { people } }
}

export default Index
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from 'isomorphic-unfetch'
import fetch from 'node-fetch'

const Person = ({ data, status }) =>
status === 200 ? (
Expand Down Expand Up @@ -30,11 +30,29 @@ const Person = ({ data, status }) =>
<p>{data.message}</p>
)

Person.getInitialProps = async ({ query }) => {
const response = await fetch(`http://localhost:3000/api/people/${query.id}`)
export async function getStaticPaths() {
const response = await fetch('http://localhost:3000/api/people')
const data = await response.json()

const paths = data.map(person => ({
params: {
id: person.id,
},
}))

return { paths, fallback: false }
}

export async function getStaticProps({ params }) {
const response = await fetch(`http://localhost:3000/api/people/${params.id}`)
const data = await response.json()
return { data, status: response.status }

return {
props: {
data,
status: response.status,
},
}
}

export default Person