2022-04-09 20:48:19 -04:00
|
|
|
import type { GetServerSideProps, GetStaticPaths, GetStaticProps } from "next"
|
2022-03-07 23:42:44 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
import type { Post } from "@lib/types"
|
|
|
|
import PostPage from "@components/post-page"
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
export type PostProps = {
|
2022-04-09 20:48:19 -04:00
|
|
|
post: Post
|
2022-03-21 17:20:20 -04:00
|
|
|
}
|
2022-03-07 01:16:08 -05:00
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
const PostView = ({ post }: PostProps) => {
|
2022-04-09 20:48:19 -04:00
|
|
|
return <PostPage post={post} />
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
export const getServerSideProps: GetServerSideProps = async ({
|
|
|
|
params,
|
|
|
|
res
|
|
|
|
}) => {
|
|
|
|
const post = await fetch(process.env.API_URL + `/posts/${params?.id}`, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"x-secret-key": process.env.SECRET_KEY || ""
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const sMaxAge = 60 * 60 * 24
|
|
|
|
res.setHeader(
|
|
|
|
"Cache-Control",
|
|
|
|
`public, s-maxage=${sMaxAge}, max-age=${sMaxAge}`
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!post.ok || post.status !== 200) {
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
destination: "/404",
|
|
|
|
permanent: false
|
|
|
|
},
|
|
|
|
props: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const json = await post.json()
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
post: json
|
|
|
|
}
|
|
|
|
}
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-03-21 23:30:45 -04:00
|
|
|
export default PostView
|