π΅οΈββοΈ getStaticProps λ?
function Home ({data}){ <h1>{data}</h1> } export async function getStaticProps(context) { const data = await fetch('url') return { props: {data}, // will be passed to the page component as props } }
λ°νλ propsλ₯Ό μ¬μ©νμ¬ λΉλ μ μ΄ νμ΄μ§λ₯Ό 미리 λ λλ§νλ
(Static Site Generation) κ°λ μ΄λ€.μ μμ μ½λ ν νμ΄μ§μμ
getStaticProps
ν¨μλ₯Όexport
νκ² λλ©΄ μ΅μ΄ λΉλ μprops
λ°μ΄ν°λ₯Ό κ°μ§κ³pre-rendering
νλ€.
β μΈμ μ¬μ©νλ κ² μ’μκΉ?
1. νμ΄μ§ λ λλ§μ νμν λ°μ΄ν°κ° μ¬μ©μ μμ²μ μμ λΉλ μκ°μ μ¬μ©ν μ μλ κ²½μ°.
2. νμ΄μ§κ° SEOλ₯Ό μν΄ μ¬μ λ λλ§λμ΄μΌ νκ³ , λΉ λ₯΄κ² λ‘λ©λμ΄μΌ νλ κ²½μ°.
β μ₯μ
νΈμΆ μ λ§λ€ fetch νμ§ μμμgetServerSideProps
λ³΄λ€ μ±λ₯λ©΄μμ μ’λ€.
π΅οΈββοΈ getServerSideProps λ?
function Home ({data}){ <h1>{data}</h1> } export async function getServerSideProps(context) { const data = await fetch('url') return { props: {data}, // will be passed to the page component as props } }
λ°νλ propsλ₯Ό μ¬μ©νμ¬ κ° μμ² μ μ΄ νμ΄μ§λ₯Ό 미리 λ λλ§νλ
(Dynamic Site Generation) κ°λ μ΄λ€.μ μμ μ½λ ν νμ΄μ§μμ
getServerSideProps
ν¨μλ₯Όexport
νκ² λλ©΄ νμ΄μ§ μμ² μprops
λ°μ΄ν°λ₯Ό κ°μ§κ³pre-rendering
νλ€.
β μΈμ μ¬μ©νλ κ² μ’μκΉ?
1. λμ μΌλ‘ νμ΄μ§ λ λλ§μ΄ νμν κ²½μ°.
β μ₯λ¨μ
1. νμ΄μ§λ₯Ό μΈμ λ μμ ν μ μλ€.
2. νΈμΆ μ λ§λ€ fetch κ° μ΄λ£¨μ΄μ ΈgetStaticProps
λ³΄λ€ μ±λ₯μ΄ λ¨μ΄μ§λ€.
π΅οΈββοΈ getStaticPaths λ?
export default function Post({ post }) { // Render post... } // pages/posts/[id].js // Generates `/posts/1` and `/posts/2` export async function getStaticPaths() { return { paths: [{ params: { id: '1' } }, { params: { id: '2' } }], fallback: false, // can also be true or 'blocking' } } // `getStaticPaths` requires using `getStaticProps` export async function getStaticProps(context) { return { // Passed to the page component as props props: { post: {} }, } }
λμ κ²½λ‘λ₯Ό κ°μ§κ³ μλ νμ΄μ§κ° μκ³ , κ·Έ νμ΄μ§κ°
getStaticProps
λ₯Ό μ¬μ©νλ€λ©΄getStaticPaths
λ₯Ό ν΅ν΄ λΉλ νμ λ μ μ μΌλ‘ λ λλ§ν κ²½λ‘λ₯Ό μ€μ ν΄μΌ νλ€.μ μμ μ½λμμ
getStaticPaths
λμ κ²½λ‘λ₯Ό μ¬μ©νλ νμ΄μ§
(/posts/1, /posts/2)μμgetStaticProps
ν¨μλ₯Όexport
νλ©΄ μ§μ ν λͺ¨λ κ²½λ‘λ₯Ό μ¬μ λ λλ§νλ€.
β μΈμ μ¬μ©νλ κ² μ’μκΉ?
1. λμ κ²½λ‘λ₯Ό μ¬μ©νκ³ νμ΄μ§λ₯Ό μ μ μΌλ‘ μ¬μ λ λλ§νλ κ²½μ° μ¬μ©ν΄μΌ νλ€.
β μ₯λ¨μ
1. λμ κ²½λ‘λ₯Ό μ¬μ©νλ κ²½μ° νμ©μ±μ΄ λλ€.
2.getStaticProps
μ κ°μ΄ μ¬μ©ν΄μΌ νλ€.(μ μ )