How do you migrate a Next.js app from the Pages Router to the App Router?

AdvancedNext.js interview questionBy , updated 21 November 2026

Short answer

Incrementally. The app directory is designed to run alongside pages, so you add a root layout, then move one route at a time and ship each move. For every route you replace getServerSideProps and getStaticProps with data fetched in async Server Components, next/head with the Metadata API, and next/router with next/navigation, and you mark the interactive parts as Client Components.

Step 1: Add the app directory and a root layout

Create app/layout.tsx. It must render <html> and <body>, and it replaces both _app and _document for routes in app:

// app/layout.tsx
import "../styles/globals.css";
import { Providers } from "./providers";

export const metadata = { title: "Acme", description: "..." };

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <Providers>{children}</Providers>
      </body>
    </html>
  );
}

Copy global styles and shared shell markup from _app and _document. Context providers (theme, query client, auth) must move into a "use client" file such as providers.tsx, because a Server Component cannot create context. Keep _app and _document until the last page has moved: styles in app/layout.tsx do not apply to pages/*.

Step 2: Move one route, the low-risk way

A route lives at app/<path>/page.tsx instead of pages/<path>.tsx. Next.js reports a conflict if both routers define the same path, so delete the old file in the same change.

The official path for a first move keeps behaviour close to the Pages Router:

  1. Move the old page component into a Client Component, for example app/dashboard/dashboard-page.tsx with "use client" at the top. It still receives data as props and can use state and effects.
  2. Create app/dashboard/page.tsx as a Server Component that fetches the data and renders that Client Component.
// app/dashboard/page.tsx (Server Component)
import { cookies } from "next/headers";
import DashboardPage from "./dashboard-page";

export default async function Page() {
  const session = (await cookies()).get("session")?.value;
  const projects = await getProjects(session);
  return <DashboardPage projects={projects} />;
}

That works on day one. Later, shrink the client part: push "use client" down to the components that actually need interactivity so the rest stops shipping JavaScript. The boundary decisions are covered in Server Components vs Client Components.

Step 3: Replace the data fetching functions

Pages Router App Router
getServerSideProps Fetch in the async page; request data from cookies() and headers()
getStaticProps Fetch in the page, with the result cached
getStaticProps with revalidate fetch(url, { next: { revalidate: 60 } }), or a cache lifetime with Cache Components
getStaticPaths generateStaticParams
fallback dynamicParams
{ notFound: true } notFound() from next/navigation
{ redirect } redirect() from next/navigation
req and res cookies(), headers() (both async)

Because rendering is now decided by what components read, not which function a page exports, check that a page you expected to be static has not become dynamic, and the other way round. In Next.js 16, Cache Components and "use cache" give explicit control; the details are in how caching works in Next.js and data fetching in the App Router. Note that params and searchParams are Promises in the App Router and must be awaited.

Step 4: Replace next/head with the Metadata API

Export a metadata object, or generateMetadata when the title depends on data, from a layout or page. Remove <Head> tags; they are not supported in app.

export async function generateMetadata({ params }: PageProps<"/blog/[slug]">) {
  const { slug } = await params;
  const post = await getPost(slug);
  return { title: post.title, description: post.excerpt };
}

Step 5: Replace the router hooks

useRouter from next/router does not work in app. Import from next/navigation, in Client Components only:

  • router.pathname becomes usePathname().
  • router.query splits into useSearchParams() for the query string and useParams() for dynamic segments.
  • router.push() and router.replace() still exist but no longer return a Promise. Wrap them in startTransition from useTransition to show a pending state.
  • isFallback, asPath, isReady and locale are gone, and router.events is replaced by watching usePathname() and useSearchParams().

For components shared by both routers during the migration, useRouter from next/compat/router can be used in both: it returns null instead of throwing when the Pages Router is not mounted.

Step 6: Finish the rest

  • API routes keep working in pages/api. Move them to route.ts Route Handlers when convenient, or replace client-to-API calls with Server Components and Server Actions.
  • Error pages: 404.js becomes not-found.tsx; _error.js becomes per-segment error.tsx.
  • Shared layouts: the getLayout pattern becomes nested layout.tsx files.
  • Styles: global CSS can now be imported in any layout; Tailwind needs app in its content paths if you configure them.

When pages is empty except for anything you chose to keep, delete _app and _document.

Planning the order

Start with a simple, low-traffic page to prove the root layout, providers and styles. Then move routes in groups that users navigate between, because navigating between a Pages route and an App route is a hard navigation and next/link does not prefetch across the two. Measure bundle size and Core Web Vitals per route as you go. The general strategy for this kind of change is the exercise in migrating a large app without a feature freeze.

Common follow-up questions

Do I have to migrate?

No. The Pages Router is still supported. Migrate when you want Server Components, streaming, nested layouts or Server Actions. The differences are summarised in App Router vs Pages Router.

Why did my provider break after moving it into the layout?

The root layout is a Server Component, and context needs a Client Component. Wrap providers in a "use client" component and render that from the layout.

Are there codemods?

Yes, Next.js ships codemods for deprecated APIs, including one that converts synchronous params, cookies() and headers() access to async. Moving data fetching and deciding the client boundary are still manual work.

Mistakes to avoid

  • A big-bang rewrite when both routers can run side by side

  • Marking every migrated page "use client" and keeping the Pages Router bundle cost

  • Deleting _app before the last page has moved, which drops global styles from pages/*

  • Leaving the same path in both pages and app

  • Expecting getServerSideProps or next/head to work inside app

  • Assuming the old caching behaviour carries over without checking each route

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Migrate a large webpack app to Vite or Next.js without a feature freezeHard50 min
  2. Decide the server/client component boundary for a Next.js product pageHard45 min
  3. Check React Server Component boundaries: missing 'use client', server-only imports in client code and non-serialisable propsHard50 min
  4. Implement SSR hydration mismatch detection and a reproduction/fix exerciseHard60 min
  5. Choose a rendering strategy per route for a marketplaceHard50 min