next.js๋Š” ๋นŒ๋“œ ์‹œ ์ž๋™์œผ๋กœ ๋ชจ๋“  ํŽ˜์ด์ง€์™€ ๊ทธ ์˜์กด์„ฑ์„ ํŠธ๋ž™ํ‚นํ•˜์—ฌ ๋ฐฐํฌ์‹œ ํ•„์š”ํ•œ ํŒŒ์ผ์„ ์•Œ์•„๋‚ธ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ๋ฐฐํฌ๋  ํŒŒ์ผ์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

์ด์ „์— ๋„์ปค๋กœ ๋ฐฐํฌํ•  ๋•, ๋ชจ๋“  ์˜์กด์„ฑ์„ ๊ฐ€์ ธ์˜จ ํ›„ next start๋ฅผ ์‹คํ–‰ํ•ด์•ผํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ next.js 12๋ถ€ํ„ฐ๋Š” Output File Tracing์„ ํ†ตํ•ด .next/ ๋””๋ ‰ํ„ฐ๋ฆฌ๋งŒ ์žˆ์œผ๋ฉด ๋œ๋‹ค. ๋‹จ, standalone ๋ชจ๋“œ๋ฅผ ์ผœ์•ผํ•œ๋‹ค.

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

next build๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋™์•ˆ, next.js๋Š” @vercel/nft๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์ ์œผ๋กœ import, require, fs๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋กœ๋“œ๋  ํŽ˜์ด์ง€๋ฅผ ์•Œ์•„๋‚ธ๋‹ค.

next.js์˜ ํ”„๋กœ๋•์…˜ ์„œ๋ฒ„ ๋˜ํ•œ ํ•„์š”ํ•œ ํŒŒ์ผ๊ณผ, .next/next-server.js.nft.json ์„ ํŠธ๋ž˜ํ‚นํ•œ๋‹ค. .nft.json๋ฅผ ํ™œ์šฉํ•  ๋•Œ๋Š” nft.json์— ์ƒ๋Œ€์ ์ธ ๋ชจ๋“  trace์˜ ํŒŒ์ผ๋ฆฌ์‹œํŠธ๋ฅผ ์ฝ๊ณ , ๋ฐฐํฌํ•  ์œ„์น˜์— ๋ณต์‚ฌํ•œ๋‹ค.

standalone mode

next.js๋Š” node_modules์—์„œ ํ•„์š”ํ•œ ํŒŒ์ผ์„ ๋ฝ‘์•„๋‚ด ๋ฐฐํฌ์‹œ์— ํ•„์š”ํ•œ ํŒŒ์ผ๋งŒ์œผ๋กœ ๊ตฌ์„ฑ๋œ standalone ํด๋”๋ฅผ ๊ตฌ์„ฑํ• ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ™œ์„ฑํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„  next.config.js์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•ด์•ผํ•œ๋‹ค.

module.exports = {
  output: 'standalone',
}

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด, ๋นŒ๋“œ์‹œ .next/standalone ํด๋”๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ์—ฌ๊ธฐ์— node_modules์—์„œ ํ•„์š”ํ•œ ํŒŒ์ผ๋งŒ ์ €์žฅ๋œ๋‹ค.

๋˜ํ•œ ์ตœ์†Œํ™”๋œ server.js๊ฐ€ next start ๋Œ€์‹ ์— ์‚ฌ์šฉ๋œ๋‹ค. ์ด ์ตœ์†Œํ™”๋œ ์„œ๋ฒ„๋Š” public, .next/static ํด๋”๋ฅผ ๋””ํดํŠธ๋กœ ๋ณต์‚ฌํ•˜์ง€ ์•Š๋Š”๋‹ค. CDN์— ์ €์žฅ๋˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ด์ƒ์ ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‹ค๋งŒ, standalone/.next/static ํด๋”์— ์ˆ˜๋™์œผ๋กœ ๋ณต์‚ฌํ•  ์ˆ˜๋Š” ์žˆ๋‹ค.

Note : next.config.js๋Š” next build ์‹œ ์ฝํ˜€์ ธ server.js์— ๋ณต์‚ฌ๋œ๋‹ค. ๋งŒ์•ฝ serverRuntimeConfig ๋˜๋Š” publicRuntimeConfig์˜ต์…˜ ์ด ์‚ฌ์šฉ๋œ๋‹ค๋ฉด, ๋นŒ๋“œ์‹œ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค.

๋งŒ์•ฝ ํ”„๋กœ์ ํŠธ์—์„œ ๋””ํดํŠธ ๋กœ๋”๋กœ Image Optimization๋ฅผ ์‚ฌ์šฉ์ค‘์ด๋ผ๋ฉด, sharp๋ฅผ ์„ค์น˜ํ•ด์•ผํ•œ๋‹ค.

npm i sharp

์ฃผ์˜์‚ฌํ•ญ

  • ๋ชจ๋…ธ๋ ˆํฌ ์„ค์ •์—์„œ tracing ํ•˜๋ฉด, ํ”„๋กœ์ ํŠธ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. next build packages/web-app์—์„œ packages/web-app์ด tracing root๊ฐ€ ๋˜๊ณ , ํ•ด๋‹น ํด๋” ๋ฐ–์— ์žˆ๋Š” ํŒŒ์ผ๋“ค์€ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋งŒ์•ฝ ๋ฐ”๊นฅ ํŒŒ์ผ๋„ tracing์— ํฌํ•จํ•˜๊ณ  ์‹ถ์œผ๋ฉด, experimental.outputFileTracingRoot๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

    // packages/web-app/next.config.js
    module.exports = {
      experimental: {
        // this includes files from the monorepo base two directories up
        outputFileTracingRoot: path.join(__dirname, '../../'),
      },
    }
  • ํ•„์š”ํ•œ ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจํ•˜๊ฑฐ๋‚˜ ์ž˜๋ชป๋œ ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ unstable_includeFiles, unstable_excludeFiles ๊ฐ™์€ page config๋ฅผ export ํ•˜๋ฉด ๋œ๋‹ค. ๊ฐ prop์€ minimatch์˜ ๋ฐฐ์—ด์„ ๋ฐ›๋Š”๋‹ค. ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ๋ฅผ ๋ฃจํŠธ๋กœ ์‚ผ๊ณ  ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค

  • ํ˜„์žฌ Next.js๋Š” .nft.json์„ ๊ฐ€์ง€๊ณ  ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด ํŒŒ์ผ์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ฐฐํฌ๋˜๋Š” ์‹œ์Šคํ…œ์—์„œ minimal deployment๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค. ํ–ฅํ›„ .nft.json ๋ฅผ ํ™œ์šฉํ•œ ์ปค๋งจ๋“œ๋ฅผ ๋‚ผ ๊ณ„ํš์ด ์žˆ๋‹ค.

Experimental turbotrace

์˜์กด์„ฑ์„ tracingํ•˜๋Š” ๊ฒƒ์€ ๋А๋ ค์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ next.js ํŒ€์€ turbotrace๋ฅผ ๋Ÿฌ์ŠคํŠธ๋กœ ์ž‘์„ฑํ•˜์—ฌ ์ด ๊ณผ์ •์„ ๋น ๋ฅด๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

ํ™œ์„ฑํ™”๋ฅผ ์œ„ํ•ด์„  next.config.js์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

// next.config.js
module.exports = {
  experimental: {
    turbotrace: {
      // control the log level of the turbotrace, default is `error`
      logLevel?:
      | 'bug'
      | 'fatal'
      | 'error'
      | 'warning'
      | 'hint'
      | 'note'
      | 'suggestions'
      | 'info',
      // control if the log of turbotrace should contain the details of the analysis, default is `false`
      logDetail?: boolean
      // show all log messages without limit
      // turbotrace only show 1 log message for each categories by default
      logAll?: boolean
      // control the context directory of the turbotrace
      // files outside of the context directory will not be traced
      // set the `experimental.outputFileTracingRoot` has the same effect
      // if the `experimental.outputFileTracingRoot` and this option are both set, the `experimental.turbotrace.contextDirectory` will be used
      contextDirectory?: string
      // if there is `process.cwd()` expression in your code, you can set this option to tell `turbotrace` the value of `process.cwd()` while tracing.
      // for example the require(process.cwd() + '/package.json') will be traced as require('/path/to/cwd/package.json')
      processCwd?: string
      // control the maximum memory usage of the `turbotrace`, in `MB`, default is `6000`.
      memoryLimit?: number
    },
  },
}

์ถœ์ฒ˜

https://nextjs.org/docs/advanced-features/output-file-tracing