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 api-rest-example to SSG #11362

Merged
merged 5 commits into from
Mar 28, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/api-routes-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"start": "next start"
},
"dependencies": {
"isomorphic-unfetch": "3.0.0",
"next": "latest",
"react": "^16.8.6",
"react-dom": "^16.8.6"
"react-dom": "^16.8.6",
"swr": "^0.1.18"
},
"license": "ISC"
}
8 changes: 1 addition & 7 deletions examples/api-routes-rest/pages/api/users.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
// Fake users data
const users = [
{
id: 1,
},
{ id: 2 },
{ id: 3 },
]
const users = [{ id: 1 }, { id: 2 }, { id: 3 }]

export default (req, res) => {
// Get data from your database
Expand Down
36 changes: 18 additions & 18 deletions examples/api-routes-rest/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import fetch from 'isomorphic-unfetch'
import useSwr from 'swr'
import Link from 'next/link'

const Index = ({ users }) => (
<ul>
{users.map(user => (
<li key={user.id}>
<Link href="/user/[id]" as={`/user/${user.id}`}>
<a>{`User ${user.id}`}</a>
</Link>
</li>
))}
</ul>
)
const fetcher = url => fetch(url).then(res => res.json())

Index.getInitialProps = async () => {
const response = await fetch('http://localhost:3000/api/users')
const users = await response.json()
export default function Index() {
const { data, error } = useSwr('/api/users', fetcher)

return { users }
}
if (error) return <div>Failed to load users</div>
if (!data) return <div>Loading...</div>

export default Index
return (
<ul>
{data.map(user => (
<li key={user.id}>
<Link href="/user/[id]" as={`/user/${user.id}`}>
<a>{`User ${user.id}`}</a>
</Link>
</li>
))}
</ul>
)
}
18 changes: 10 additions & 8 deletions examples/api-routes-rest/pages/user/[id].js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import fetch from 'isomorphic-unfetch'
import { useRouter } from 'next/router'
import useSwr from 'swr'

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

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

return { user }
}
if (error) return <div>Failed to load user</div>
if (!data) return <div>Loading...</div>

export default User
return <div>{data.name}</div>
}