Capítulo 58 de 456
Step-by-step migration of an existing Create React App (CRA) codebase into Next.js, starting as a client-only SPA and adopting Next.js features incrementally afterward.
output: 'export'), then adopt App Router features incrementally — minimizes merge conflicts.output: 'export': next.config.ts option that produces a static export; no SSR/API routes available while set.[[...slug]]): catches every path so a single page can serve the whole CRA app as an SPA during transition.next/dynamic: wraps the old CRA root <App /> with dynamic(() => import(...), { ssr: false }) to fully disable server rendering for it.REACT_APP_ → NEXT_PUBLIC_ for client-exposed env vars..src property (use <img src={logo.src} /> or the <Image> component).next dev --webpack restores webpack behavior for custom configs.// app/[[...slug]]/client.tsx — client-only entrypoint wrapping the CRA app
'use client'
import dynamic from 'next/dynamic'
const App = dynamic(() => import('../../App'), { ssr: false })
export function ClientOnly() {
return <App />
}
// next.config.ts — initial SPA-mode config
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
output: 'export', // Outputs a Single-Page Application (SPA)
distDir: 'build', // Changes the build output directory to `build`
}
export default nextConfig
| CRA | Next.js equivalente |
|---|---|
public/index.html | app/layout.tsx |
src/index.tsx | app/[[...slug]]/page.tsx (+ client.tsx) |
REACT_APP_* env vars | NEXT_PUBLIC_* |
homepage em package.json | basePath em next.config.ts |
proxy em package.json | rewrites() em next.config.ts |
| service worker CRA | navigator.serviceWorker.register(new URL(...)) |
| custom webpack/Babel | webpack() em next.config.ts (requer --webpack) |
output: 'export' esperando usar useParams ou SSR: static export não suporta esses recursos; remover a flag para liberar recursos de servidor.<img> sem .src ao trocar imports de imagem: import estático no Next.js retorna objeto, não string.[[...slug]] + generateStaticParams retornando [{ slug: [''] }] é o padrão para capturar todas as rotas durante a fase SPA.next-env.d.ts precisa entrar no include do tsconfig.json para resolver erros de tipo em .src de imagens.react-scripts e artefatos específicos do CRA (reportWebVitals, react-app-env.d.ts) devem ser removidos ao final.<Image>, next/font, <Script>) substituem soluções manuais do CRA.