{
  "id": "migrate-nextjs-14-to-16",
  "type": "prompts",
  "category": "prompts",
  "locale": "pt",
  "url": "/pt/prompts/migrate-nextjs-14-to-16",
  "title": "Prompt para migrar um aplicativo Next.js 14 para Next.js 16",
  "description": "Prompt estruturado de agente de IA para migrar um projeto Next.js 14 App Router para Next.js 16 com tratamento de mudanças de quebra e etapas incrementais.",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "TypeScript"
  ],
  "tags": [
    "nextjs",
    "typescript",
    "migrate"
  ],
  "difficulty": "hard",
  "updated": "2026-06-08",
  "markdown": "Use este prompt para conduzir uma migração cuidadosa e incremental do Next.js 14 para o 16 — lidando com a mudança de API assíncrona de `params`/`searchParams`, a dependência React 19 e padrões obsoletos de `next/headers` antes da atualização.\n\n## Prompt Principal\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## Notas de Implementação\n\n- No Next.js 15+, `params` e `searchParams` são encapsulados em `Promise`. Acessá-los de forma síncrona irá compilar, mas lançará em tempo de execução — a auditoria no Passo 1 é essencial antes de atualizar.\n- `cookies()` e `headers()` são assíncronos no Next.js 15+; o modo de falha mais comum é chamá-los dentro de uma função que não está marcada como `async`.\n- O React 19 remove algumas APIs legadas (`defaultProps` em componentes de função, string refs). Execute `bun run typecheck` entre cada etapa para detectar essas cedo.\n\n## Mudanças Esperadas nos Arquivos\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## Critérios de Aceitação\n\n- `bun run typecheck` retorna 0 após todas as etapas.\n- `bun run build` retorna 0 sem avisos sobre APIs obsoletas.\n- Todas as rotas dinâmicas renderizam corretamente em `bun run dev`.\n- Nenhum prop `params` ou `searchParams` é acessado de forma síncrona.\n\n## Comandos de Teste\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## Erros Comuns de IA\n\n- Pular a etapa de auditoria e fazer todas as alterações de uma só vez, causando erros difíceis de rastrear.\n- Tipar params assíncronos como `{ params: { slug: string } }` em vez de `{ params: Promise<{ slug: string }> }`.\n- Esquecer de atualizar `@types/react` junto com `react`, deixando incompatibilidades de tipo.\n- Não aguardar `cookies()` dentro de uma função auxiliar aninhada que não é assíncrona.\n\n## Prompt de Correção\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```"
}