getStaticProps , getStaticPathsgetServerSidePropsuseEffect()**를getStaticProps
빌드 시점에 api를 호출하고 데이터를 응답 받아서 HTML 을 완성하는 정적생성을 위한 메서드이다.
import { GetStaticProps } from 'next'
export const getStaticProps: GetStaticProps = async context => {
// 생략
}
getStaticPaths
export const getStaticPaths: GetStaticPaths = async () => {
return {
paths: [{ params: { id: '1' } }, { params: { id: '2' } }]
};
};
export const getStaticProps: GetStaticProps = async ({ params }) => {
const response = await axios(`${API_URL}/restaurant?id=${params.id}`);
return {
props: {
data: response.data.restaurant
}
};
};
getServerSideProps
사용자가 페이지에 접근하는 요청이 있을 때마다 getServerSideProps(서버측)에서 api를 호출하고 응답받은 데이터를 해당 컴포넌트의 props로 넘겨주어 HTML을 완성하여 클라이언트로 전달한다.
export const getServerSideProps: GetServerSideProps = async () => {
const all = await axios(`${API_URL}/restaurants`);
return {
props: {
data: all.data.restaurants
}
};
};
서버에서 데이터를 가져오기 위해 api를 호출하고 응답하는 시간 + 응답받은 데이터로 HTML을 완성하는 시간 이 있어 Time to first byte (TTFB)가 느리다.case 1) 데이터가 시간에 따라 계속 변경되어서 페이지를 접근할 때마다 새로운 api를 호출해야하는 상황
case 2) 무조건 서버측 렌더링을 해야하는 경우에만 사용한다.
그렇지 않은 경우에는 getStaticProps나 클라이언트측 렌더링이 낫다.
getInitialProps