N Next.js 中文
原文 ↗

turbopackGc

配置 Turbopack,以从内存和持久缓存中移除不可达的工作。

更新于 2026-10-06Next.js 16.4.6

此功能目前处于实验阶段,可能会发生变化,不建议用于生产环境。欢迎试用并在 GitHub 上分享反馈。

experimental.turbopackGc 选项为 Turbopack 的内存缓存和持久缓存启用垃圾回收。

TypeScript

next.config.tsts
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  experimental: {
    turbopackGc: true,
  },
}

export default nextConfig

JavaScript

next.config.jsjs
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    turbopackGc: true,
  },
}

module.exports = nextConfig

参考

turbopackGc 默认为 false,接受布尔值或对象:

值 描述
false 禁用垃圾回收。
true 使用默认时序启用垃圾回收。
{ minProgressMs?, rootTtlMs? } 启用垃圾回收,并覆盖一个或多个默认时序。

启用后,Turbopack 会从内存及其持久缓存中移除不可达的工作。当 next dev 和 next build 使用 Turbopack 时,该选项对两者均适用。

须知:

选项

对象形式接受以下属性:

属性 类型 默认值 描述
minProgressMs 非负 number 100 垃圾回收流程在响应中断之前运行的最短时间。
rootTtlMs 非负 number 259200000(3 天) 垃圾回收根在 Turbopack 将其移除之前可以保持未锚定的时间。

以下配置让一次垃圾回收流程在让出之前至少运行 200 毫秒,并在一天后使未锚定的根过期:

TypeScript

next.config.tsts
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  experimental: {
    turbopackGc: {
      minProgressMs: 200,
      rootTtlMs: 24 * 60 * 60 * 1000,
    },
  },
}

export default nextConfig

JavaScript

next.config.jsjs
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    turbopackGc: {
      minProgressMs: 200,
      rootTtlMs: 24 * 60 * 60 * 1000,
    },
  },
}

module.exports = nextConfig

版本历史

版本 变更
v16.4.0 新增 experimental.turbopackGc 配置项。