How do you migrate a Next.js app from the Pages Router to the App Router?
AdvancedNext.js interview questionBy UIRound, 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:
- Move the old page component into a Client Component, for example
app/dashboard/dashboard-page.tsxwith"use client"at the top. It still receives data as props and can use state and effects. - Create
app/dashboard/page.tsxas 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.pathnamebecomesusePathname().router.querysplits intouseSearchParams()for the query string anduseParams()for dynamic segments.router.push()androuter.replace()still exist but no longer return a Promise. Wrap them instartTransitionfromuseTransitionto show a pending state.isFallback,asPath,isReadyandlocaleare gone, androuter.eventsis replaced by watchingusePathname()anduseSearchParams().
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 toroute.tsRoute Handlers when convenient, or replace client-to-API calls with Server Components and Server Actions. - Error pages:
404.jsbecomesnot-found.tsx;_error.jsbecomes per-segmenterror.tsx. - Shared layouts: the
getLayoutpattern becomes nestedlayout.tsxfiles. - Styles: global CSS can now be imported in any layout; Tailwind needs
appin 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 costDeleting
_appbefore the last page has moved, which drops global styles frompages/*Leaving the same path in both
pagesandappExpecting
getServerSidePropsornext/headto work insideappAssuming the old caching behaviour carries over without checking each route
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Migrate a large webpack app to Vite or Next.js without a feature freezeHard50 min
- Decide the server/client component boundary for a Next.js product pageHard45 min
- Check React Server Component boundaries: missing
'use client', server-only imports in client code and non-serialisable propsHard50 min - Implement SSR hydration mismatch detection and a reproduction/fix exerciseHard60 min
- Choose a rendering strategy per route for a marketplaceHard50 min