getServerSideProps는 SSR을 구현하기 위한 함수로, 페이지 요청시 실행되어 이 함수에서 반환된 props가 포함되어 페이지가 pre-render된다.(next에서 돌아가는 함수)
import Head from "next/head";
// 렌더링 파트
export default function Post({
post
}: Props) {
return (
<div>
<Head>
<title>{post.title} | chanstory</title>
<meta name="description" content={post.content} />
<meta name="keywords" content={post.tags} />
</Head>
<div>
// ... view
</div>
</div>
);
}
// SSR 파트
export const getServerSideProps = async function ({
req,
query,
}: NextPageContext) {
const id = Number(query.id); // postId
// 데이터 load api를 호출하여 post data load
const post = await client.post.findUnique({
where: { id: +id }
});
return {
props: {
post
},
};
};
<Link href="/" prefetch="false" passHref>
text
</Link>