-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy path[id].js
55 lines (46 loc) · 1.21 KB
/
[id].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
import Error from 'next/error'
import Link from 'next/link'
const Show = ({ errorCode, show, env }) => {
// If show item was not found, render 404 page
if (errorCode) {
return <Error statusCode={errorCode} />
}
// Otherwise, render show
return (
<div>
<p>
This page uses getInitialProps() to fetch the show with the ID provided
in the URL: /shows/:id
<br />
Refresh the page to see server-side rendering in action.
<br />
You can also try changing the ID to any other number between 1-10000.
Env: {env}
</p>
<hr />
<h1>Show #{show.id}</h1>
<p>{show.name}</p>
<hr />
<Link href="/">
<a>Go back home</a>
</Link>
</div>
)
}
export const getServerSideProps = async ({ params, req }) => {
// The ID to render
const { id } = params
console.log(req.headers)
const res = await fetch(`https://api.tvmaze.com/shows/${id}`)
const data = await res.json()
// Set error code if show item could not be found
const errorCode = res.status > 200 ? res.status : false
return {
props: {
errorCode,
show: data,
env: process.env.HELLO_WORLD || null,
},
}
}
export default Show