SuperAPI AI 工具集

next-cache-components-adoption

通用与其它

查看项目

更新于 10-16 00:21

技能介绍

用途:为 Next.js App Router 项目开启 Cache Components(顶层配置 cacheComponents: true),并提供逐路由迁移到通过构建的流程。适用于用户希望启用、采用或迁移到 Cache Components 的场景。

前提:需 App Router 项目(app/ 或 src/app/);定位 next.config.{js,ts,mjs,cjs} 作为项目根。Pages 路由不受影响。需可运行的 App,以及 Next.js 16.3 或更高版本,可用 npx @next/codemod@latest upgrade latest 升级。不得残留 dynamic、revalidate、fetchCache 导出;experimental.dynamicIO 必须先移除,experimental.useCache 为已弃用别名。

主要流程:选择两种策略之一——增量式(先运行 codemod 让所有路由退出校验,再逐个功能修复)或直接式(直接开启并修复构建标出的阻塞路由)。核心循环是借助 next dev 开发覆盖层与浏览器自上而下逐路由修复,构建仅作检测与最终验证。

阻塞类型:请求时读取(cookies()、headers()、await params、await searchParams)、模块或渲染期同步 IO(new Date()、Date.now()、Math.random()、crypto.randomUUID())、在含顶层 "use cache" 文件中读取请求数据。修复通常是把读取推入 <Suspense> 包裹的子组件。export const instant = false 可标记某路由允许阻塞。

工具:建议安装 next-dev-loop 进行运行时验证(npx skills add https://github.com/vercel/next.js/tree/canary/skills/next-dev-loop),需要 Turbopack;也可用浏览器自动化或仅构建验证。构建可用 --debug-build-paths 和 --debug-prerender 辅助。每个报错对应一个文档页,应查阅。

使用方式与限制:codemod 命令为 npx @next/codemod@latest cache-components-instant-false ./app,需在干净工作树上运行。具体每错误配方在开发覆盖层修复卡与构建输出中,离线文档位于 node_modules/next/dist/docs/。资料未提供具体收费信息与效果保证。