{ const id = context.params!.id; const book = await fetchOnebiteBook(Number(id)); return { props: { books, } }"> { const id = context.params!.id; const book = await fetchOnebiteBook(Number(id)); return { props: { books, } }"> { const id = context.params!.id; const book = await fetchOnebiteBook(Number(id)); return { props: { books, } }">
export const getStaticPaths = () => {
return {
fallback: "false",
}
}
export const getStaticProps = async (
context: GetStaticPropsContext
) => {
const id = context.params!.id;
const book = await fetchOnebiteBook(Number(id));
return {
props: {
books,
}
}
export const getStaticPaths = () => {
return {
fallback: "blocking",
}
}
export const getStaticProps = async (
context: GetStaticPropsContext
) => {
const id = context.params!.id;
const book = await fetchOnebiteBook(Number(id));
return {
props: {
books,
}
}
export const getStaticPaths = () => {
return {
paths: [
{ params: { id : "1" } }, //반드시 문자열
{ params: { id : "2" } },
{ params: { id : "3" } },
],
fallback: "true",
}
}
export const getStaticProps = async (
context: GetStaticPropsContext
) => {
const id = context.params!.id;
const book = await fetchOnebiteBook(Number(id));
return {
props: {
books,
}
}
false: 어떤 페이지도 동적 생성하지 않고 404 반환한다.
blocking: SSR처럼 해당 경로로 이동하는 즉시 생성한다.
.next / server / pages / (경로) 하위에 해당 파라미터에 대한 html 과 json 정적 파일이 생성됨을 확인할 수 있다.
→ 한번만 만들면 서버에 저장되기 때문에, SSR과 SSG 가 결합된 형태로 동작한다.
→ 그러나 백엔드 요청이 큰 경우, 페이지 생성 중 로딩이 발생할 수 있다.
true: 위 예제처럼 지정한 params 경로에 대해서만 빌드 시 렌더링하고, 그 외 경로는 404 반환한다.
→ blocking과 마찬가지로, 없는 페이지에 대해 요청했을 때 페이지를 생성하는데 백엔드 요청이 느린 경우 props가 없는 페이지를 우선 반환한다.
export const getStaticPaths = () => {
return {
paths: [
{ params: { id : "1" } }, //반드시 문자열
{ params: { id : "2" } },
{ params: { id : "3" } },
],
fallback: "true",
}
}
export const getStaticProps = async (
context: GetStaticPropsContext
) => {
const id = context.params!.id;
const book = await fetchOnebiteBook(Number(id));
if (!book) { //404페이지로 리다이렉트
return {
notFound: true,
};
}
return {
props: {
book,
}
}
export default function page({ book }): InferGetStaticPropsType<typeof getStaticProps> {
const router = useRouter()
if (router.isFallback) { // router에서 fallback 상태 추출
return "로딩 중입니다."
}
if (!book) {
return "문제가 발생했습니다. 다시 시도해주세요."
}
}