How do you migrate from the Pages Router to the App Router?

Migrate incrementally: both routers work in the same app, so upgrade Next.js first, create a root app/layout.tsx that replaces _app and _document, then move routes one at a time from pages/ to app/, converting data fetching to Server Components, next/router to next/navigation, and API routes to Route Handlers or Server Functions as you go.

Step by step

  1. Upgrade to the latest Next.js and React (npx @next/codemod@canary upgrade latest, or next upgrade), and fix breaking changes (async request APIs, proxy.ts, image defaults).
  2. Root layout: create app/layout.tsx with <html> and <body>, global CSS, fonts and providers from _app and _document. Put providers in a "use client" file.
  3. Move one route: copy pages/about.tsx to app/about/page.tsx. Routes in app/ take priority; delete the pages/ version once it works.
  4. Data fetching:
    • getStaticProps becomes an async Server Component with "use cache" + cacheLife (or fetch with caching options).
    • getServerSideProps becomes a Server Component reading cookies()/headers() inside <Suspense>.
    • getStaticPaths becomes generateStaticParams.
  5. Client logic: mark interactive components with "use client". Replace useRouter from next/router with useRouter, usePathname and useSearchParams from next/navigation; router.query becomes useParams and useSearchParams; router events become effects on pathname changes.
  6. Head tags: next/head becomes the Metadata API.
  7. Errors and 404: pages/404.tsx and _error become not-found.tsx, error.tsx and global-error.tsx.
  8. APIs: pages/api routes can stay. Move them to route.ts when convenient; form posts can become Server Functions.
  9. Adopt Cache Components last, once routes are on the App Router.

Pitfalls

  • Navigating between pages/ and app/ routes is a full page load.
  • CSS-in-JS needs a registry.
  • Libraries using next/router need App Router versions.

Code example

tsx
1// BEFORE: pages/products/[id].tsx
2// export const getStaticPaths = async () => ({ paths: [], fallback: "blocking" });
3// export const getStaticProps = async ({ params }) => {
4// const product = await getProduct(params.id);
5// return { props: { product }, revalidate: 3600 };
6// };
7// export default function Product({ product }) {
8// const router = useRouter(); // next/router
9// return <><Head><title>{product.name}</title></Head><h1>{product.name}</h1></>;
10// }
11
12// AFTER: app/products/[id]/page.tsx
13import type { Metadata } from "next";
14import { cacheLife } from "next/cache";
15
16async function getProduct(id: string) {
17 "use cache";
18 cacheLife("hours"); // replaces revalidate: 3600
19 return fetch(`https://api.example.com/products/${id}`).then((r) => r.json());
20}
21
22export async function generateStaticParams() {
23 return [{ id: "1" }]; // replaces getStaticPaths
24}
25
26export async function generateMetadata({ params }: PageProps<"/products/[id]">): Promise<Metadata> {
27 const { id } = await params;
28 return { title: (await getProduct(id)).name }; // replaces next/head
29}
30
31export default async function Product({ params }: PageProps<"/products/[id]">) {
32 const { id } = await params;
33 const product = await getProduct(id);
34 return <h1>{product.name}</h1>;
35}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app