next.js app router์ caching์ ๊ดํด ๊ณต๋ถํ ๋ด์ฉ์ ๋๋ค.
Overview
next.js ์์ ์ ๊ณตํ๋ caching mechanism์๋ ๋ค์๊ณผ ๊ฐ์ ๊ฒ๋ค์ด ์๋ค.
| Mechanism | What | Where | Purpose | Duration |
|---|---|---|---|---|
| Request Memoization | Return values of functions | Server | Re-use data in a React Component tree | Per-request lifecycle |
| Data Cache | Data | Server | Store data across user requests and deployments | Persistent (can be revalidated) |
| Full Route Cache | HTML and RSC payload | Server | Reduce rendering cost and improve performance | Persistent (can be revalidated) |
| Router Cache | RSC Payload | Client | Reduce server requests on navigation | User 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 :
fetchAPI์AbortController signal์ ์ค ์ ์์. ์์ - ๊ธฐํ
- next.js๊ฐ ์๋ React ๊ธฐ๋ฅ์
fetchํจ์์GETmethod์์๋ง ๋์ํจ- 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 ์์ฒญ ํ, ๊ฒฐ๊ณผ๋ฅผ ์บ์ฑํจ.
- ๋์๋ฐฉ์
- Time-based Revalidation : ํน์ ํ ์๊ฐ๋ง๋ค revalidate ํ ์ ์์.
- 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๋ฅผ ์๋ตํ์ฌ ํ์ด์ง ๋ก๋ฉ์ ๋น ๋ฅด๊ฒ ๋ง์น ์ ์๊ฒํ๋ค.
๋์๋ฐฉ์
- React Rendering on the Server
- next.js๋ React API๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๋๋ง์ ์ค์ผ์คํธ๋ ์ด์ ํ๋ค. ์ด๋ ๊ฐ route segments์ Suspense boundaries๋ฅผ chunks๋ก ๋๋๋ค.
- ๊ฐ chunk๋ ๋ ๋จ๊ณ๋ก ๋ ๋๋ง๋๋ค.
- React๊ฐ Server Component๋ฅผ React Server Component Payload๋ผ๋ ์คํธ๋ฆฌ๋ฐ์ ์ต์ ํ๋ ํน์ํ ๋ฐ์ดํฐ ํฌ๋งท์ผ๋ก ๋ ๋ํ๋ค.
- 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
- Next.js Caching on the Server (Full Route Server)
- ๊ธฐ๋ณธ์ ์ผ๋ก next.js๋ ๋ ๋๋ง ๊ฒฐ๊ณผ (React Server Component Payload, HTML)์ ์บ์ฑํ๋ค. ๋น๋ํ์ ๋๋ revalidation ์ ์์ฑ๋๋ ๊ฒฐ๊ณผ ์บ์ฑ
- React Hydration and Reconciliation on the Client : ์์ฒญ ์ ํด๋ผ์ด์ธํธ์์์ ๋์
- HTML ๋ ๋๋ง. ์ํธ์์ฉ ๋ถ๊ฐ๋ฅ
- React Server Component Payload๋ฅผ ์ฌ์ฉํ์ฌ Client์ ๋ ๋๋ Server Component tree๋ฅผ ์กฐ์ ํ๊ณ DOM์ ์ ๋ฐ์ดํธํ๋ค.
- JS instruction์ผ๋ก Client Components๋ฅผ hydrationํ๊ณ ์ํธ์์ฉ์ ๊ฐ๋ฅํ๊ฒ ํ๋ค.
- Next.js Caching on the Client (Router Cache)
- React Server Component Payload๋ ํด๋ผ์ด์ธํธ์ Router Cache๋ก์ ์ ์ฅ๋๋ค.
- Router Cache๋ navigation ์ ์ด์ ๋ฐฉ๋ฌธํ๋ ํ์ด์ง๋ฅผ ๋น ๋ฅด๊ฒ ๋ ๋๋งํ๊ฑฐ๋, ๋ค์ route๋ฅผ prefetchingํ๋ค.
- 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=0route 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 ๋๋ ๊ฒ๋ค ๋ฐฉ์งํ๊ธฐ ์ํด)
- On-demand revalidating ์ฌ์ฉ :
- ํด๋ผ์ด์ธํธ์์ :
router.refresh๋ฅผ ์ฌ์ฉํ๋ฉด Router Cache๊ฐ ์ ๋ถ clear ๋จ
- Server Action์์
- 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๊ฐ ์ํฅ์ ๋ผ์น๋ ์บ์์ ๋ฆฌ
| API | Router Cache | Full Route Cache | Data Cache | React Cache |
|---|---|---|---|---|
<Link prefetch> | Cache | |||
router.prefetch | Cache | |||
router.refresh | Revalidate | |||
fetch | Cache | Cache | ||
fetch options.cache | Cache or Opt out | |||
fetch options.next.revalidate | Revalidate | Revalidate | ||
fetch options.next.tags | Cache | Cache | ||
revalidateTag | Revalidate | Revalidate | ||
revalidatePath | Revalidate | Revalidate | ||
const revalidate | Revalidate or Opt out | Revalidate or Opt out | ||
const dynamic | Cache or Opt out | Cache or Opt out | ||
cookies | Revalidate | Opt out | ||
headers, useSearchParams, searchParams | Opt out | |||
generateStaticParams | Cache | |||
React.cache | Cache | |||
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์ ์๋๋ ๋์์ธ์ง, ์ด์์ธ์ง๋ ์ข ๋ ์ดํด๋ด์ผํ ๊ฒ ๊ฐ๋ค.