CoastalCommitsPastes/client/pages/post/[id].tsx
Max Leiter 2ecf1b21ca
client: remove server as build requirement
This means public/unlisted posts and the home-page are no longer
generated at build-time, so TTFB may be increased for the first user to
load/access a page. Cache-Control headers are set so if the RP / server
supports it the results should be cached for future users.
2022-04-06 10:41:42 -07:00

50 lines
1.1 KiB
TypeScript

import type { GetServerSideProps, GetStaticPaths, GetStaticProps } from "next";
import type { Post } from "@lib/types";
import PostPage from "@components/post-page";
export type PostProps = {
post: Post
}
const PostView = ({ post }: PostProps) => {
return <PostPage post={post} />
}
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
}
}
}
export default PostView