Lessons in this course (8)

8. Pages Router (legacy) vs App Router

IntermediateMediumLesson 8 of 8

Many existing apps still use the Pages Router (pages/ folder):

text
pages/
├── _app.tsx            wraps every page (global CSS, providers)
├── _document.tsx       custom <html>/<body>
├── index.tsx           "/"
├── products/[id].tsx   "/products/42"
└── api/products.ts     API route: export default function handler(req, res)
tsx
// Pages Router data fetching
export async function getServerSideProps({ params }) { return { props: { product: await getProduct(Number(params.id)) } }; }   // per request
export async function getStaticProps() { return { props: { products: await listProducts() }, revalidate: 60 }; }                 // build + ISR
export async function getStaticPaths() { return { paths: [], fallback: "blocking" }; }
Pages RouterApp Router
getServerSideProps / getStaticPropsasync Server Components + fetch/DB calls, revalidate, generateStaticParams
_app.tsx / _document.tsxapp/layout.tsx
API routes pages/api/*.tsRoute handlers app/**/route.ts + Server Actions
All components are client componentsServer Components by default
next/headMetadata API

Both routers can coexist during a gradual migration.