-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
index.js
64 lines (58 loc) · 1.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { useState } from 'react'
export default function Home({ book, inProduction }) {
const [reviews, setReviews] = useState(null)
const handleGetReviews = () => {
// Client-side request are mocked by `mocks/browser.js`.
fetch('/reviews')
.then((res) => res.json())
.then(setReviews)
}
if (inProduction) {
return (
<div>
<p>
This example does not work in production, as MSW is not intended for
use in production. In a real-world app, your request will hit the
actual backend instead.
</p>
</div>
)
}
return (
<div>
<img src={book.imageUrl} alt={book.title} width="250" />
<h1>{book.title}</h1>
<p>{book.description}</p>
<button onClick={handleGetReviews}>Load reviews</button>
{reviews && (
<ul>
{reviews.map((review) => (
<li key={review.id}>
<p>{review.text}</p>
<p>{review.author}</p>
</li>
))}
</ul>
)}
</div>
)
}
export async function getServerSideProps() {
// Server-side requests are mocked by `mocks/server.js`.
// In a real-world app this request would hit the actual backend.
try {
const res = await fetch('https://my.backend/book')
const book = await res.json()
return {
props: {
book,
},
}
} catch (error) {
return {
props: {
inProduction: true,
},
}
}
}