2022-04-06 10:41:42 -07:00
|
|
|
import type { GetServerSideProps, GetStaticPaths, GetStaticProps } from "next";
|
2022-03-07 20:42:44 -08:00
|
|
|
|
2022-03-22 20:06:15 -07:00
|
|
|
import type { Post } from "@lib/types";
|
2022-03-21 20:30:45 -07:00
|
|
|
import PostPage from "@components/post-page";
|
2022-03-06 16:46:59 -08:00
|
|
|
|
2022-03-22 20:06:15 -07:00
|
|
|
export type PostProps = {
|
2022-03-21 18:51:19 -07:00
|
|
|
post: Post
|
2022-03-21 14:20:20 -07:00
|
|
|
}
|
2022-03-06 22:16:08 -08:00
|
|
|
|
2022-03-22 20:06:15 -07:00
|
|
|
const PostView = ({ post }: PostProps) => {
|
|
|
|
return <PostPage post={post} />
|
2022-03-06 16:46:59 -08:00
|
|
|
}
|
|
|
|
|
2022-04-06 10:41:42 -07:00
|
|
|
export const getServerSideProps: GetServerSideProps = async ({ params, res }) => {
|
2022-03-21 17:20:41 -07:00
|
|
|
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 || "",
|
2022-03-19 20:15:17 -04:00
|
|
|
}
|
2022-03-21 17:20:41 -07:00
|
|
|
})
|
2022-03-13 01:13:35 -03:00
|
|
|
|
2022-04-06 10:41:42 -07:00
|
|
|
const sMaxAge = 60 * 60 * 24
|
|
|
|
res.setHeader(
|
|
|
|
'Cache-Control',
|
|
|
|
`public, s-maxage=${sMaxAge}, max-age=${sMaxAge}`
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!post.ok || post.status !== 200) {
|
2022-03-26 00:05:05 -07:00
|
|
|
return {
|
|
|
|
redirect: {
|
2022-04-06 10:41:42 -07:00
|
|
|
destination: '/404',
|
|
|
|
permanent: false,
|
2022-03-26 00:05:05 -07:00
|
|
|
},
|
2022-04-06 10:41:42 -07:00
|
|
|
props: {}
|
2022-03-26 00:05:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 10:41:42 -07:00
|
|
|
|
|
|
|
const json = await post.json();
|
|
|
|
|
2022-03-15 22:49:41 -04:00
|
|
|
return {
|
|
|
|
props: {
|
2022-04-06 10:41:42 -07:00
|
|
|
post: json
|
|
|
|
}
|
2022-03-15 22:49:41 -04:00
|
|
|
}
|
2022-03-06 16:46:59 -08:00
|
|
|
}
|
|
|
|
|
2022-03-21 20:30:45 -07:00
|
|
|
export default PostView
|
2022-03-21 15:55:21 -07:00
|
|
|
|