# Why the first step to Next.js should leave the router alone

> Wrap the old single-page app in one catch-all route, get it running, then move pages to the server one at a time.

Original: https://fdetimes.net/en/guides/nextjs-migration-leave-router-alone/

The official Next.js guide to migrating an old Create React App project sets a single goal for the first stage: get a working Next.js application as quickly as possible, then adopt new features incrementally.

No router rewrite, no move to the server, no optimization. For a frontend FDE sitting in front of a client's five-year-old React or Vue codebase, that is the most valuable advice in the entire documentation.

The reason is practical. Clients rarely bring in an FDE just to "modernize onto Next.js". They want the first page to load faster, they want the screen to stop flickering before login, and they want their own team to ship new features.

A full rewrite makes all of that wait, while the old app still has to run and still has real users.

Inngest says it moved its Vite-based Dev Server app to Next.js following exactly this SPA-first approach and finished in less than a day, to make room for upcoming features. That figure does not mean a client's app will also be done in a day. It shows how small the first step can be if you are willing to leave the router alone.

## Day one: wrap the whole legacy app in Next.js

The App Router requires a root layout, and that layout is a Server Component wrapping every page. During the migration, this file takes the place of the old `index.html`, which used to hold the `html`, `head` and `body` tags. The first job is to copy what is in `index.html` across.

```tsx
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <title>Customer Dashboard</title>
      </head>
      <body>
        <div id="root">{children}</div>
      </body>
    </html>
  )
}
```

Next comes the central trick. The Vite migration guide uses an optional catch-all route, `[[...slug]]`, so that every path in the app lands on the same Next.js page. The old router, whether React Router or something else, keeps handling everything else as before.

```tsx
// app/[[...slug]]/page.tsx
import { ClientOnly } from './client'

export function generateStaticParams() {
  return [{ slug: [''] }]
}

export default function Page() {
  return <ClientOnly />
}
```

```tsx
// app/[[...slug]]/client.tsx
'use client'
import dynamic from 'next/dynamic'

const App = dynamic(() => import('../../src/App'), { ssr: false })

export function ClientOnly() {
  return <App />
}
```

The starting configuration in the CRA migration guide uses `output: 'export'`, which produces a static SPA and disables server features. On day one that is precisely what you want: the new app behaves identically to the old one, so the client's QA team can compare it screen by screen.

```js
// next.config.mjs
const nextConfig = {
  output: 'export', // export a static SPA; delete this line when you want server features
  distDir: './dist',
}
export default nextConfig
```

One small task is often forgotten: environment variables. Any variable exposed to the browser needs a new prefix, from `VITE_` to `NEXT_PUBLIC_`, and the CRA guide asks the same of `REACT_APP_`. Skip this and values such as the API URL never reach the browser, and the bug usually surfaces only when the app makes a network call.

## When to start using the server

Once the SPA build runs reliably in the client's environment, delete the `output: 'export'` line. The Next.js guide states plainly that this is how to start using server features. From then on, each route pulled out of the catch-all is a separate decision, one that can be measured and reversed.

In the App Router, layouts and pages are Server Components by default, which means they can fetch data and render part of the interface on the server. Client Components should be added only where interactivity or browser APIs are needed.

Moving data fetching to the server solves two familiar problems of client-rendered apps: slow initial page loads and requests that wait on each other in a waterfall.

For pages behind a login, the Next.js documentation names Proxy specifically as the way to avoid users briefly seeing unauthenticated content before being redirected to the login page. If the client has complained about that flicker, this is usually the route worth extracting first, because the benefit is visible immediately.

## Legacy context and the 'use client' trap

Legacy React apps almost always rely on context for theme, auth or a store. The problem is that React context does not work in Server Components, and the root layout is a Server Component. The fix is to gather the providers into a single Client Component and place it in the layout.

```tsx
// app/providers.tsx
'use client'
import { AuthProvider } from '../src/auth'
import { ThemeProvider } from '../src/theme'

export function Providers({ children }: { children: React.ReactNode }) {
  return (
    <AuthProvider>
      <ThemeProvider>{children}</ThemeProvider>
    </AuthProvider>
  )
}
```

This is where many people make the most expensive mistake: whenever a component throws an error, they add `'use client'` to the top of the parent file and move on. The size of the client bundle is determined by where that boundary sits. The Next.js documentation recommends marking only interactive leaf components, rather than turning large sections of the interface into Client Components.

Take a hypothetical orders list page with a data table, a search box and a CSV export button. The table and the data fetching should stay on the server; only the search box and the export button need `'use client'`. Mark the whole page and you have moved to Next.js while still shipping the entire page to the browser, just as in the SPA days.

**Key point:** The first migration only has to run; every improvement comes later, one route at a time.

## Rollback must be cheap, even when the legacy app is Vue

Vercel has a guide to incremental migration. Its main idea: instead of rewriting in one go, move parts of the old app to the new one gradually.

That guide recommends putting riskier routes behind feature flags. When something goes wrong, switching the flag off is the rollback. For a client with real users, a line like that in the plan is more persuasive than any architecture slide.

Combined with the approach above, the flag can live inside the page just pulled out of the catch-all. Back to the orders page: the new version runs on the server, but if the flag is off, the page renders `` again, sending users back to the old app exactly as it was.

```tsx
// app/orders/page.tsx
import { ClientOnly } from '../[[...slug]]/client'
import { OrdersPage } from './orders-page'
import { isEnabled } from '../../src/flags'

export default async function Page() {
  if (!(await isEnabled('new-orders'))) return <ClientOnly />
  return <OrdersPage />
}
```

If the legacy app is Vue, the wrapper code above cannot be used directly, because it imports and renders a React component, `src/App`. In that case the strangler fig approach is worth considering: the new app grows while the old one shrinks.

When Thoughtworks moved away from AngularJS this way, it had the two frameworks communicate through the browser's native custom events, and the technique carries over to a Vue-to-React pairing.

Custom events work because they do not force the two sides to share a store or any library. The Vue side fires `window.dispatchEvent(new CustomEvent('cart:updated', { detail }))`, the React side listens, and the contract between them is just the event name and the shape of `detail`. Write that contract down in a file so the client's team can take over when you leave.

## Mistakes to avoid, and how to tell the story on a CV

The most common mistake is day-one ambition: switching frameworks while also reworking the router and changing how data is fetched. When a bug appears, nobody knows which step caused it. The second is forgetting to rename environment variable prefixes. The third is scattering `'use client'` from the root of the component tree, so the client pays for a migration without getting any speed benefit.

The fourth is a people problem: failing to agree with the client on the order in which routes are extracted. Pick the routes with the clearest pain, such as a flickering login page or a slow dashboard, so every step gives the client something to see.

If you are aiming for an FDE role, this experience belongs on your CV as a process, with the steps in order: wrap the SPA in Next.js, move routes to the server, keep rollback behind flags.

When reading job postings, look for lines that mention a client's legacy systems or incremental upgrades, and put this experience near the top of the application for that role.

Before the interview, prepare a story about a time you chose not to rewrite, with the reasons and how you kept a rollback path, because it shows you can weigh risk on the client's behalf.

A legacy codebase is what keeps the client's business running every day. The FDE's job is to move it into a new home without letting it stop breathing even once.

**Try this week:**

- Take a small open-source Vite or CRA app, build a Next.js SPA version with a [[...slug]] route and output: 'export', and record how long it took and what broke.
- Count the files containing 'use client' in that migration, then try pushing the boundary down to leaf components and compare the client bundle size before and after.
- Write a one-page migration plan for a hypothetical client: the order in which routes will be extracted, which routes sit behind feature flags, and how rollback works.

## Sources

- [How to migrate from Create React App to Next.js](https://nextjs.org/docs/app/guides/migrating/from-create-react-app)

- [How to migrate from Vite to Next.js](https://nextjs.org/docs/app/guides/migrating/from-vite)

- [Server and Client Components](https://nextjs.org/docs/app/getting-started/server-and-client-components)

- [Incremental migration (Microfrontends on Vercel)](https://vercel.com/academy/microfrontends-on-vercel/incremental-migration)

- [Farewell AngularJS](https://www.thoughtworks.com/en-sg/insights/articles/farewell-angularjs)

- [Migrating from Vite to Next.js (Inngest blog)](https://www.inngest.com/blog/migrating-from-vite-to-nextjs)
