next.js app router์˜ caching์— ๊ด€ํ•ด ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

Overview

next.js ์—์„œ ์ œ๊ณตํ•˜๋Š” caching mechanism์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ๋‹ค.

MechanismWhatWherePurposeDuration
Request MemoizationReturn values of functionsServerRe-use data in a React Component treePer-request lifecycle
Data CacheDataServerStore data across user requests and deploymentsPersistent (can be revalidated)
Full Route CacheHTML and RSC payloadServerReduce rendering cost and improve performancePersistent (can be revalidated)
Router CacheRSC PayloadClientReduce server requests on navigationUser session or time-based

next.js๋Š” ๊ฐ€๋Šฅํ•œ ๋งŽ์€ ์บ์‹ฑ์„ ์ ์šฉํ•˜๋ ค๊ณ  ํ•˜๊ณ , ๋”ฐ๋ผ์„œ ๋”ฐ๋กœ opt-outํ•˜์ง€ ์•Š๋Š”ํ•œ ์ •์ ์œผ๋กœ ๋ Œ๋”๋ง๋˜๊ณ , ์š”์ฒญ์€ ์บ์‹ฑ๋œ๋‹ค. ๊ฐ mechanism์€ ๋‹ค์Œ ๋„์‹์ฒ˜๋Ÿผ ๋™์ž‘ํ•œ๋‹ค.

Request Memoization

React์—์„œ ํ™•์žฅํ•œ fetch API๊ฐ€ ์ž๋™์œผ๋กœ ๊ฐ™์€ URL๊ณผ ์˜ต์…˜์œผ๋กœ ๋ณด๋‚ด์ง„ ์š”์ฒญ์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ์–ตํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํ•œ route๋ฅผ ๋ Œ๋”๋งํ• ๋•Œ ๊ฐ™์€ ์š”์ฒญ์ด ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐœ์ƒํ•˜๋ฉด, ๋”ฑ ํ•œ๋ฒˆ๋งŒ ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ์‚ฌ์šฉํ•œ๋‹ค.

  • Duration : React Component tree์˜ ๋ Œ๋”๋ง์ด ์ข…๋ฃŒ๋ ๋•Œ๊นŒ์ง€๋งŒ ์œ ์ง€. ๋ Œ๋”๋ง ์ข…๋ฃŒ์‹œ ์ดˆ๊ธฐํ™”๋จ.
  • Revalidating : server requests ์‚ฌ์ด์—์„œ ๊ณต์œ ๋˜์ง€์•Š์œผ๋ฏ€๋กœ revalidatingํ•  ์ด์œ ๊ฐ€ ์—†์Œ
  • Opting-out : fetch API์— AbortController signal์„ ์ค„ ์ˆ˜ ์žˆ์Œ. ์˜ˆ์‹œ
  • ๊ธฐํƒ€
    • next.js๊ฐ€ ์•„๋‹Œ React ๊ธฐ๋Šฅ์ž„
    • fetch ํ•จ์ˆ˜์˜ GET method์—์„œ๋งŒ ๋™์ž‘ํ•จ
    • React Component tree์—์„œ๋งŒ ๋™์ž‘ํ•˜๋ฏ€๋กœ, route handler์—์„œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š์Œ.
    • fetch๋ฅผ ์“ธ ์ˆ˜ ์—†๋Š” ํ™˜๊ฒฝ์—์„œ๋Š” React cache function์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.

Data Cache

์—ฌ๋Ÿฌ server requests์™€ deployments ์‚ฌ์ด์—์„œ ๊ณต์œ ๋˜๋Š” ์บ์‹œ๋กœ, next.js์—์„œ ํ™•์žฅํ•œ fetch API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ ์šฉ๋œ๋‹ค. fetch API์˜ cache, next.revalidate ์˜ต์…˜์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ž์„ธํ•œ ๋‚ด์šฉ์€ fetch API references์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

  • Duration : ๋”ฐ๋กœ revalidateํ•˜๊ฑฐ๋‚˜ opt-outํ•˜์ง€ ์•Š๋Š”ํ•œ ๊ณ„์† ์œ ์ง€๋œ๋‹ค.
  • Revalidating
    • Time-based Revalidation : ํŠน์ •ํ•œ ์‹œ๊ฐ„๋งˆ๋‹ค revalidate ํ•  ์ˆ˜ ์žˆ์Œ.
      • ๋™์ž‘๋ฐฉ์‹ : { next: { revalidate: 60 } } ์ผ ๋•Œ,
        • ์ตœ์ดˆ ์š”์ฒญ ์‹œ : data source๋กœ API ์š”์ฒญ ํ›„, ๊ฒฐ๊ณผ๋ฅผ ์บ์‹ฑํ•จ.
        • 60์ดˆ ์ด์ „ ์žฌ์š”์ฒญ : ์บ์‹œ๋œ ๋ฐ์ดํ„ฐ ๋ฆฌํ„ด
        • 60์ดˆ ์ดํ›„ ์žฌ์š”์ฒญ : ์บ์‹œ๋œ ๋ฐ์ดํ„ฐ(stale ์ƒํƒœ) ๋ฆฌํ„ด, ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ fetch ํ›„ ๊ฒฐ๊ณผ ์บ์‹ฑ. ์š”์ฒญ์ด ์‹คํŒจํ•  ๊ฒฝ์šฐ ์ด์ „ ๋ฐ์ดํ„ฐ(stale)๋ฅผ ๊ณ„์† ์œ ์ง€ํ•จ.
    • On-demand Revalidation : ์›ํ• ๋•Œ revalidate ํ•  ์ˆ˜ ์žˆ์Œ.
      • ๋™์ž‘๋ฐฉ์‹
        • ์ตœ์ดˆ ์š”์ฒญ ์‹œ : data source๋กœ API ์š”์ฒญ ํ›„, ๊ฒฐ๊ณผ๋ฅผ ์บ์‹ฑํ•จ.
        • on-demand revalidation ์‹œ : ์บ์‹œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ PURGE ์‹œํ‚ด
        • ์žฌ์š”์ฒญ ์‹œ : data source๋กœ API ์š”์ฒญ ํ›„, ๊ฒฐ๊ณผ๋ฅผ ์บ์‹ฑํ•จ.
  • Oping-out
    • fetch API ์˜ต์…˜ : { cache: 'no-store' }์™€ ๊ฐ™์ด fetch API ์˜ต์…˜์œผ๋กœ Data Cache๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Œ.
    • Route Segment Config options : ํŠน์ • route segment๋ฅผ ๊ธฐ์ค€์œผ๋กœ opt-out ํ•  ์ˆ˜ ์žˆ์Œ. ํ•ด๋‹น route segment์— ํฌํ•จ๋œ ๋ชจ๋“  data request์— ์ ์šฉ๋œ๋‹ค.
      • export const dynamic = 'force-dynamic'
      • export const revalidate = 0
  • ๊ธฐํƒ€
    • { cache : 'no-store'}์™€ ๊ฐ™์ด ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋„๋ก ์„ค์ •ํ•ด๋„ Request Memoization์€ ์ผ์–ด๋‚œ๋‹ค. (๋ฆฌ์•กํŠธ ๋™์ž‘์ด๊ธฐ์—)

Full Route Cache

next.js๋Š” ๋นŒ๋“œ๋Š” ๊ฐ route๋ฅผ ๋ Œ๋”๋งํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์บ์‹ฑํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋งค ์š”์ฒญ๋งˆ๋‹ค ๋ Œ๋”๋งํ•˜์ง€ ์•Š๊ณ  ์บ์‹œ๋œ route๋ฅผ ์‘๋‹ตํ•˜์—ฌ ํŽ˜์ด์ง€ ๋กœ๋”ฉ์„ ๋น ๋ฅด๊ฒŒ ๋งˆ์น  ์ˆ˜ ์žˆ๊ฒŒํ•œ๋‹ค.

๋™์ž‘๋ฐฉ์‹

  1. React Rendering on the Server
    • next.js๋Š” React API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ Œ๋”๋ง์„ ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ด์…˜ํ•œ๋‹ค. ์ด๋•Œ ๊ฐ route segments์™€ Suspense boundaries๋ฅผ chunks๋กœ ๋‚˜๋ˆˆ๋‹ค.
    • ๊ฐ chunk๋Š” ๋‘ ๋‹จ๊ณ„๋กœ ๋ Œ๋”๋ง๋œ๋‹ค.
      1. React๊ฐ€ Server Component๋ฅผ React Server Component Payload๋ผ๋Š” ์ŠคํŠธ๋ฆฌ๋ฐ์— ์ตœ์ ํ™”๋œ ํŠน์ˆ˜ํ•œ ๋ฐ์ดํ„ฐ ํฌ๋งท์œผ๋กœ ๋ Œ๋”ํ•œ๋‹ค.
      2. next.js๋Š” React Server Component Payload์™€ Client Component JS instruction์„ ์‚ฌ์šฉํ•˜์—ฌ HTML์„ ์„œ๋ฒ„์—์„œ ๋ Œ๋”๋งํ•œ๋‹ค.
    • ์ด๋กœ์จ ์ž‘์—…์„ ์บ์‹ฑํ•˜๊ฑฐ๋‚˜ ์‘๋‹ต์„ ๋ณด๋‚ด๊ธฐ ์ „์— ๋ชจ๋“  ๊ฒƒ์ด ๋ Œ๋”๋ง๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ , ์ž‘์—… ์™„๋ฃŒ ์‹œ ์‘๋‹ต์„ ์ŠคํŠธ๋ฆฌ๋ฐ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์ฐธ๊ณ ) React Server Component Payload
      • ๋ Œ๋”๋œ React Server Components tree์˜ ์••์ถ• ์ด์ง„ ํ‘œํ˜„. ํด๋ผ์ด์–ธํŠธ์—์„œ browserโ€™s DOM์„ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.
      • ์•„๋ž˜ ๊ฒƒ๋“ค์„ ํฌํ•จ
        • Server Components์˜ ๋ Œ๋”๋ง ๊ฒฐ๊ณผ
        • Client Component๊ฐ€ ๋ Œ๋”๋˜๊ณ  ์ฐธ์กฐ๋  ์œ„์น˜์— ๋Œ€ํ•œ placeholder
        • Server Component์—์„œ Client Component๋กœ ๋‚ด๋ ค์ค„ props
  2. Next.js Caching on the Server (Full Route Server)
    • ๊ธฐ๋ณธ์ ์œผ๋กœ next.js๋Š” ๋ Œ๋”๋ง ๊ฒฐ๊ณผ (React Server Component Payload, HTML)์„ ์บ์‹ฑํ•œ๋‹ค. ๋นŒ๋“œํƒ€์ž„ ๋˜๋Š” revalidation ์‹œ ์ƒ์„ฑ๋˜๋Š” ๊ฒฐ๊ณผ ์บ์‹ฑ
  3. React Hydration and Reconciliation on the Client : ์š”์ฒญ ์‹œ ํด๋ผ์ด์–ธํŠธ์—์„œ์˜ ๋™์ž‘
    1. HTML ๋ Œ๋”๋ง. ์ƒํ˜ธ์ž‘์šฉ ๋ถˆ๊ฐ€๋Šฅ
    2. React Server Component Payload๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Client์™€ ๋ Œ๋”๋œ Server Component tree๋ฅผ ์กฐ์ •ํ•˜๊ณ  DOM์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
    3. JS instruction์œผ๋กœ Client Components๋ฅผ hydrationํ•˜๊ณ  ์ƒํ˜ธ์ž‘์šฉ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค.
  4. Next.js Caching on the Client (Router Cache)
    • React Server Component Payload๋Š” ํด๋ผ์ด์–ธํŠธ์— Router Cache๋กœ์„œ ์ €์žฅ๋œ๋‹ค.
    • Router Cache๋Š” navigation ์‹œ ์ด์ „ ๋ฐฉ๋ฌธํ–ˆ๋˜ ํŽ˜์ด์ง€๋ฅผ ๋น ๋ฅด๊ฒŒ ๋ Œ๋”๋งํ•˜๊ฑฐ๋‚˜, ๋‹ค์Œ route๋ฅผ prefetchingํ•œ๋‹ค.
  5. Subsequent Navigations
    • prefetching์ด๋‚˜ subsequent navigation ๋„์ค‘ next.js๋Š” React Server Component Payload๊ฐ€ Router Cache์— ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ณ , ์žˆ์œผ๋ฉด ์ƒˆ๋กœ์šด ์š”์ฒญ์„ server๋กœ ๋ณด๋‚ด์ง€ ์•Š๋Š”๋‹ค.

Static and Dynamic Rendering

route๊ฐ€ ๋นŒ๋“œํƒ€์ž„์— ์บ์‹œ ๋˜๋Š”์ง€ ์•ˆ๋˜๋Š”์ง€๋Š” ๊ทธ route๊ฐ€ ์ •์ ์ธ์ง€ ๋™์ ์œผ๋กœ ๋ Œ๋”๋ง๋˜๋Š”์ง€์— ๋‹ฌ๋ ค์žˆ๋‹ค. Static route๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์บ์‹œ๋˜๋ฉฐ, dynamic route๋Š” ์š”์ฒญ์‹œ ๋ Œ๋”๋ง๋˜๊ณ  ์บ์‹œ๋˜์ง€ ์•Š๋Š”๋‹ค.

๊ธฐํƒ€

  • Duration : ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜๊ตฌ์ ์ด๋ฉฐ ์—ฌ๋Ÿฌ request์—์„œ ์žฌ์‚ฌ์šฉ๋œ๋‹ค.
  • Invalidations
    • Revalidating Data : Data Cache๋ฅผ revalidatingํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์„œ๋ฒ„์—์„œ ๋ฆฌ๋ Œ๋”๋งํ•˜๊ณ , ์ƒˆ๋กœ์šด ๋ Œ๋”๋ง ๊ฒฐ๊ณผ๋ฅผ ์บ์‹ฑํ•˜์—ฌ Router Cache๋„ invalidation ๋œ๋‹ค.
    • Redeploying : Data cache์™€๋Š” ๋‹ฌ๋ฆฌ, Full Route Cache๋Š” deployments ์‚ฌ์ด์—์„œ ์œ ์ง€๋˜์ง€ ์•Š๋Š”๋‹ค.
  • Opting out
    • Dynamic Function ์‚ฌ์šฉ ์‹œ Full Route Cache๋Š” opt-out ๋˜๊ณ  ์š”์ฒญ์‹œ ๋ Œ๋”๋ง๋œ๋‹ค. Data Cache๋Š” ์œ ์ง€๋œ๋‹ค.
    • dynamic = 'force-dynamic', revalidate=0 route segment config ์˜ต์…˜ ์‚ฌ์šฉ
      • Full Route Cache, Data Cache ๋ชจ๋‘ opting-out ํ•œ๋‹ค.
      • Router Cache๋Š” ํด๋ผ์ด์–ธํŠธ ๋™์ž‘์ด๋ฏ€๋กœ ์œ ์ง€๋œ๋‹ค.
    • Opting out Data Cache : fetch API๋ฅผ ์บ์‹œํ•˜์ง€ ์•Š์œผ๋ฉด Full Route Cache๋„ opt-out ๋œ๋‹ค. ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ์š”์ฒญ์„ ๋ณด๋‚ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

Router Cache

next.js๊ฐ€ ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ์—์„œ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” in-memory ์บ์‹œ์ด๋‹ค. React Server Component Payload๋ฅผ ์ €์žฅํ•˜๋ฉฐ ๊ฐ route segments ๋ณ„๋กœ ๋ถ„๋ฆฌ๋˜์–ด์žˆ๋‹ค. ์œ ์ €๊ฐ€ routes ์‚ฌ์ด๋ฅผ ์ด๋™ํ•  ๋–„๋งˆ๋‹ค prefetch๋˜๊ฑฐ๋‚˜ ๋ฐฉ๋ฌธํ•œ route segment๋ฅผ ์ €์žฅํ•œ๋‹ค. browser์˜ bfcache์™€๋Š” ๋‹ค๋ฅธ ๊ฒƒ์ด์ง€๋งŒ ๋น„์Šทํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‚ธ๋‹ค.

  • Duration : ๋ธŒ๋ผ์šฐ์ € ์ž„์‹œ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜๋ฉฐ ๋‹ค์Œ ๋‘ ๊ฐ€์ง€ ์š”์ธ์— ์˜ํ•ด duration์ด ๊ฒฐ์ •๋œ๋‹ค./
    • Session : Navigation ์‚ฌ์ด์—์„œ ์œ ์ง€๋˜์ง€๋งŒ, ์ƒˆ๋กœ๊ณ ์นจ ์‹œ ์‚ฌ๋ผ์ง„๋‹ค.
    • Automatic Invalidation Period : ๊ฐœ๋ณ„ segment์˜ ์บ์‹œ๋Š” ํŠน์ • ์‹œ๊ฐ„ ์ดํ›„์— invalidation ๋œ๋‹ค.
      • ๋™์  ๋ Œ๋”๋ง ๊ฒฐ๊ณผ๋ฌผ : 30์ดˆ
      • ์ •์  ๋ Œ๋”๋ง ๊ฒฐ๊ณผ๋ฌผ : 5๋ถ„
      • prefetching ๋œ ๋™์  ๋ Œ๋”๋ง : 5๋ถ„๊นŒ์ง€ ์ €์žฅํ•˜๋„๋ก ์„ค์ •๊ฐ€๋Šฅ
  • Invalidation
    • Server Action์—์„œ
      • On-demand revalidating ์‚ฌ์šฉ : revalidatePath, revalidateTag
      • cookies.set, cookies.delete ์‚ฌ์šฉ (์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” route๊ฐ€ stale ๋˜๋Š” ๊ฒƒ๋“ค ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด)
    • ํด๋ผ์ด์–ธํŠธ์—์„œ : router.refresh๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Router Cache๊ฐ€ ์ „๋ถ€ clear ๋จ
  • Opting out : Router Cache๋Š” opt-out์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. prefetch์˜ ๊ฒฝ์šฐ๋Š” <Link> ์ปดํฌ๋„ŒํŠธ์—์„œ props๋กœ opt-outํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋ฐฉ๋ฌธํ•œ ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์บ์‹œ๋Š” ์—ฌ์ „ํžˆ ๋‚จ๋Š”๋‹ค.

Cache Interactions

Data Cache and Full Route Cache

  • Data Cache๋ฅผ opt-outํ•˜๊ฑฐ๋‚˜ revalidating ํ•˜๋ฉด Full Route Cache๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ ์šฉ๋œ๋‹ค.
  • Full Route Cache๋ฅผ opt-out ํ•œ๋‹ค๊ณ  Data Cache๋Š” opt-out ๋˜์ง€ ์•Š๋Š”๋‹ค. ํ•œ route ์•ˆ์— Data Cache๊ฐ€ ์ ์šฉ๋œ ์š”์ฒญ๊ณผ opt-out ๋œ ์š”์ฒญ์ด ๊ณต์กดํ•  ์ˆ˜ ์žˆ๋‹ค.

Data Cache and Client-side Router Cache

  • Route Handler์—์„œ Data Cache๋ฅผ revalidatingํ•˜์—ฌ๋„ Router Cache๊ฐ€ ๋ฐ”๋กœ invalidate ๋˜์ง€ ์•Š๋Š”๋‹ค. Router Handler๋Š” ํŠน์ • route์™€ ๋ฌถ์—ฌ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. Router Cache๋Š” ์œ ์ €๊ฐ€ ์ƒˆ๋กœ๊ณ ์นจํ•˜๊ฑฐ๋‚˜ Automatic Invalidation Period๊ฐ€ ์ง€๋‚ ๋•Œ๊นŒ์ง€ ์œ ์ง€๋œ๋‹ค.
  • ์ฆ‰๊ฐ์ ์œผ๋กœ Router Cache๋ฅผ ๋ฌดํšจํ™”์‹œํ‚ค๊ณ  ์‹ถ์œผ๋ฉด, Server Action์—์„œ revalidatePath, revalidateTag๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

APIs

๊ฐ next.js API๊ฐ€ ์˜ํ–ฅ์„ ๋ผ์น˜๋Š” ์บ์‹œ์ •๋ฆฌ

APIRouter CacheFull Route CacheData CacheReact Cache
<Link prefetch>Cache
router.prefetchCache
router.refreshRevalidate
fetchCacheCache
fetch options.cacheCache or Opt out
fetch options.next.revalidateRevalidateRevalidate
fetch options.next.tagsCacheCache
revalidateTagRevalidateRevalidate
revalidatePathRevalidateRevalidate
const revalidateRevalidate or Opt outRevalidate or Opt out
const dynamicCache or Opt outCache or Opt out
cookiesRevalidateOpt out
headers, useSearchParams, searchParamsOpt out
generateStaticParamsCache
React.cacheCache
unstable_cache (Coming Soon)

ํ˜„์žฌ ์กด์žฌํ•˜๋Š” ์ด์Šˆ

route segment config๋กœ opt-outํ•ด๋„ ์บ์‹œ๊ฐ€ ์ ์šฉ๋˜๋Š” ์ด์Šˆ

ํ˜„์žฌ ๊ณต์‹๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด ๋‹ค์Œ ๋‘ ์„ค์ •์€ Data Cache๊ณผ Full Route Cache๋ฅผ opt-out ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

  • const dynamic = 'force-dynamic'
  • const revalidate = 0

ํ•˜์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์Šˆ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

revalidate = 0์„ ์ฃผ์–ด๋„ ์บ์‹œ๊ฐ€ ์ ์šฉ๋œ๋‹ค๋Š” ์ด์Šˆ์ด๋‹ค. ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ๋‹ค์Œ ๋‘๊ฐ€์ง€๊ฐ€ ์ œ์‹œ๋˜์—ˆ์ง€๋งŒ, ๊ณต์‹์ ์ธ ๋‹ต๋ณ€์€ ์—†์—ˆ๋‹ค.

  • ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ํ›„ router.refresh()
  • <Link> ๋Œ€์‹  <a> ์‚ฌ์šฉ

route segment config์™€ fetch API์˜ revalidate ์„ค์ • ์ถฉ๋Œ ๋ฌธ์ œ

next.js ๊ณต์‹๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด, revalidate ์†์„ฑ์€ ํ•œ route์—์„œ ๊ฐ€์žฅ ๋‚ฎ์€ ๊ฐ’์œผ๋กœ ์„ค์ •๋œ๋‹ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„ ์„ค๋ช…์—์„œ revalidate = 0์€ data cache๋ฅผ opt-out ํ•œ๋‹ค๊ณ  ํ•˜์˜€์œผ๋‹ˆ route segment config๋กœ revalidate =0์„ ์„ค์ •ํ•˜๋ฉด, fetch API์—์„œ revalidate : 60์„ ์„ค์ •ํ•ด๋„ data cache๊ฐ€ ๋ฌด์‹œ๋  ์ค„ ์•Œ์•˜๋‹ค.

ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋กœ ์ ์šฉํ•ด๋ณธ ๊ฒฐ๊ณผ data cache๊ฐ€ opt-out ๋˜์ง€ ์•Š์•˜๋‹ค. ์บ์‹œ๊ฐ€ ๊ณ„์† ์ด๋ฃจ์–ด์ง€๊ณ  ์žˆ์—ˆ๋‹ค.

  • ์ฝ”๋“œ
// app/layout.js
export const dynamic = "force-dynamic"
 
export default function Layout({ children }) {
   return (
      <div>
         {children}
      </div>
   )
}
// app/post/page.js
export default function Page() {
   const posts = await fetch("http://localhost:8080/api/post", { next : {revalidate:60} });  
   return (
      <div>
         {posts.map(p => <div>{p.content}</div>)}
      </div>
   )
}
  • next.js ์ฝ˜์†” ๊ฒฐ๊ณผ๋ฌผ
 GET /post 200 in 281ms
 โ”‚
 โ”œโ”€โ”€โ”€โ”€ GET http://localhost:8080/api/post 200 in 7ms (cache: HIT)

์ด๊ฒƒ์ด next.js์˜ ์˜๋„๋œ ๋™์ž‘์ธ์ง€, ์ด์Šˆ์ธ์ง€๋Š” ์ข€ ๋” ์‚ดํŽด๋ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค.

review