๐Ÿ’ถ Next.js Cache ์ ์šฉ

์ด๋„๊ฒฝยท2024๋…„ 2์›” 11์ผ
1

fRONTeND

๋ชฉ๋ก ๋ณด๊ธฐ
13/14
post-thumbnail

next.js๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ž์› ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆํ•„์š”ํ•œ ๋„คํŠธ์›Œํ‚น ์ž์› ์†Œ๋ชจ๊ฐ€ ํฌ๋”๋ผ๊ตฌ์š”.
์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด์„œ ๊ธฐ๋ณธ ์„ค์ •์„ ์ˆ˜์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์„ค์ •

// next.config.js
...
images: {
  	// ๋ฆฌ๋ชจํŠธ ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ
  	domains: ['your-cdn-image-domain'],
  	// ํ•˜๋ฃจ๋™์•ˆ ์บ์‹œ ์ €์žฅ
    minimumCacheTTL: 86400,
}
...

https://nextjs.org/docs/pages/api-reference/components/image#minimumcachettl

minimumCacheTTL์˜ ์„ค์ •๊ฐ’์œผ๋กœ ํ•˜๊ฑฐ๋‚˜, ํ—ค๋”์˜ Cache-control ์ค‘ ๋” ํฐ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ ํ—ค๋”์— cache๋ฅผ ์„ค์ •ํ•œ ๊ธฐ์–ต์ด ์—†์Šต๋‹ˆ๋‹ค.

์—ญ์‹œ๋‚˜ ์„ค์ •ํ•˜์ง€ ์•Š์•˜๋„ค์š”. ๐Ÿ™„

๋ฐฉ๊ธˆ ์ˆ˜์ •ํ•œ next.config.js ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

// next.config.js
...
headers: async () => {
    return [
      {
        headers: [
          {
            key: 'Cache-Control',
            value: 'private, no-cache, no-store, max-age=0, must-revalidate',
          },
        ],

        source: '/:path*',
      },
      {
        headers: [
          {
            key: 'Cache-Control',
            value: 'public, max-age=86400, immutable',
          },
        ],

        source: '/:path(.+\\.(?:ico|png|svg|jpg|jpeg|gif|webp|json|mp3|mp4|ttf|ttc|otf|woff|woff2)$)',
      },
      {
        headers: [
          {
            key: 'Cache-Control',
            value: 'private, no-cache, no-store, max-age=0, must-revalidate',
          },
        ],

        source: '/_next/data/:path*',
      },
      {
        headers: [
          {
            key: 'Cache-Control',
            value: 'private, no-cache, no-store, max-age=0, must-revalidate',
          },
        ],

        source: '/_next/:path(.+\\.(?:json)$)',
      },
    ];
  },
...

์บ์‹ฑ

์ด์ œ ์ด๋ฏธ์ง€๋ฅผ ํฌํ•จํ•œ ํฐํŠธ๋‚˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋“ค์ด ์บ์‹œ ์ฒ˜๋ฆฌ๋œ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์—…๋ฐ์ดํŠธ ํ›„ ์บ์‹œ์™€ ์ค‘๋ณต๋˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ..?
-> ๊ธฐ๋ณธ์ ์œผ๋กœ next์—์„œ๋Š” my_image.cd0657b0.png ์™€ ๊ฐ™์ด Content Hash ๊ฐ€ ์ ์šฉ๋˜์–ด ๋ฐฐํฌ ๋•Œ ๋งˆ๋‹ค ๋‹ค๋ฅธ ์ด๋ฆ„์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ์บ์‹ฑ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์ง€ ์•Š์„๊ฒƒ์ด๋ฉฐ, ํด๋ผ์ด์–ธํŠธ๋Š” ํ•ญ์ƒ ์ตœ์‹  ๋ฒ„์ „์˜ ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

profile
์•ˆ๋…•ํ•˜์„ธ์šฉ

0๊ฐœ์˜ ๋Œ“๊ธ€