-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy path[id].js
58 lines (47 loc) · 1.38 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
56
57
58
import { useRouter } from 'next/router'
import Link from 'next/link'
const Show = ({ show, time }) => {
const router = useRouter()
if (router.isFallback) {
return <div>Loading...</div>
}
return (
<div>
<p>This page uses getStaticProps() to pre-fetch a TV show.</p>
<p>
Ids 1 and 2 are prerendered and others should show a{' '}
<a href="https://nextjs.org/docs/api-reference/data-fetching/get-static-paths#fallback-true">fallback page</a>{' '}
while rendering.
</p>
<p>The page should be revalidated after 60 seconds.</p>
<hr />
<h1>Show #{show.id}</h1>
<p>{show.name}</p>
<p>Rendered at {time} </p>
<hr />
<Link href="/">Go back home</Link>
</div>
)
}
export async function getStaticPaths() {
// Set the paths we want to pre-render
const paths = [{ params: { id: '1' } }, { params: { id: '2' } }]
// We'll pre-render only these paths at build time.
// { fallback: false } means other routes should 404.
return { paths, fallback: true }
}
export async function getStaticProps({ params }) {
// The ID to render
const { id } = params
const res = await fetch(`https://tvproxy.netlify.app/shows/${id}`)
const data = await res.json()
const time = new Date().toLocaleTimeString()
return {
props: {
show: data,
time,
},
revalidate: 60,
}
}
export default Show