{
  "id": "migrate-nextjs-14-to-16",
  "type": "prompts",
  "category": "prompts",
  "locale": "zh",
  "url": "/zh/prompts/migrate-nextjs-14-to-16",
  "title": "将 Next.js 14 应用迁移到 Next.js 16 的提示",
  "description": "结构化 AI 代理提示，用于将 Next.js 14 App Router 项目迁移到 Next.js 16，处理破坏性变更并分步进行。",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "TypeScript"
  ],
  "tags": [
    "nextjs",
    "typescript",
    "migrate"
  ],
  "difficulty": "hard",
  "updated": "2026-06-08",
  "markdown": "使用此提示来驱动一个谨慎、渐进的 Next.js 14 到 16 迁移——处理异步 `params`/`searchParams` API 变更、React 19 对等依赖以及升级前已弃用的 `next/headers` 模式。\n\n## Main Prompt\n\n```txt title=\"Main Prompt\"\nYou are migrating an existing Next.js 14 App Router project to Next.js 16.\n\nDo this in the following ordered steps — do not skip steps or combine them.\n\nStep 1 — Audit (read-only):\n  - List every file that uses `params` or `searchParams` as synchronous props.\n  - List every file that calls `cookies()`, `headers()`, or `draftMode()` without `await`.\n  - List every `next/image` usage with a deprecated prop (e.g., `layout`, `objectFit`).\n  - List the current `peerDependencies` for React and React DOM.\n  Output the audit as a plain list. Stop and wait for my review.\n\nStep 2 — Update deps:\n  - Run: `bun add next@16 react@19 react-dom@19 @types/react@19 @types/react-dom@19`\n  - Do NOT change any source files in this step.\n\nStep 3 — Fix async params:\n  - For every Page, Layout, and `generateMetadata` function that receives `params` or\n    `searchParams`, make the function `async` and `await` the prop before use.\n  - Type the props as `Promise<{ slug: string }>` (or the relevant shape), not `{ slug: string }`.\n\nStep 4 — Fix async dynamic APIs:\n  - Add `await` before every call to `cookies()`, `headers()`, and `draftMode()`.\n  - If the call is inside a non-async function, make that function async.\n\nStep 5 — Fix deprecated Image props:\n  - Remove `layout=\"fill\"` and replace with `fill` (boolean).\n  - Remove `objectFit` and `objectPosition` props and move them to a `className` or `style`.\n\nStep 6 — Verify:\n  - Run `bun run typecheck` and fix all remaining TypeScript errors.\n  - Run `bun run build` and confirm exit 0.\n\nAfter each step, show the diff and wait for confirmation before proceeding.\n```\n\n## 实现说明\n\n- 在 Next.js 15+ 中，`params` 和 `searchParams` 被 `Promise` 包裹。同步访问它们会编译通过但在运行时抛出错误——因此在升级前，第一步的审计至关重要。\n- `cookies()` 和 `headers()` 在 Next.js 15+ 中是异步的；最常见的失败模式是在未标记为 `async` 的函数内部调用它们。\n- React 19 移除了部分遗留 API（函数组件上的 `defaultProps`、字符串 refs）。在每个步骤之间运行 `bun run typecheck` 以尽早发现这些变化。\n\n## 预期文件更改\n\n```txt\npackage.json                          (edited — next, react, react-dom versions)\nbun.lock / package-lock.json          (edited automatically)\nsrc/app/**/page.tsx                   (edited — async params)\nsrc/app/**/layout.tsx                 (edited — async params)\nsrc/app/**/generateMetadata           (edited — async params)\nsrc/components/**/*.tsx               (edited — deprecated Image props)\n```\n\n## 验收标准\n\n- 所有步骤完成后，`bun run typecheck` 退出码为 0。\n- `bun run build` 退出码为 0，且无关于已弃用 API 的警告。\n- 所有动态路由在 `bun run dev` 中正确渲染。\n- 没有同步访问 `params` 或 `searchParams` 属性。\n\n## 测试命令\n\n```bash\nbun run typecheck\nbun run build\nbun run dev\n# visit several dynamic routes and confirm no runtime errors\ngrep -r \"layout=\\\"fill\\\"\" src/ && echo \"FAIL: deprecated Image prop found\"\ngrep -rn \"cookies()\\|headers()\\|draftMode()\" src/ | grep -v \"await\" && echo \"WARN: unawaited dynamic API\"\n```\n\n## 常见 AI 错误\n\n- 跳过审计步骤并一次性完成所有更改，导致难以追踪的错误。\n- 将异步 params 类型写为 `{ params: { slug: string } }` 而不是 `{ params: Promise<{ slug: string }> }`。\n- 忘记同时升级 `@types/react` 和 `react`，导致类型不匹配。\n- 在本身不是异步的嵌套辅助函数内部未对 `cookies()` 使用 await。\n\n## 修复提示\n\n```txt title=\"Fix Prompt\"\nThe build fails with type errors on params or a runtime error about cookies/headers.\nFix in order:\n1. For any page or layout receiving `params`, update the prop type to `Promise<{ slug: string }>` and\n   add `const { slug } = await params;` at the top of the function body.\n2. Find every call to `cookies()`, `headers()`, `draftMode()` not preceded by `await` and add it.\n   Make the containing function async if it isn't already.\n3. Run `bun run typecheck` and show me only the remaining errors.\n```"
}