/
[post].js
48 lines (39 loc) · 953 Bytes
/
[post].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
import { useRouter } from 'next/router'
export default function Post(props) {
const router = useRouter()
if (typeof window !== 'undefined' && !window.initialHref) {
window.initialHref = window.location.href
}
if (router.isFallback) return <p>Loading...</p>
return (
<>
<p id="gsp">getStaticProps</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}
export const getStaticProps = ({ params }) => {
if (params.post.startsWith('redir')) {
let destination = '/404'
if (params.post.includes('dest-')) {
destination = params.post.split('dest-').pop().replace(/_/g, '/')
}
return {
redirect: {
destination,
permanent: params.post.includes('permanent'),
},
}
}
return {
props: {
params,
},
}
}
export const getStaticPaths = () => {
return {
paths: ['first', 'second'].map((post) => ({ params: { post } })),
fallback: true,
}
}