Next.js Pre rendering and Data Fetching - TEAM-ARK/inflearn-clone-front GitHub Wiki

Next.js version 12 ์—…๋ฐ์ดํŠธ ํ›„ .server.js๋กœ ํŽ˜์ด์ง€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๋ฉด getServerSideProps, getStaticProps ๋‘˜๋‹ค ํ•„์š”์—†๋‹ค๊ณ  ํ•˜๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ์ด๊ฒƒ์„ ์‹คํ—˜ํ•ด๋ณด๊ธฐ ์ „์— getServerSideProps์„ ๋จผ์ € ์‚ฌ์šฉํ•ด๋ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

์ด๋ฒˆ์— ๋‚˜ํ˜„๋‹˜์˜ PR์—์„œ router์— shallow ์˜ต์…˜์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋๋Š”๋ฐ ์ด๊ฒƒ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ด๋ณด๋ ค ํ•œ๋‹ค.

Pre-rendering and Data Fetching

Two Forms of Pre-rendering

  • Static Generation : getStaticProps

  • Server-side Rendering : getServerSideProps

getStaticProps์™€ getServerSideProps in next.js

๊ตฌ๋ฒ„์ „ next์—์„œ๋Š” data fetching์„ getInitialProps์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋Š”๋ฐ, 9.3๋ถ€ํ„ฐ๋Š” getStaticProps, getStaticPaths, getServerSideProps์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.

getStaticProps

  • ๋นŒ๋“œ์‹œ ๊ณ ์ •๋˜๋Š” ๊ฐ’์œผ๋กœ, ๋นŒ๋“œ์ดํ›„์—๋Š” ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
// **************************************** e.g.1,
export default function Home(props) { ... }

export async function getStaticProps() {
  // Get external data from the file system, API, DB, etc.
  const data = ...

  // The value of the `props` key will be
  //  passed to the `Home` component
  return {
    props: ...
  }
}
  
// **************************************** e.g.2,
function Blog({ posts }) {
  return (
    <ul>
      {posts.map((post) => (
        <li>{post.title}</li>
      ))}
    </ul>
  )
}

export async function getStaticProps() {
  const res = await fetch('https://.../posts')
  const posts = await res.json()

  // By returning { props: posts }, the Blog component
  // will receive `posts` as a prop at build time
  return {
    props: {
      posts,
    },
  }
}

export default Blog

getServerSideProps

  • ๋นŒ๋“œ์™€ ์ƒ๊ด€์—†์ด, ๋งค ์š”์ฒญ๋งˆ๋‹ค ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

์ ์šฉ

  • useEffect์—์„œ dispatch๋ฅผ ์ด์šฉํ•ด์„œ reqeust action์„ ๋ณด๋‚ด๋Š” ๊ฒƒ์„ getServerSideProps์„ ์ด์šฉํ•˜๋ ค๊ณ  ํ–ˆ์œผ๋‚˜ hooks rule์— ์œ„๋ฐฐ(hook์€ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•จ)
  • ์‹คํ—˜ ๋ถˆ๊ฐ€

getStaticProps vs. getServerSideProps

getStaticProps

"๋นŒ๋“œ ์‹œ์— ๋”ฑ ํ•œ ๋ฒˆ"๋งŒ ํ˜ธ์ถœ๋˜๊ณ , ๋ฐ”๋กœ static file๋กœ ๋นŒ๋“œ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์ดํ›„ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. SSG (Static Site Generation) ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.

getServerSideProps

"page๊ฐ€ ์š”์ฒญ๋ฐ›์„๋•Œ๋งˆ๋‹ค" ํ˜ธ์ถœ๋˜์–ด pre-renderingํ•ฉ๋‹ˆ๋‹ค. SSR (Server Side Rendering) ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. pre-render๊ฐ€ ๊ผญ ํ•„์š”ํ•œ ๋™์  ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” page์— ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋งค ์š”์ฒญ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋ฏ€๋กœ ์„ฑ๋Šฅ์€ getStaticProps์— ๋’ค์ง€์ง€๋งŒ, ๋‚ด์šฉ์„ ์–ธ์ œ๋“  ๋™์ ์œผ๋กœ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

// getStaticProp()์— ์˜ํ•ด ๋นŒ๋“œ์‹œ์— ๊ฒŒ์‹œ๋ฌผ์ด ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค.
function Blog({ posts }) {
  return (
    <ul>
      {posts.map((post) => (
        <li>{post.title}</li>
      ))}
    </ul>
  )
}

export async function getStaticProps() {
  // ์™ธ๋ถ€ API endpoint๋กœ Call ํ•˜์—ฌ Post ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  const res = await fetch('https://.../posts')
  const posts = await res.json()

  // posts ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ธด prop๋ฅผ ๋นŒ๋“œ์‹œ๊ฐ„์— Blog ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  return {
    props: {
      posts,
    },
  }
}

export default Blog

๊ณ ์ฐฐ

  • getServerSideProps์—์„  hooks๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. fetch๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ ๋’ค props๋ฅผ return ํ•œ ๊ฒƒ์„ ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ๋  ๊ฒƒ ๊ฐ™๋‹ค.
  • ๋ฆฌ๋•์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„์„œ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง์„ ๋ชจ๋‘ ๋ฆฌ๋•์Šค์— ๋‹ด์„ ์ˆ˜ ์—†๋Š” ์ ์€ ์•„์‰ฌ์šด ๊ฒƒ ๊ฐ™๋‹ค.
  • React 18๋ฒ„์ „์ด ์ •์‹ ์ถœ์‹œ๋˜๊ณ  ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์˜ dependencies์—์„œ React18์ด ์ถ”๊ฐ€๊ฐ€ ๋œ๋‹ค๋ฉด Next.js 12์˜ React Server Components๋ฅผ ์ด์šฉํ•ด์„œ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ณ  ์‹ถ๋‹ค.

์ฐธ๊ณ 

Next.js 12 ๋ฒ„์ „์˜ React Server Components

image

๋ฌธ์ œ์ 1 : React 18๋ฒ„์ „์ด ํ•„์š”ํ•˜์ง€๋งŒ material UI์˜ dependency ์กฐ๊ฑด๊ณผ ๋งž์ง€ ์•Š์•„ ์„ค์น˜ ๋ถˆ๊ฐ€๋Šฅ

-> React 18๋ฒ„์ „ ์ •์‹ ์ถœ์‹œ ๋ฐ material UI ๋ฒ„์ „ ์˜ฌ๋ฆฐ ๋’ค ๋‹ค์‹œ ์‹œ๋„ํ•  ์˜ˆ์ •

๋ฌธ์ œ์ 2 : Typescript์—์„œ ์•„์ง ์ง€์›์ด ์•ˆ๋˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ž„

  • tsํŒŒ์ผ๋กœ ๋งŒ๋“ค ๊ฒฝ์šฐ jsx ๋ฌธ๋ฒ• ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ

์ฐธ๊ณ 

โš ๏ธ **GitHub.com Fallback** โš ๏ธ