# 输出文件跟踪

During a build, Next.js will automatically trace each page and its dependencies to determine all of the files that are needed for deploying a production version of your application.

This feature helps reduce the size of deployments drastically. Previously, when deploying with Docker you would need to have all files from your package's dependenciesinstalled to run next start. Starting with Next.js 12, you can leverage Output File Tracing in the .next/directory to only include the necessary files.

Furthermore, this removes the need for the deprecated serverlesstarget which can cause various issues and also creates unnecessary duplication.

# How It Works

During next build, Next.js will use @vercel/nft to statically analyze import, require, and fsusage to determine all files that a page might load.

Next.js' production server is also traced for its needed files and output at .next/next-server.js.nft.jsonwhich can be leveraged in production.

To leverage the .nft.jsonfiles emitted to the .nextoutput directory, you can read the list of files in each trace which are relative to the .nft.jsonfile and then copy them to your deployment location.

# Automatically Copying Traced Files (experimental)

Next.js can automatically create a standalonefolder which copies only the necessary files for a production deployment including select files in node_modules.

To leverage this automatic copying you can enable it in your next.config.js:

module.exports = {
  experimental: {
    outputStandalone: true,
  },
}

This will create a folder at .next/standalonewhich can then be deployed on it's own without installing node_modules.

Additionally, a minimal server.jsfile is also output which can be used instead of next start. This minimal server does not handle serving the staticdirectory as this should be handled by a CDN instead.

# Caveats

  • While tracing in monorepo setups, the project directory is used for tracing by default. For next build packages/web-app, packages/web-appwould be the tracing root and any files outside of that folder will not be included. To include files outside of this folder you can set experimental.outputFileTracingRootin your next.config.js.
// packages/web-app/next.config.js
module.exports = {
  experimental: {
    // this includes files from the monorepo base two directories up
    outputFileTracingRoot: path.join(__dirname, '../../'),
  },
}

  • There are some cases that Next.js might fail to include required files, or might incorrectly include unused files. In those cases, you can export page configs props unstable_includeFilesand unstable_excludeFilesrespectively. Each prop accepts an array of globs) relative to the project's root to either include or exclude in the trace.
  • Currently, Next.js does not do anything with the emitted .nft.jsonfiles. The files must be read by your deployment platform, for example Vercel , to create a minimal deployment. In a future release, a new command is planned to utilize these .nft.jsonfiles.
Last Updated: 5/13/2023, 8:55:38 PM