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