跳到内容

output

在构建期间,Next.js 将自动追踪每个页面及其依赖项,以确定部署应用程序生产版本所需的所有文件。

此功能有助于大幅减少部署规模。以前,当使用 Docker 部署时,您需要安装包 dependencies 中的所有文件才能运行 next start。从 Next.js 12 开始,您可以利用 .next/ 目录中的输出文件追踪功能,仅包含必要的文件。

此外,这消除了对已弃用的 serverless 目标的需求,该目标可能导致各种问题,并产生不必要的重复。

工作原理

next build 期间,Next.js 将使用 @vercel/nft 静态分析 importrequirefs 的使用情况,以确定页面可能加载的所有文件。

Next.js 的生产服务器也会被追踪其所需的文件,并在 .next/next-server.js.nft.json 输出,这可以在生产环境中使用。

要利用输出到 .next 输出目录的 .nft.json 文件,您可以读取每个追踪中相对于 .nft.json 文件的文件列表,然后将它们复制到您的部署位置。

自动复制追踪的文件

Next.js 可以自动创建一个 standalone 文件夹,该文件夹仅复制生产部署所需的必要文件,包括 node_modules 中的选定文件。

要利用此自动复制功能,您可以在 next.config.js 中启用它

next.config.js
module.exports = {
  output: 'standalone',
}

这将创建一个位于 .next/standalone 的文件夹,然后可以单独部署该文件夹,而无需安装 node_modules

此外,还会输出一个最小的 server.js 文件,可以用作 next start 的替代方案。这个最小服务器默认不复制 public.next/static 文件夹,因为这些文件夹理想情况下应由 CDN 处理,尽管这些文件夹可以手动复制到 standalone/publicstandalone/.next/static 文件夹,之后 server.js 文件将自动提供这些文件夹。

要手动复制这些文件,您可以在 next build 后使用 cp 命令行工具

终端
cp -r public .next/standalone/ && cp -r .next/static .next/standalone/.next/

要在本地启动最小的 server.js 文件,请运行以下命令

终端
node .next/standalone/server.js

须知:

  • 如果您的项目需要监听特定的端口或主机名,您可以在运行 server.js 之前定义 PORTHOSTNAME 环境变量。例如,运行 PORT=8080 HOSTNAME=0.0.0.0 node server.js 以在 http://0.0.0.0:8080 上启动服务器。

注意事项

  • 在 monorepo 设置中进行追踪时,默认情况下项目目录用于追踪。对于 next build packages/web-apppackages/web-app 将是追踪根目录,并且不会包含该文件夹之外的任何文件。要包含此文件夹之外的文件,您可以在 next.config.js 中设置 outputFileTracingRoot
packages/web-app/next.config.js
module.exports = {
  // this includes files from the monorepo base two directories up
  outputFileTracingRoot: path.join(__dirname, '../../'),
}
  • 在某些情况下,Next.js 可能无法包含所需的文件,或者可能错误地包含未使用的文件。在这些情况下,您可以分别在 next.config.js 中利用 outputFileTracingExcludesoutputFileTracingIncludes。每个配置都接受一个对象,其中包含 minimatch globs 作为键来匹配特定页面,以及一个数组的值,该数组包含相对于项目根目录的 globs,以包含或排除在追踪中。
next.config.js
module.exports = {
  outputFileTracingExcludes: {
    '/api/hello': ['./un-necessary-folder/**/*'],
  },
  outputFileTracingIncludes: {
    '/api/another': ['./necessary-folder/**/*'],
    '/api/login/\\[\\[\\.\\.\\.slug\\]\\]': [
      './node_modules/aws-crt/dist/bin/**/*',
    ],
  },
}

注意: outputFileTracingIncludes/outputFileTracingExcludes 的键是一个 glob,因此特殊字符需要转义。

  • 目前,Next.js 对生成的 .nft.json 文件不做任何处理。这些文件必须由你的部署平台读取,例如 Vercel,以创建最小化部署。在未来的版本中,计划推出一个新命令来使用这些 .nft.json 文件。

实验性 turbotrace

追踪依赖项可能很慢,因为它需要非常复杂的计算和分析。我们用 Rust 创建了 turbotrace,作为 JavaScript 实现的更快更智能的替代方案。

要启用它,你可以将以下配置添加到你的 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 `outputFileTracingRoot` has the same effect
      // if the `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
    },
  },
}