Guide ·

How to Migrate a React App to Next.js Page by Page, Without a Big Rewrite

How to move a Create React App or Vite app to Next.js step by step: run the old app inside Next.js, move routes one by one and keep URLs and SEO intact.

By Ritesh Singh, full-stack developer

Many businesses have a React single-page app built with Create React App or Vite a few years ago. It works, but Google sees an almost empty HTML page, every route waits for a large JavaScript bundle, and Create React App is no longer maintained. Next.js fixes those problems with server rendering, file-based routes and built-in metadata. The mistake is trying to rewrite everything at once. This guide covers how I migrate a React app to Next.js page by page, so the product keeps working and users never see a "new version" freeze.

1. Decide whether Next.js is worth it

Next.js pays off when public pages need to rank on Google (product, service, blog or listing pages), when the first load feels slow on phones, or when the app is stuck on an unmaintained toolchain. If the app is an internal dashboard behind a login and nobody complains about speed, moving from Create React App to Vite may be enough. Be honest about this before starting: a migration costs weeks, and it should buy something the business can feel.

2. Take an inventory first

List every route in the React Router config, every environment variable, every place that reads window, document or localStorage, how login tokens are stored, and which pages get traffic from search (Google Search Console shows this). Also note third-party scripts such as analytics, chat widgets and payment SDKs. This list becomes the migration plan and the test checklist.

3. Step one: run the existing app inside Next.js

Create the Next.js project with the App Router and TypeScript, move the existing src folder in, and add one optional catch-all route, app/[[...slug]]/page.tsx. That page renders a small client component which loads your old App with dynamic(() => import('../../App'), { ssr: false }). React Router keeps handling every URL in the browser exactly as before. This is the approach the official Next.js migration guides for Create React App and Vite describe.

After this step nothing looks different to users, but the app now builds and deploys with Next.js. Ship it to production on its own. If something breaks, you know it is the toolchain, not a rewritten page.

4. Fix environment variables and global CSS

Create React App exposes variables that start with REACT_APP_, and Vite uses import.meta.env.VITE_. In Next.js, only variables starting with NEXT_PUBLIC_ reach the browser; everything else stays on the server. Rename the public ones, and use this moment to check that no secret key was ever shipped to the browser under the old prefix. Global CSS goes into the root layout; CSS modules and Tailwind CSS keep working as they are.

5. Move routes one at a time

Now take routes out of React Router one by one and give each its own folder in app/. A route that exists in app/ wins over the catch-all, so the rest of the app keeps running the old way. Start with public pages that matter for search, such as the home page, service pages or product listings, because they gain the most from server rendering. Leave complex logged-in screens for later.

Inside each moved page, swap React Router's Link (to=) for next/link (href=), and useNavigate or useParams for the hooks in next/navigation. Components that use state, effects or browser APIs need "use client" at the top. Code that touches window during render must move into useEffect, because the server has no window.

6. Fetch data on the server where it helps

In the old app, a page loads empty and then calls your API. In a Next.js server component the page can call the API before sending HTML, so Google and users get real content immediately. Your Node.js or PHP API stays exactly as it is; Next.js becomes one more client of it, next to your mobile app. I explain why that split works in my guide on one API for a Flutter app and web dashboard.

7. Move login from localStorage to cookies

Most React SPAs keep a JWT in localStorage. The server cannot read that, so server-rendered pages cannot tell who is logged in. Move the token to an httpOnly, Secure cookie set by your API or by a Next.js route handler, and read it on the server with cookies() from next/headers. As a bonus, JavaScript on the page can no longer read the token. Keep checking permissions in the API itself, as I describe in my guide on role-based access control in Next.js and Node.js.

8. Keep every URL, and redirect the ones that change

Rankings belong to URLs. Keep the same paths wherever possible, including trailing-slash style. If a URL must change, add a permanent redirect in the redirects() function of next.config, and update internal links so they point at the new address directly. Hash-based URLs such as /#/pricing never reach the server, so plan a client-side redirect for those during the move.

9. Add the SEO the SPA never had

Each moved page should export metadata or generateMetadata with its own title, description and canonical URL. Add app/sitemap.ts and app/robots.ts, structured data where it fits, and next/image with real width and height for large images. My Next.js SEO checklist for business websites covers the full list.

10. Deploy the same way at every step

Set output: 'standalone' in next.config and deploy the app as a Docker container, so each migration step ships the same way. I usually run it with Coolify on a VPS the client owns, with Cloudflare in front, as described in my guide on deploying a Next.js app and API with Coolify. Unlike the old static build, a Next.js app with server rendering needs a running Node.js process, so the hosting plan must change if the SPA lived on plain static hosting.

11. Test each step before the next

After every moved route, click through the old and the new parts together: login, logout, deep links, the back button, forms and payments. Compare page titles and status codes with a crawler, and watch Search Console for crawl errors in the weeks after. When the last route has moved, delete the catch-all page and React Router. That final step is small, because all the real work happened in safe pieces.

Checklist

  • Clear reason for the move: SEO, speed or an unmaintained toolchain.
  • Inventory of routes, env vars, browser-only code, auth and third-party scripts.
  • Old app running inside Next.js through an optional catch-all route, shipped on its own.
  • Public variables renamed to NEXT_PUBLIC_; no secrets in the browser.
  • Routes moved one at a time, search-relevant pages first.
  • Browser-only code in client components and effects.
  • Login token in an httpOnly cookie; permissions still enforced by the API.
  • Same URLs, permanent redirects for any that change.
  • Metadata, sitemap and robots on every moved page.
  • Docker deployment that runs a Node.js server, tested after every step.

Have a React app that should load faster or rank better? See my Next.js development service, or, if the backend needs work too, my Node.js and PHP API development service.

Related services

Hire me for this

Want this done for your product?

Start your project →