Skip to content

Commit 9e2ae69

Browse files
jazibjafriLuis Alvarez
andauthored
Update api-rest-example to SSG (#11362)
* Update api-rest-example to SSG * Use SWR for data fetching * Updated pages Co-authored-by: Luis Alvarez <luis@zeit.co>
1 parent 48650a7 commit 9e2ae69

File tree

4 files changed

+31
-35
lines changed

4 files changed

+31
-35
lines changed

examples/api-routes-rest/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
"start": "next start"
88
},
99
"dependencies": {
10-
"isomorphic-unfetch": "3.0.0",
1110
"next": "latest",
1211
"react": "^16.8.6",
13-
"react-dom": "^16.8.6"
12+
"react-dom": "^16.8.6",
13+
"swr": "^0.1.18"
1414
},
1515
"license": "ISC"
1616
}

examples/api-routes-rest/pages/api/users.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
// Fake users data
2-
const users = [
3-
{
4-
id: 1,
5-
},
6-
{ id: 2 },
7-
{ id: 3 },
8-
]
2+
const users = [{ id: 1 }, { id: 2 }, { id: 3 }]
93

104
export default (req, res) => {
115
// Get data from your database
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import fetch from 'isomorphic-unfetch'
1+
import useSwr from 'swr'
22
import Link from 'next/link'
33

4-
const Index = ({ users }) => (
5-
<ul>
6-
{users.map(user => (
7-
<li key={user.id}>
8-
<Link href="/user/[id]" as={`/user/${user.id}`}>
9-
<a>{`User ${user.id}`}</a>
10-
</Link>
11-
</li>
12-
))}
13-
</ul>
14-
)
4+
const fetcher = url => fetch(url).then(res => res.json())
155

16-
Index.getInitialProps = async () => {
17-
const response = await fetch('http://localhost:3000/api/users')
18-
const users = await response.json()
6+
export default function Index() {
7+
const { data, error } = useSwr('/api/users', fetcher)
198

20-
return { users }
21-
}
9+
if (error) return <div>Failed to load users</div>
10+
if (!data) return <div>Loading...</div>
2211

23-
export default Index
12+
return (
13+
<ul>
14+
{data.map(user => (
15+
<li key={user.id}>
16+
<Link href="/user/[id]" as={`/user/${user.id}`}>
17+
<a>{`User ${user.id}`}</a>
18+
</Link>
19+
</li>
20+
))}
21+
</ul>
22+
)
23+
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import fetch from 'isomorphic-unfetch'
1+
import { useRouter } from 'next/router'
2+
import useSwr from 'swr'
23

3-
const User = ({ user }) => <div>{user.name}</div>
4+
const fetcher = url => fetch(url).then(res => res.json())
45

5-
User.getInitialProps = async ({ query: { id } }, res) => {
6-
const response = await fetch(`http://localhost:3000/api/user/${id}`)
7-
const user = await response.json()
6+
export default function User() {
7+
const router = useRouter()
8+
const { data, error } = useSwr(`/api/user/${router.query.id}`, fetcher)
89

9-
return { user }
10-
}
10+
if (error) return <div>Failed to load user</div>
11+
if (!data) return <div>Loading...</div>
1112

12-
export default User
13+
return <div>{data.name}</div>
14+
}

0 commit comments

Comments
 (0)