Next.js

[λ‚΄λ³΄λ‚΄λ²ˆ] Understanding Next.js Data Fetching (CSR, SSR, SSG, ISR)

EveryoneIsAWeirdo 2022. 3. 6. 13:50

λ‚΄λ³΄λ‚΄λ²ˆ(λ‚΄κ°€ 보렀고 λ‚΄κ°€ λ²ˆμ—­ν•œ...) 아티클 

β€» μ˜μ–΄ μ „κ³΅μžλ„ ν•΄μ™Έ μœ ν•™νŒŒλ„ μ•„λ‹ˆκΈ°μ— λ²ˆμ—­μ—λŠ” μ˜μ—­, μ˜€μ—­, κ΅¬κΈ€ λ²ˆμ—­μ΄ 무수히 λ§Žμ„ 수 있으며, 개인적인 곡뢀 λͺ©μ μœΌλ‘œ λ²ˆμ—­ν•˜λ‹€ λ³΄λ‹ˆ μ˜€νƒ€λ„ λ§Žμ„ 수 μžˆλ‹€. μ •ν™•ν•œ λ‚΄μš©μ€ ν•΄λ‹Ή 아티클을 직접 μ‚΄νŽ΄λ³΄κ±°λ‚˜ λ‹€λ₯Έ 정보듀을 더 μ°Ύμ•„λ³΄λŠ” 것을 μΆ”μ²œν•œλ‹€.

(ν•˜μ§€λ§Œ λŒ“κΈ€ ν”Όλ“œλ°±λ„ ν™˜μ˜ν•©λ‹ˆλ‹€πŸ˜ƒ 글을 μ½μ–΄λ³΄μ‹œκ³  도움을 μ–»μœΌμ…¨λ‹€λ©΄ 아티클 μ €μžμ˜ λΈ”λ‘œκ·Έμ—μ„œ μ’‹μ•„μš”λ„ λˆŒλŸ¬μ£Όμ„Έμš”.)

Understanding Next.js Data Fetching (CSR, SSR, SSG, ISR) by Theodorus Clarence 원문 읽기 >>

 

Understanding Next.js Data Fetching (CSR, SSR, SSG, ISR) | theodorusclarence.com

Thorough explanation on Next.js data fetching method such as CSR, SSR, SSG, and ISR.

theodorusclarence.com

 

πŸ‘€ 이 아티클을 λ²ˆμ—­ν•˜κ²Œ 된 이유

더보기

μ‚¬μ΄λ“œ ν”„λ‘œμ νŠΈλ₯Ό μ§„ν–‰ν•˜λ©΄μ„œ λ‚΄κ°€ 자체적으둜 λ§Œλ“  APIκ°€ 있고 κ·Έ APIμ—μ„œ 데이터λ₯Ό κ°€μ Έμ™€μ„œ μ‚¬μš©μžμ—κ²Œ 보여주어야 ν•˜λŠ” 화면이 μžˆμ—ˆλ‹€. 맀번 데이터가 λ™μΌν•˜κ²Œ 보여지면 μž¬λ―Έκ°€ μ—†μœΌλ‹ˆ λ“€μ–΄μ˜¬ λ•Œλ§ˆλ‹€ λΉ λ₯΄κ²Œ λ‘œλ“œκ°€ λ˜λ©΄μ„œλ„ λžœλ€ν•˜κ²Œ 보여지기λ₯Ό μ›ν–ˆκ³  크둬 Lighthouse의 μ μˆ˜λ„ μ–΄λ–»κ²Œλ“  높여보렀고 ν˜ˆμ•ˆμ΄ λ˜μ–΄μžˆμ—ˆλ‹€. κ·Έλž˜μ„œ Next.js Data Fetching λ¬Έμ„œλ₯Ό 보며 이것저것 μ‹œλ„ν•΄λ΄€λ˜ 기얡이 λ‚œλ‹€. λͺ…ν™•ν•˜κ²Œ SSR, SSG, ISR을 μ΄ν•΄ν•˜μ§€ λͺ»ν•œ μƒνƒœμ—μ„œ 괜히 revalidateλ₯Ό μΆ”κ°€ν•΄μ„œ μˆ«μžλ„ 바꿔보고 λ‹€λ₯Έ μ˜΅μ…˜λ„ 넣어보고 κ·Έλž¬μ—ˆλŠ”λ°... μš°μ„  λ¬Έμ œλŠ” ν•΄κ²°ν•˜κ³  λ‚œ λ’€ ν•œμ°Έ 뒀에 μ œλŒ€λ‘œ 짚고 λ„˜μ–΄κ°€λ©΄ μ’‹μ§€μ•Šμ„κΉŒ ν•˜λŠ” 생각에 μ—¬λŸ¬ 아티클을 λ’€μ Έλ΄€λ‹€. κ·ΈλŸ¬λ‹€ λˆˆμ— 띈 아티클이 ν˜„μž¬ λ²ˆμ—­ν•˜λ €κ³  ν•˜λŠ” 아티클이닀. 살짝 ν›‘μ–΄λ§Œ 봀을 λ•ŒλΆ€ν„° 였! ꡉμž₯히 잘 μ •λ¦¬λœ λŠλ‚Œμ΄λ‹€! μ‹Άμ—ˆμ§€λ§Œ 각작고 μ½μ–΄μ•Όν•œλ‹€λŠ” 생각에 ν•œλ‘λ‹¬ λ―Έλ€„λ‘μ—ˆλ‹€ μ΄μ œμ•Ό λ²ˆμ—­ν•΄λ³Έλ‹€. 

 

[λ‚΄λ³΄λ‚΄λ²ˆ] Next.js docs - Data Fetching

λ‚΄λ³΄λ‚΄λ²ˆ( λ‚΄κ°€ 보렀고 λ‚΄κ°€ λ²ˆμ—­ν•œ...) Next.js docs 2021λ…„ 3μ›” 29일 κΈ°μ€€ Next.js 곡식 λ¬Έμ„œλ₯Ό λ²ˆμ—­ν–ˆλ‹€. β€» μ˜μ–΄ μ „κ³΅μžλ„ ν•΄μ™Έ μœ ν•™νŒŒλ„ μ•„λ‹ˆκΈ°μ— λ²ˆμ—­μ—λŠ” μ˜μ—­, μ˜€μ—­μ΄ 무수히 λ§Žμ„ 수 있으며, 혼자

birdmee.tistory.com


Introduction 

When I started to learn Next.js, I got overwhelmed with the list of abbreviations that looks similar, I didn't know what it is and what is the difference. It is quite confusing because when using Create React App, we usually only use 1 strategy to fetch data from API which is using useEffect.

λ‚΄κ°€ 처음으둜 Next.jsλ₯Ό 배우기 μ‹œμž‘ν–ˆμ„ λ•Œ λ­”κ°€ λΉ„μŠ·ν•΄λ³΄μ΄λŠ” μ•½μ–΄λ“€μ˜ λͺ©λ‘μ„ 보고 λ‹Ήν™©μŠ€λŸ¬μ› λ‹€. 그게 뭔지도 λͺ¨λ₯΄κ³  차이점도 λͺ°λžμ—ˆλ‹€. κ½€ ν—·κ°ˆλ ΈλŠ”λ° Create React App을 μ‚¬μš©ν•  λ•Œ λŒ€κ°œ ν•œ κ°€μ§€ μ „λž΅, useEffectλ₯Ό μ‚¬μš©ν•˜μ—¬ APIλ‘œλΆ€ν„° 데이터λ₯Ό κ°€μ Έμ˜€κΈ° λ•Œλ¬Έμ΄λ‹€. 

Next.js has many data fetching strategies. Although initially Next.js was well known to be a Server-Side Rendering Framework, it turns out that Next.js has 4 methods of Data Fetching. Here is the short explanation each so you get familiar with the abbreviation of CSR, SSR, SSG, ISR.

Next.jsλŠ” 데이터λ₯Ό κ°€μ Έμ˜€λŠ” μ—¬λŸ¬ μ „λ ₯듀이 μžˆλ‹€. Next.jsκ°€ μ²˜μŒμ—λŠ” μ„œλ²„ μ‚¬μ΄λ“œ λ Œλ”λ§ ν”„λ ˆμž„μ›Œν¬λ‘œ 잘 μ•Œλ €μ§€κΈ΄ ν–ˆμ§€λ§Œ, Next.jsλŠ” 데이터λ₯Ό κ°€μ Έμ˜€λŠ” 4κ°€μ§€ 방법이 있게 λ˜μ—ˆλ‹€. 이제 μ—¬λŸ¬λΆ„μ΄ CSR, SSR, SSG, ISRμ΄λΌλŠ” 약어에 μ΅μˆ™ν•΄μ§€λ„λ‘ 짧게 μ„€λͺ…을 μ§„ν–‰ν•˜κ² λ‹€. 

  • CSR - Client-Side Rendering, this is the usual kind of data fetching using useEffect, it will fetch the data from the API every single page request on the client-side (after the page is rendered, then the function will run).
  • SSR - Server-Side Rendering, will run a special function to fetch data from API every page request on the server-side (before the page is loaded, that special function will run first, creating a delay, then after that, it will serve the page).
  • SSG - Static Site Generation, will run a special function to fetch data once when that page builds.
  • ISR – Incremental Static Regeneration, this is a new thing, shortly put, a combination of SSG, and SSR, where it served statically, but at a certain time and certain condition that page will rebuild and fetch the data from the API again.

  • CSR - Client-Side Rendering, 이것은 useEffectλ₯Ό μ‚¬μš©ν•˜μ—¬ 데이터λ₯Ό κ°€μ Έμ˜€λŠ” 것과 같은 μ’…λ₯˜μ˜ λ°©λ²•μœΌλ‘œ, ν΄λΌμ΄μ–ΈνŠΈ μΈ‘μ—μ„œ  λ§€ μ‹±κΈ€ νŽ˜μ΄μ§€ μš”μ²­λ§ˆλ‹€ APIλ‘œλΆ€ν„° 데이터λ₯Ό κ°€μ Έμ˜¨λ‹€. (νŽ˜μ΄μ§€κ°€ λ Œλ”λ˜κ³  λ‚œ ν›„, ν•¨μˆ˜κ°€ λ™μž‘ν•œλ‹€.)
  • SSR - Server-Side Rendering, μ„œλ²„ μΈ‘μ—μ„œ λ§€ νŽ˜μ΄μ§€ ν˜ΈμΆœλ§ˆλ‹€ νŠΉλ³„ν•œ ν•¨μˆ˜λ₯Ό λ™μž‘μ‹œμΌœ APIλ‘œλΆ€ν„° 데이터λ₯Ό κ°€μ Έμ˜¨λ‹€. (νŽ˜μ΄μ§€κ°€ λ‘œλ“œλ˜κΈ° 전에, κ·Έ νŠΉλ³„ν•œ ν•¨μˆ˜κ°€ λ¨Όμ € λ™μž‘ν•˜κ³  λ”œλ ˆμ΄λ₯Ό λ§Œλ“€κ³  κ·Έ 후에 νŽ˜μ΄μ§€λ₯Ό κ°€μ Έμ˜¨λ‹€.) 
  • SSG - Static Site Generation, νŽ˜μ΄μ§€κ°€ λΉŒλ“œλ  λ•Œ νŠΉλ³„ν•œ ν•¨μˆ˜λ₯Ό λ™μž‘μ‹œμΌœ 데이터λ₯Ό κ°€μ Έμ˜¨λ‹€. 
  • ISR - Incremental Static Regeneration, μƒˆλ‘œμš΄ 방법인데 짧게 μ„€λͺ…ν•˜μžλ©΄ SSG와 SSR의 κ²°ν•©μœΌλ‘œ μ •μ μœΌλ‘œ μ œκ³΅ν•˜λ˜ κ³³μ—μ„œ νŠΉμ • μ‹œκ°„κ³Ό νŠΉμ • 쑰건에 ν•΄λ‹Ή νŽ˜μ΄μ§€κ°€ μž¬λΉŒλ“œλ˜κ³  APIμ—μ„œ 데이터λ₯Ό λ‹€μ‹œ κ°€μ Έμ˜¨λ‹€. 

Don't worry if you didn't get that, because I will be explaining it thoroughly, just familiarize the words first.

μ² μ €νžˆ μ„€λͺ…을 ν•΄μ€„ν…Œλ‹ˆ 이해가 μ•ˆλœλ‹€ν•΄λ„ κ±±μ • 말라, λ¨Όμ € 단어에 μ΅μˆ™ν•΄μ§€λ„λ‘ ν•˜μž. 


I mentioned before that there is a special function that will run when using a specific data fetching method. Keep that in mind as I will show you what is that special function.

μ•žμ„œ μ–ΈκΈ‰ν–ˆλ˜ κ²ƒμ²˜λŸΌ νŠΉμ • 데이터 κ°€μ Έμ˜€κΈ° λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•  λ•Œ λ™μž‘ν•˜λŠ” νŠΉλ³„ν•œ ν•¨μˆ˜κ°€ μžˆλ‹€. κ·Έ νŠΉλ³„ν•œ ν•¨μˆ˜κ°€ 무엇인지 λ³΄μ—¬μ€„ν…Œλ‹ˆ μ°Έκ³ ν•˜λΌ. 

This code example will fetch a date-time from an API using axios, then render it on the page. It is useful to see the date-time so we can truly know when the API is hit.

μ—¬κΈ°μ˜ μ½”λ“œ μ˜ˆμ‹œλŠ” axiosλ₯Ό μ‚¬μš©ν•΄μ„œ APIλ‘œλΆ€ν„° λ‚ μ§œμ™€ μ‹œκ°„μ„ κ°€μ Έμ˜€κ³  νŽ˜μ΄μ§€μ— λ Œλ”ν•œλ‹€. λ‚ μ§œμ™€ μ‹œκ°„μ„ λ³΄λŠ” 것이 μœ μš©ν•œ 것은 API에 μ–Έμ œ μ ‘κ·Όλ˜λŠ”μ§€ μ•Œ 수 있기 λ•Œλ¬Έμ΄λ‹€. 

 

Client-Side Rendering (CSR)

Special Function: `useEffect`

Demo Site

Code Example

export default function CSRPage() {
  const [dateTime, setDateTime] = React.useState<string>();

  React.useEffect(() => {
    axios
      .get('https://worldtimeapi.org/api/ip')
      .then((res) => {
        setDateTime(res.data.datetime);
      })
      .catch((error) => console.error(error));
  }, []);

  return (
    <main>
      <TimeSection dateTime={dateTime} />
    </main>
  );
}

Demo

https://theodorusclarence.com/blog/nextjs-fetch-method#client-side-rendering-csr

Terms:

  • PT → Preview Time, the time shown when the API is hit. Can be seen in the middle.
  • RT → Real-Time, the real ticking time updating every second, can be seen on the right bottom corner
  • PT → 미리보기 μ‹œκ°„, API에 μ ‘κ·Όν–ˆμ„ λ•Œμ˜ μ‹œκ°„. 쀑간에 보여짐. 
  • RT → μ‹€μ œ μ‹œκ°„, 맀초 μ—…λ°μ΄νŠΈλ˜λŠ” μ‹€μ œ μ‹œκ°„μœΌλ‘œ 우츑 ν•˜λ‹¨ μ½”λ„ˆμ— 보여짐.

Video Description:

  1. Page reloads on 15:46:03 Real-Time (RT), then a LOADING indicator is shown
  2. After about 1s, Preview Time is showing 15:46:04(PT)

  3. 15:46:03 μ‹€μ œ μ‹œκ°„(RT)에 νŽ˜μ΄μ§€κ°€ μž¬λ‘œλ“œλ˜κ³  LOADING ν‘œμ‹œκ°€ 보여짐
  4. 1초 μ―€ 후에 미리보기 μ‹œκ°„ 15:46:04(PT)κ°€ 보여짐

Keys to Emphasize

  1. useEffect function, this function is the key indicator that a page is using Client-Side Rendering.
  2. LOADING indicator, because the data fetching runs after the page is rendered, the data is not fetched instantly, therefore showing a loading state.
  3. Data is fetched on every page request, which is why the time shown is different for each reloads.

  4. useEffect ν•¨μˆ˜, 이 ν•¨μˆ˜λŠ” νŽ˜μ΄μ§€κ°€ Client-Side Rending을 μ‚¬μš©ν•˜κ³  μžˆλ‹€λŠ” 핡심 μš”μ†Œμ΄λ‹€.
  5. LOADING ν‘œμ‹œ, 데이터 κ°€μ Έμ˜€κΈ°κ°€ νŽ˜μ΄μ§€κ°€ λ Œλ”λœ ν›„ μ‹€ν–‰λ˜κΈ° λ•Œλ¬Έμ—, λ°μ΄ν„°λŠ” μ¦‰μ‹œ νŒ¨μΉ­λ˜λŠ” 것이 μ•„λ‹ˆκ³  λ”°λΌμ„œ λ‘œλ”© μƒνƒœκ°€ 보여진닀. 
  6. λ°μ΄ν„°λŠ” λ§€ νŽ˜μ΄μ§€ μš”μ²­λ§ˆλ‹€ νŒ¨μΉ˜λœλ‹€, κ·Έλ ‡κΈ° λ•Œλ¬Έμ— 각 μž¬λ‘œλ“œ λ§ˆλ‹€ λ‹€λ₯Έ μ‹œκ°„μ΄ 보여진닀. 

https://theodorusclarence.com/blog/nextjs-fetch-method#client-side-rendering-csr


Server Side Rendering (SSR)

Special Function: `getServerSideProps`

Demo Site

Code Example

export default function SSRPage({ dateTime }: SSRPageProps) {
  return (
    <main>
      <TimeSection dateTime={dateTime} />
    </main>
  );
}

export const getServerSideProps: GetServerSideProps = async () => {
  const res = await axios.get('https://worldtimeapi.org/api/ip');

  return {
    props: { dateTime: res.data.datetime },
  };
};

Demo

https://theodorusclarence.com/blog/nextjs-fetch-method#server-side-rendering-ssr

 

Video Description:

  1. Clicked the link on 16:32:38(RT), a slight pause for 2s, then page loads showing 16:02:40(PT)
  1. 16:02:38(RT)*에 링크λ₯Ό ν΄λ¦­ν–ˆκ³ , μ•½ 2초 μ •λ„μ˜ 멈좀 후에, 16:02:40(PT) λ³΄μ΄λŠ” νŽ˜μ΄μ§€κ°€ λ‘œλ“œλœλ‹€. 
    (* 원문에 16:32:38이 μ˜€νƒ€μΈ 것 κ°™μ•„μ„œ λ²ˆμ—­μ‹œ 16:02:38둜 μˆ˜μ •ν•¨.)

Keys to Emphasize

  1. getServerSideProps function, this function is the key indicator that a page is using Server-Side Rendering.
  2. DELAY before render, and no LOADING indicator, the data is fetched before the page is rendered, so there will be a slight delay where the API is being hit at the moment, then it will show the page without loading indicator
  3. Data is fetched on every page request, which is why the time shown is different for each reloads.

  4. getServerSideProps ν•¨μˆ˜, μ΄ ν•¨μˆ˜λŠ” νŽ˜μ΄μ§€κ°€ Server-Side Rending을 μ‚¬μš©ν•œλ‹€λŠ” 핡심 μš”μ†Œμ΄λ‹€. 
  5. λ Œλ” μ „ λ”œλ ˆμ΄, 그리고 LOADING ν‘œμ‹œκ°€ μ—†μŒ, λ°μ΄ν„°λŠ” νŽ˜μ΄μ§€κ°€ λ Œλ”λ˜κΈ° 전에 패치되고 κ·Έλ ‡κΈ° λ•Œλ¬Έμ— APIκ°€ μ ‘κ·Όν•˜λŠ” μˆœκ°„μ— μ•½κ°„ λ”œλ ˆμ΄κ°€ λ°œμƒν•œ 후에 λ‘œλ”© ν‘œμ‹œ 없이 νŽ˜μ΄μ§€μ— 보여진닀. 
  6. λ°μ΄ν„°λŠ” λ§€ 페이치 μš”μ²­λ§ˆλ‹€ νŒ¨μΉ˜λœλ‹€, 이것이 각 μž¬λ‘œλ“œ λ§ˆλ‹€ λ‹€λ₯Έ μ‹œκ°„μ΄ λ³΄μ—¬μ§€λŠ” μ΄μœ μ΄λ‹€. 

https://theodorusclarence.com/blog/nextjs-fetch-method#server-side-rendering-ssr


CSR vs SSR

Here is the difference between CSR vs SSR, keep an eye on delay and loading indicators.

이제 CSRκ³Ό SSR μ‚¬μ΄μ˜ 차이점에 λŒ€ν•΄ μ΄μ•ΌκΈ°ν•˜λ‹ˆ λ”œλ ˆμ΄μ™€ λ‘œλ”© ν‘œμ‹œμ— μ£Όμ˜ν•΄λ³΄μž. 

https://theodorusclarence.com/blog/nextjs-fetch-method#csr-vs-ssr

Video Description:

  1. When clicking CSR, with no delay a LOADING text is visible for a second, then the Preview Time loads.
  2. When clicking SSR, a slight delay happened, then the page loads.
  1. CSR을 ν΄λ¦­ν•˜λ©΄ λ”œλ ˆμ΄ 없이 LOADING ν…μŠ€νŠΈκ°€ 잠깐 보여지고 Preview μ‹œκ°„μ΄ λ‘œλ“œλœλ‹€. 
  2. SSR을 ν΄λ¦­ν•˜λ©΄ μ•½κ°„μ˜ λ”œλ ˆμ΄ 후에 νŽ˜μ΄μ§€κ°€ λ‘œλ“œλœλ‹€. 

Keys to Emphasize

  1. CSR hit the API after the page loads.
  2. SSR hit the API before the page loads.
  1. CSR은 νŽ˜μ΄μ§€ λ‘œλ“œ 후에 API에 μ ‘κ·Όν•œλ‹€. 
  2. SSR은 νŽ˜μ΄μ§€ λ‘œλ“œ 전에 API에 μ ‘κ·Όν•œλ‹€. 

https://theodorusclarence.com/blog/nextjs-fetch-method#csr-vs-ssr

Short addition

I will probably create a new post about the pros and cons of each method, but when using CSR the SEO is not really great because the data is only fetched after the page renders. This is useful and convenient when we are creating a page with a gated authentication, as you don't really need SEO for pages like the dashboard, edit profile page, etc.

μ•„λ§ˆλ„ 각 λ©”μ„œλ“œμ˜ μž₯단점에 λŒ€ν•΄ μƒˆλ‘œμš΄ 포슀트λ₯Ό μž‘μ„±ν•  것이긴 ν•˜μ§€λ§Œ CSR을 μ‚¬μš©ν•  λ•Œ SEOμ—λŠ” κ·Έλ‹€μ§€ μ’‹μ§€ μ•ŠλŠ”λ° μ™œλƒν•˜λ©΄ 데이터가 νŽ˜μ΄μ§€κ°€ λ Œλ”λœ ν›„μ—λ§Œ 가져와지기 λ•Œλ¬Έμ΄λ‹€. 이 방법은 λŒ€μ‹œλ³΄λ“œλ‚˜ ν”„λ‘œν•„ μˆ˜μ • νŽ˜μ΄μ§€μ™€ 같은 SEOκ°€ λ³„λ‘œ ν•„μš”κ°€ μ—†λŠ” 게이트 인증이 μžˆλŠ” νŽ˜μ΄μ§€λ₯Ό 생성할 λ•Œ μœ μš©ν•˜κ³  νŽΈλ¦¬ν•˜λ‹€. 

But, for the SSR, although it creates a delay, data that was fetched is injected and helps SEO. This is quite useful for a thread or post that we need to get traffic into, like Reddit or some sort.

ν•˜μ§€λ§Œ SSR에 λŒ€ν•΄μ„œλŠ” λ”œλ ˆμ΄κ°€ μžˆκΈ°λŠ” ν•˜μ§€λ§Œ κ°€μ Έμ˜¨ 데이터가 μ£Όμž…λ˜μ—ˆκ³  SEO에도 도움이 λœλ‹€. 이 방법은 Redditμ΄λ‚˜ 그와 λΉ„μŠ·ν•œ μ’…λ₯˜μ˜ νŠΈλž˜ν”½μ„ κ°€μ Έμ™€μ•Όν•˜λŠ” μŠ€λ ˆλ“œλ‚˜ ν¬μŠ€νŠΈμ— μœ μš©ν•˜λ‹€. 


Static Site Generation (SSG)

Special function: `getStaticProps`

Demo Site

Code Example

export default function SSGPage({ dateTime }: SSGPageProps) {
  return (
    <main>
      <TimeSection dateTime={dateTime} />
    </main>
  );
}

export const getStaticProps: GetStaticProps = async () => {
  const res = await axios.get('https://worldtimeapi.org/api/ip');

  return {
    props: { dateTime: res.data.datetime },
  };
};

Demo

https://theodorusclarence.com/blog/nextjs-fetch-method#static-site-generation-ssg

Video Description:

  1. Preview Time is showing 13:39:36(PT). But the real-time is 16:16:59(RT), about 3 hours late.
  2. Reloading and going back and forth to the home page did not change anything.
  1. 미리보기 μ‹œκ°„μ€ 13:39:36(PT)둜 보여쀀닀. ν•˜μ§€λ§Œ μ‹€μ œ μ‹œκ°„μ€ 16:16:59(RT)둜 3μ‹œκ°„ 정도 λŠ¦λ‹€. 
  2. μž¬λ‘œλ”©ν•˜κ³  μ•žλ’€λ‘œ ν™ˆ ν™”λ©΄μœΌλ‘œ κ°”λ‹€ λŒμ•„μ™”λ‹€ 해봐도 νŽ˜μ΄μ§€λŠ” 아무것도 λ°”λ€Œμ§€ μ•ŠλŠ”λ‹€. 

Keys to Emphasize

  1. getStaticProps function, this function is the key indicator that a page is using Static Site Generation.
  2. Fetched when running yarn build, the API will be hit ONLY when the application is building. This is why the time is at 13:39, while the real-time is 16:17.
  3. Data will not change because no further fetch, which is why the time shown is the same for each reloads.
  1. getStaticProps ν•¨μˆ˜, 이 ν•¨μˆ˜λŠ” νŽ˜μ΄μ§€κ°€ Static Site Generation을 μ‚¬μš©ν•˜κ³  μžˆλ‹€λŠ” 핡심 μš”μ†Œμ΄λ‹€.
  2. yarn buildκ°€ 싀행될 λ•Œ 데이터λ₯Ό κ°€μ Έμ˜΄, μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ λΉŒλ“œλ  λ•Œμ—λ§Œ API에 μ ‘κ·Όν•œλ‹€. 이것이 μ‹€μ œ μ‹œκ°„μ€ 16:17인데도 13:39둜 λ³΄μ—¬μ§€λŠ” μ΄μœ μ΄λ‹€. 
  3. μΆ”κ°€ νŒ¨μΉ˜κ°€ μ—†κΈ° λ•Œλ¬Έμ— λ°μ΄ν„°λŠ” λ°”λ€Œμ§€ μ•ŠλŠ”λ‹€, κ·Έλ ‡κΈ° λ•Œλ¬Έμ— 맀번 μž¬λ‘œλ“œν•  λ•Œλ§ˆλ‹€ λ™μΌν•œ μ‹œκ°„μ„ 보여쀀닀. 

https://theodorusclarence.com/blog/nextjs-fetch-method#static-site-generation-ssg


Incremental Static Regeneration

Special function: `getStaticProps` + `revalidate`

Demo Site

Code Example

export default function ISR20Page({ dateTime }: ISR20PageProps) {
  return (
    <main>
      <TimeSection dateTime={dateTime} />
    </main>
  );
}

export const getStaticProps: GetStaticProps = async () => {
  const res = await axios.get('https://worldtimeapi.org/api/ip');

  return {
    props: { dateTime: res.data.datetime },
    revalidate: 20,
  };
};

Demo

https://theodorusclarence.com/blog/nextjs-fetch-method#incremental-static-regeneration

Disclaimer: Revalidate time is set to 20 seconds.

주의: κ°±μ‹  μ‹œκ°„μ€ 20초둜 μ„€μ •λ˜μ–΄ 있음.

Video Description:

  1. At first, it was 16:40:12(PT), and real-time when reloading is 16:40:25(RT) and 16:40:29(RT). In those 2 reload, Preview Time (PT) did not change.
  2. Then, when 16:40:32(RT) (20s after initial), reload is done twice, the first time on 16:40:36(RT) and 16:40:40(RT). The Preview Time change to 16:40:37(PT) after the second reload.
  1. 맨 처음 16:40:12(PT)μ˜€λ‹€. 그리고 μž¬λ‘œλ”©λ  λ•Œ μ‹€μ œ μ‹œκ°„μ€ 16:40:25(RT) 그리고 16:40:29(RT)이닀. 이 2번의 μž¬λ‘œλ“œμ—μ„œ, 미리보기 μ‹œκ°„ (PT)λŠ” λ³€ν•˜μ§€ μ•ŠλŠ”λ‹€.
  2. 그리고, 16:40:32(PT)일 λ•Œ (μ²˜μŒμœΌλ‘œλΆ€ν„° 20초 ν›„), μž¬λ‘œλ“œλ₯Ό 2번 ν•˜μ˜€λ‹€. 첫 λ²ˆμ§ΈλŠ” 16:40:36(RT)에 그리고 16:40:40(RT)μ΄μ—ˆλ‹€. 미리보기 μ‹œκ°„μ€ 16:40:37(PT)둜 λ‘λ²ˆμ§Έ μž¬λ‘œλ“œ 후에 λ³€κ²½λ˜μ—ˆλ‹€. 

Keys to Emphasize

Now, this is might be confusing for you, but here is the key I want you to look at.

μ’€ ν—·κ°ˆλ¦΄ μˆ˜λ„ μžˆμ§€λ§Œ 여기에 λ…μžλ“€μ΄ λ³΄μ•˜μœΌλ©΄ ν•˜λŠ” 핡심이 μžˆλ‹€. 

  1. When in a 20-second cooldown span–16:40:12(RT) - 16:40:32(RT), reloading doesn't trigger changes. This is because the page is in a cooldown state, as we set on the revalidate key.
  2. After the 20-second cooldown–16:40:32(RT), we did 2 reloads.
    1. First Reload at 16:40:36(RT), we know that it is not on the cooldown state anymore. The first visit after the cooldown state is off, is going to trigger page rebuild. Page rebuild meaning, only this certain page is going to be rebuild. Not the whole application. The fetch API will run in the background, but there will be no changes on the Preview Time
    2. Second Full Reload at 16:40:40(RT), the Preview Time change to 16:40:37(PT). Exactly a second after the page rebuild (which means the rebuild takes about 1s). This second reload is going to serve that rebuilt page from the previous reload.
  1. 20초의 μΏ¨λ‹€μš΄ κΈ°κ°„ λ•Œμ—16:40:12(RT) - 16:40:32(RT), μž¬λ‘œλ“œλŠ” 변경사항이 νŠΈλ¦¬κ±°λ˜μ§€ μ•ŠλŠ”λ‹€. 이것은 μš°λ¦¬κ°€ revalidate ν‚€λ₯Ό μ„€μ •ν•΄λ†“μ€λŒ€λ‘œ ν•΄λ‹Ή νŽ˜μ΄μ§€κ°€ μΏ¨λ‹€μš΄ μƒνƒœμ— 있기 λ•Œλ¬Έμ΄λ‹€.
  2. 20초의 μΏ¨λ‹€μš΄ 후에16:40:32(RT), 2번의 μž¬λ‘œλ“œλ₯Ό ν–ˆλ‹€.
    1. 첫번째 μž¬λ‘œλ“œλŠ” 16:40:36(RT) μ΄μ—ˆλ‹€. 이 μ‹œμ μ€ 더 이상 μΏ¨λ‹€μš΄ μƒνƒœμ— μžˆμ§€ μ•Šλ‹€λŠ” 것을 μš°λ¦¬λŠ” μ•ˆλ‹€. μΏ¨λ‹€μš΄ μƒνƒœκ°€ μ’…λ£Œλ˜κ³  λ‚œ ν›„μ˜ 첫번째 방문은 νŽ˜μ΄μ§€ μž¬λΉŒλ“œλ₯Ό 트리거 ν•œλ‹€. νŽ˜μ΄μ§€ μž¬λΉŒλ“œμ˜ μ˜λ―ΈλŠ” ν•΄λ‹Ήλ˜λŠ” 이 νŠΉμ • νŽ˜μ΄μ§€λ§Œ μž¬λΉŒλ“œ λœλ‹€λŠ” 것이닀. 전체 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ μž¬λΉŒλ“œ λ˜λŠ” 것이 μ•„λ‹ˆλ‹€. 패치 APIλŠ” λ°±κ·ΈλΌμš΄λ“œμ—μ„œ λ™μž‘ν•˜μ§€λ§Œ 미리보기 μ‹œκ°„μ—λŠ” λ³€ν™”κ°€ 없을 것이닀. 
    2. λ‘λ²ˆμ§Έ μƒˆλ‘œκ³ μΉ¨(*μ•„λž˜μ˜ μš©μ–΄ μ„€λͺ…이 λ‚˜μ˜΄)λŠ” 16:40:40(RT)이닀. 미리보기 μ‹œκ°„μ€ 16:40:37(PT)둜 λ°”λ€Œμ—ˆλ‹€. νŽ˜μ΄μ§€ μž¬λΉŒλ“œλ˜κ³  정말 잠깐 후에 말이닀. (이것은 μž¬λΉŒλ“œν•˜λŠ”λ° μ•½ 1초 정도 κ±Έλ¦°λ‹€λŠ” λœ»μ΄λ‹€.) 이 λ‘λ²ˆμ§Έ μž¬λ‘œλ“œλŠ” 이전 μž¬λ‘œλ“œλ‘œλΆ€ν„° μž¬λΉŒλ“œλœ νŽ˜μ΄μ§€λ₯Ό 보여쀄 것이닀. 
https://theodorusclarence.com/blog/nextjs-fetch-method#incremental-static-regeneration

https://theodorusclarence.com/blog/nextjs-fetch-method#incremental-static-regeneration

Revisiting Page vs Full Reload

https://theodorusclarence.com/blog/nextjs-fetch-method#revisiting-page-vs-full-reload
 
 

Terms:

  1. Revisiting pages → navigating using next/link (going back to home, then to that page again)
  2. Full reload → doing reload at a website (command+r)
  1. νŽ˜μ΄μ§€ 재방문 → next/linkλ₯Ό μ‚¬μš©ν•˜μ—¬ μ΄λ™ν•˜λŠ” 것 (ν™ˆμœΌλ‘œ λŒμ•„κ°”λ‹€κ°€ ν•΄λ‹Ή νŽ˜μ΄μ§€λ‘œ λ‹€μ‹œ μ˜€λŠ” 것)
  2. μƒˆλ‘œκ³ μΉ¨ → μ›Ήμ‚¬μ΄νŠΈμ—μ„œ μƒˆλ‘œκ³ μΉ¨/μž¬λ‘œλ“œν•˜λŠ” 것 (command+r)

Video Description:

  1. Revisiting pages at the first time 18:38:45(RT), will trigger rebuild, but after the second revisit, the Preview Time did not change.
  2. After a full reload, then Preview Time is changed to 18:38:45(PT)
  1. 18:38:45(RT)에 처음으둜 νŽ˜μ΄μ§€λ₯Ό 재방문 ν•˜λŠ” 것은, μž¬λΉŒλ“œλ₯Ό νŠΈλ¦¬κ±°ν•  κ²ƒμ΄μ§€λ§Œ, 두 번째 재방문 후에 미리보기 μ‹œκ°„μ€ λ³€ν•˜μ§€ μ•Šμ•˜λ‹€
  2. μƒˆλ‘œκ³ μΉ¨ν›„μ—λŠ”, λ―Έλ¦¬λ³΄κΈ° μ‹œκ°„μ΄ 18:38:45(PT)둜 λ°”λ€Œμ—ˆλ‹€.

Note:

  1. The first reload does not have to be a full reload, we can go back to the home page, then to that page again (revisit), it will trigger the rebuild as long as we are not in the cooldown state.
  2. But, the second reload must be a full reload. Going back to the home page, then to the page again won't change the new Preview Time.
  1. 첫번째 μž¬λ‘œλ“œλŠ” ν’€ μž¬λ‘œλ“œμΌ ν•„μš”λŠ” μ—†λ‹€. ν™ˆ ν™”λ©΄μœΌλ‘œ λŒμ•„κ°”λ‹€κ°€ ν•΄λ‹Ή νŽ˜μ΄μ§€λ‘œ λ‹€μ‹œ λŒμ•„μ™€λ„ λœλ‹€(재방문). μ΄λ ‡κ²Œ ν•˜λŠ” 것은 μΏ¨λ‹€μš΄ μƒνƒœκ°€ μ•„λ‹Œ ν•œ μž¬λΉŒλ“œλ₯Ό νŠΈλ¦¬κ±°ν•  것이닀. 
  2. ν•˜μ§€λ§Œ, λ‘λ²ˆμ§Έ μž¬λ‘œλ“œλŠ” λ°˜λ“œμ‹œ μƒˆλ‘œκ³ μΉ¨μ΄μ–΄μ•Όλ§Œ ν•œλ‹€. ν™ˆ ν™”λ©΄μœΌλ‘œ λŒμ•„κ°”λ‹€κ°€ ν•΄λ‹Ή νŽ˜μ΄μ§€λ‘œ λ‹€μ‹œ λŒμ•„μ˜€λŠ” 것은 μƒˆλ‘œμš΄ 미리보기 μ‹œκ°„μœΌλ‘œ λ°”κΎΈμ§€ μ•Šμ„ 것이닀. 

Now, this is a case where we are assuming that only 1 person is accessing the website. But, that reloads will happen every person visit while still respecting the cooldown state.

이것은 μš°λ¦¬κ°€ 단 ν•œ λͺ…λ§Œμ΄ 이 μ›Ήμ‚¬μ΄νŠΈμ— μ ‘κ·Όν•œλ‹€κ³  κ°€μ •ν–ˆμ„ λ•Œμ΄λ‹€. 그런데 이 μž¬λ‘œλ“œλŠ” μΏ¨λ‹€μš΄ μƒνƒœλ₯Ό μœ μ§€ν•˜λ©΄μ„œ λ°©λ¬Έν•˜λŠ” λͺ¨λ“  μ‚¬λžŒμ—κ²Œ λ°œμƒν•œλ‹€. 

Is it going to be rebuilt every 20s then? 

그럼 λ§€ 20μ΄ˆλ§ˆλ‹€ μž¬λΉŒλ“œκ°€ μ΄λ£¨μ–΄μ§€λŠ”κ°€?

Nope.

κ·Έλ ‡μ§€ μ•Šλ‹€.

When the cooldown is off, if no one visits the page, then that page will not rebuild, even after long past the 20s.

μΏ¨λ‹€μš΄μ΄ μ’…λ£Œλ˜κ³  아무도 νŽ˜μ΄μ§€λ₯Ό λ°©λ¬Έν•˜μ§€ μ•ŠμœΌλ©΄ νŽ˜μ΄μ§€λŠ” 20μ΄ˆκ°€ μ§€λ‚œ 후에도 μž¬λΉŒλ“œ λ˜μ§€ μ•Šμ„ 것이닀. 

But, the first person that visits when the cooldown state is off, is going to trigger a rebuild. That person won't be seeing changes. But, the changes will be served for the next full reload.

ν•˜μ§€λ§Œ 첫 번째 μ‚¬λžŒμ΄ μΏ¨λ‹€μš΄ μƒνƒœκ°€ μ’…λ£Œλ˜μ—ˆμ„ λ•Œ λ°©λ¬Έν•˜λ©΄ μž¬λΉŒλ“œλŠ” 트리거 될 것이닀. κ·Έ μ‚¬λžŒμ€ 바뀐 λ‚΄μš©μ„ 보지 λͺ»ν•  것이닀. ν•˜μ§€λ§Œ ν•΄λ‹Ή λ³€ν™”λŠ” λ‹€μŒ μƒˆλ‘œκ³ μΉ¨ λ•Œμ— λ³΄μ—¬μ§ˆ 것이닀. 

https://theodorusclarence.com/blog/nextjs-fetch-method#revisiting-page-vs-full-reload

Conclusion

That's all, folks!

If you have understood this material, I suggest you to read more about How to choose between them. I provide 4 metrics for you to consider and some example!

이 글을 μ΄ν•΄ν–ˆλ‹€λ©΄, How to choose between them에 λŒ€ν•΄ 더 읽어보기 λ°”λž€λ‹€. 당신이 κ³ λ €ν•΄μ•Όν•  4κ°€μ§€ μΈ‘μ • ν•­λͺ©κ³Ό λͺ‡ κ°€μ§€ μ˜ˆμ œλ„ μ œκ³΅ν•œλ‹€. 


(글이 μœ μ΅ν•˜μ…¨μœΌλ©΄ 아티클 μ €μžμ˜ λΈ”λ‘œκ·Έμ—μ„œ μ’‹μ•„μš”λ„ λˆŒλŸ¬μ£Όμ„Έμš”πŸ˜ƒπŸ‘)

λ‹€μŒ 아티클 How to choose between them ν™•μΈν•˜λŸ¬κ°€κΈ°πŸ‘‡

 

[λ‚΄λ³΄λ‚΄λ²ˆ] How to choose between Next.js CSR, SSR, SSG, and ISR

λ‚΄λ³΄λ‚΄λ²ˆ( λ‚΄κ°€ 보렀고 λ‚΄κ°€ λ²ˆμ—­ν•œ... ) 아티클 β€» μ˜μ–΄ μ „κ³΅μžλ„ ν•΄μ™Έ μœ ν•™νŒŒλ„ μ•„λ‹ˆκΈ°μ— λ²ˆμ—­μ—λŠ” μ˜μ—­, μ˜€μ—­, κ΅¬κΈ€ λ²ˆμ—­μ΄ λ¬΄μˆ˜νžˆ λ§Žμ„ 수 있으며, 개인적인 곡뢀 λͺ©μ μœΌλ‘œ λ²ˆμ—­ν•˜λ‹€ λ³΄λ‹ˆ 였

birdmee.tistory.com