{
  "id": "migrate-nextjs-14-to-16",
  "type": "playbooks",
  "category": "playbooks",
  "locale": "zh",
  "url": "/zh/playbooks/migrate-nextjs-14-to-16",
  "title": "Prompt-to-PR：将 Next.js 14 迁移至 16",
  "description": "分步 SOP，用于将 Next.js 14 App Router 项目迁移至 Next.js 16 — 涵盖 Turbopack、React 19、异步 API 和缓存变更。",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "TypeScript"
  ],
  "tags": [
    "nextjs",
    "typescript",
    "migrate"
  ],
  "difficulty": "hard",
  "updated": "2026-06-08",
  "markdown": "Next.js 16 搭载了 React 19，异步 `params` 和 `searchParams`，重构了 fetch 缓存机制，并将 Turbopack 设为默认开发打包工具。本手册将代理的注意力集中在破坏性变更上，并防止其重写可正常运行的代码。\n\n## 1. 需求\n\n将 `next` 从 14.x 升级到 16.x（并将 `react`/`react-dom` 升级到 19.x），确保零功能回归。处理官方 codemod 报告的所有破坏性变更以及其未捕获的残留问题。\n\n## 2. 初始提示\n\n```txt title=\"First Prompt\"\nMigrate this project from Next.js 14 to Next.js 16. Follow these steps exactly.\n\nStep 1 — run the official codemod:\n  npx @next/codemod@latest upgrade latest --yes\n\nStep 2 — manual fixes the codemod does not cover:\n  a. `params` and `searchParams` in page.tsx/layout.tsx files are now Promises.\n     Await them: `const { id } = await params;`\n     Do NOT destructure params in the function signature.\n  b. `cookies()`, `headers()`, `draftMode()` are now async.\n     Add `await` before every call.\n  c. `fetch()` is no longer cached by default in Route Handlers.\n     Where caching is intentional, add `{ next: { revalidate: N } }`.\n  d. Remove any `export const dynamic = 'force-dynamic'` that is now the default.\n  e. Replace `<Image>` with the new `onLoad` prop signature if present.\n\nStep 3 — update package.json:\n  \"next\": \"^16.0.0\"\n  \"react\": \"^19.0.0\"\n  \"react-dom\": \"^19.0.0\"\n  \"@types/react\": \"^19.0.0\"\n  \"@types/react-dom\": \"^19.0.0\"\n\nDo not change any business logic, UI, or database code.\nList every file you changed and why.\n```\n\n## 3. 预期文件变更\n\n```txt\npackage.json                          (next, react, react-dom, @types/react*)\nsrc/app/**/page.tsx                   (await params / searchParams)\nsrc/app/**/layout.tsx                 (await params where used)\nsrc/app/api/**/route.ts               (await cookies/headers; fetch cache opts)\nsrc/middleware.ts                     (if it uses deprecated config keys)\nnext.config.ts                        (remove deprecated options)\n```\n\n## 4. 审查清单\n\n- `package.json` 将 `next` 锁定为 `^16.0.0`，React 锁定为 `^19.0.0`。\n- 每个解构了 `params` 的页面/布局现在必须先 await 它。\n- 服务器组件中不再存在同步的 `cookies()` 或 `headers()` 调用。\n- 之前依赖隐式缓存的 `fetch` 调用现在具有明确的 `revalidate` 或 `no-store` 选项。\n- `next.config.ts` 中没有已弃用的 `experimental.appDir` 或 `swcMinify` 键。\n- TypeScript 通过 `bun tsc --noEmit` 编译，零错误。\n- 代理没有重写任何 UI 组件或业务逻辑。\n\n## 5. 测试命令\n\n```bash\n# Install updated deps\nbun install\n\n# Type-check\nbun tsc --noEmit\n\n# Dev build (Turbopack default in Next.js 16)\nbun dev\n\n# Production build — catches async-params errors at compile time\nbun run build\n\n# Run existing test suite\nbun test\n```\n\n## 6. 常见失败\n\n- **`params.id` 在 await 之前使用** — TypeScript 错误：`Property 'id' does not exist on type 'Promise<...>'`。代理有时在一个文件中 await 了 `params`，但在另一个文件中没有。\n- **`cookies()` 未 await** — 运行时错误：`cookies() was called outside of a Server Component`。添加 `await`。\n- **Turbopack 不兼容的 webpack 插件** — `next dev` 在自定义 `webpack()` 配置上出错。Turbopack 忽略 webpack 插件；迁移或进行条件性门控。\n- **`@types/react` 版本冲突** — 旧组件库与 React 19 类型之间的 peer-dep 不匹配。将 `@types/react` 锁定到 19，必要时覆盖。\n- **`useFormState` 已移除** — 被 `react` 的 `useActionState` 替代。codemod 通常能捕获到这一点，但请确认。\n\n## 7. 修复提示\n\n```txt title=\"Fix Prompt\"\nTypeScript reports: \"Property 'slug' does not exist on type\n'Promise<{ slug: string }>'\" in src/app/blog/[slug]/page.tsx.\n\nThe page function signature must be:\n  export default async function Page({ params }: { params: Promise<{ slug: string }> })\n\nThen at the top of the function body:\n  const { slug } = await params;\n\nApply the same fix to every other page or layout that destructures params\nwithout awaiting. List every file changed.\n```\n\n## 8. PR 描述\n\n```md title=\"PR description\"\n## Chore: Migrate Next.js 14 → 16 + React 19\n\n**Breaking changes addressed**:\n- `params` / `searchParams` are now `Promise`s — awaited in all pages/layouts\n- `cookies()` / `headers()` are now async — awaited in all Server Components\n- Fetch caching defaults changed — explicit `revalidate` added where needed\n- Removed deprecated `next.config.ts` keys (`swcMinify`, `experimental.appDir`)\n\n**Tooling**: dev server now uses Turbopack by default (`next dev --turbopack`).\n\nRun `bun run build` to confirm zero type errors before merging.\n```"
}