nextjs-pages-to-app
Skill1 starsChanged 7 months agoMITView on GitHub
Migrate a Next.js Pages-router app to App-router incrementally — coexistence strategy, data-fetching translation (getServerSideProps → Server Components, getStaticProps → static), layouts, auth middleware, API routes → route handlers. Use when migrating an existing Pages-router app, not when starting greenfield.
In this repository50 other files it gives its agents.
- algolia-searchSkill
- audit-tautological-testsSkill
- blame-archaeologySkill
- brevo-emailSkill
- bun-runtimeSkill
- clerk-authSkill
- cloudflare-workersSkill
- dbt-data-modelingSkill
- explain-this-diffSkill
- extract-skill-from-sessionSkill
- fastapi-productionSkill
- find-dead-codeSkill
- find-real-bugSkill
- gdpr-dpiaSkill
- github-actions-ciSkill
- graphql-relaySkill
- grpc-servicesSkill
- hipaa-auditSkill
- india-dpdp-actSkill
- java-8-to-21Skill
- jest-to-vitestSkill
- kubernetes-helmSkill
- mongo-to-postgresSkill
- node-version-upgradeSkill
- opentelemetry-instrumentSkill
- pci-dss-checklistSkill
- plan-the-rollbackSkill
- python-2-to-3Skill
- razorpay-integrationSkill
- react-native-expoSkill
- scaffold-cli-toolSkill
- scaffold-fullstack-appSkill
- scaffold-go-microserviceSkill
- scaffold-new-appSkill
- scaffold-saas-starterSkill
- sentry-monitoringSkill
- shrink-this-prSkill
- soc2-evidenceSkill
- spec-from-conversationSkill
- stripe-integrationSkill
- supabase-backendSkill
- terraform-patternsSkill
- threat-modeling-strideSkill
- triage-stack-traceSkill
- wcag-accessibility-auditSkill
- webpack-to-viteSkill
- write-adrSkill
- write-commit-messageSkill
- write-pr-descriptionSkill
- write-runbookSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 2 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 39
If a route exists in both, app/ wins in Next 13.4+.
- Distinctive: line 49
Delete Pages router files: when all routes are migrated.
- Very specific: line 152
All routes under /dashboard/* automatically use this layout.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Migrate Next.js: Pages → App Router
The Pages → App migration looks daunting but Next.js supports both routers in the same project. Migrate incrementally, route by route. This skill is the order and the gotchas.
When to use
- Existing Pages-router app on Next.js 13+ (App router available since 13.4 stable).
- Want Server Components (RSC), nested layouts, streaming, parallel routes.
- Refactoring opportunity coincides with router migration.
When NOT to use
- Greenfield app — start with App router directly.
- App on Next < 13 — upgrade Next first.
- Heavy
getStaticPaths+ISR coverage where Pages router is already optimal — App router's equivalent is fine but the migration win is small.
The principle: coexist, don't cut over
Both routers can run in the same app. pages/ and app/ directories are read together. Migrate route-by-route. Never do a big-bang cutover for a non-trivial app.
my-app/
pages/
api/... # legacy API routes (still work)
blog/[slug].tsx # not yet migrated
about.tsx # not yet migrated
app/
page.tsx # migrated home
layout.tsx
login/page.tsx # migrated
api/v2/users/route.ts # new route handlerIf a route exists in both, app/ wins in Next 13.4+. Use this to test side-by-side: keep the Pages version live, build the App version at a different path, switch when ready, delete the Pages version.
Migration order (recommended)
- Add
app/layout.tsxandapp/page.tsx— root layout + home. Smallest change to get App router live. - Migrate static pages (
/about,/contact) — no data fetching, simplest first. - Migrate API routes to route handlers (
pages/api/*→app/api/*/route.ts) one at a time. - Migrate dynamic Pages with
getServerSidePropsto Server Components withasyncdata fetching. - Migrate Pages with
getStaticProps/getStaticPathsto App router static rendering withgenerateStaticParams. - Migrate auth / middleware —
middleware.tsworks for both, but App router enables route-segment-level checks. - Delete Pages router files when all routes are migrated.
Data fetching translation
| Pages | App |
getServerSideProps | async function Page() Server Component with await fetch() (default) |
getStaticProps | async function Page() with fetch(url, { cache: 'force-cache' }) (default) |
getStaticPaths | export async function generateStaticParams() |
revalidate: 60 | fetch(url, { next: { revalidate: 60 } }) |
next/router useRouter | useRouter from next/navigation (different API!) |
next/head <Head> | metadata export or generateMetadata() |
Concrete example: SSR page
// pages/users/[id].tsx (Pages router)
export async function getServerSideProps({ params }) {
const user = await fetch(`https://api.example.com/users/${params.id}`).then(r => r.json());
return { props: { user } };
}
export default function UserPage({ user }) {
return <h1>{user.name}</h1>;
}// app/users/[id]/page.tsx (App router)
export default async function UserPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params; // params is a Promise in 15+
const user = await fetch(`https://api.example.com/users/${id}`).then(r => r.json());
return <h1>{user.name}</h1>;
}Notes:
- Server Component by default — runs on server, ships zero JS for this component.
paramsbecamePromisein Next 15 (await it).- No
getServerSidePropsboilerplate.
Static page
// app/blog/[slug]/page.tsx
export async function generateStaticParams() {
const posts = await fetch("https://cms.example.com/posts").then(r => r.json());
return posts.map((p: any) => ({ slug: p.slug }));
}
export default async function PostPage({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params;
const post = await fetch(`https://cms.example.com/posts/${slug}`).then(r => r.json());
return <article>{post.body}</article>;
}API route → route handler
// pages/api/users.ts
export default async function handler(req, res) {
if (req.method === "GET") {
const users = await db.user.findMany();
return res.json(users);
}
res.status(405).end();
}// app/api/users/route.ts
import { NextResponse } from "next/server";
export async function GET() {
const users = await db.user.findMany();
return NextResponse.json(users);
}
export async function POST(req: Request) {
const body = await req.json();
// ...
}route.ts exports per-method functions (GET, POST, PUT, DELETE, etc). Cleaner than the if-method dance.
Layouts (the killer feature of App router)
In Pages router, layout sharing was hacky (getLayout on the page component). App router has nested layouts:
// app/dashboard/layout.tsx
export default function DashboardLayout({ children }: { children: React.ReactNode }) {
return (
<div className="dashboard">
<Sidebar />
<main>{children}</main>
</div>
);
}All routes under /dashboard/* automatically use this layout. The layout doesn't re-render on navigation between sub-pages — state survives.
Client Components: the 'use client' boundary
Server Components are the default. The moment you need useState, useEffect, event handlers, or browser APIs:
// app/components/Counter.tsx
'use client';
import { useState } from "react";
export function Counter() {
const [n, setN] = useState(0);
return <button onClick={() => setN(n + 1)}>{n}</button>;
}Server Components can import Client Components freely (passes them as serialized boundaries). Client Components can't import Server Components directly — pass them via children.
Common gotchas
next/router→next/navigation— different API.useRouterfromnavigationdoesn't have.query; useuseSearchParamsanduseParamsseparately.<Head>→metadata— static metadata viaexport const metadata; dynamic viagenerateMetadata().getInitialProps— only used in custom_app/_document; no equivalent in App router. Migrate by moving logic to layouts.- CSS Modules + global CSS — work the same; just import in
layout.tsxinstead of_app.tsx. pages/_app.tsx— replace withapp/layout.tsx(root layout). Move providers there.pages/_document.tsx— replace with the<html>and<body>tags inapp/layout.tsx.- Auth via NextAuth.js v4 — works with both, but v5 (Auth.js) is preferred for App router.
getServerSidePropsreturningnotFound: true— replace withnotFound()fromnext/navigation(throws specific error caught bynot-found.tsx).- Streaming + Suspense — App router supports
<Suspense>natively. Old Pages router relied ondynamic(... { ssr: false })workarounds.
Performance differences
- First Load JS drops significantly when pages are Server Components.
- TTFB can be slower for SSR pages that are now streamed (intentional — first byte arrives sooner; later bytes stream).
- Static pages behave the same.
- Data fetching is automatically deduped in Server Components — calling
fetch(url)twice in a tree fetches once.
Anti-patterns
- Big-bang migration — too risky for any app with users. Coexist.
- Mixing client and server logic in one component — split. Server Component does data fetching; Client Component handles interactivity.
'use client'at the top of the entire app — defeats RSC. Push the boundary as deep as possible.getServerSidePropsleft inpages/during migration — fine, but plan its removal. Don't add new ones.- Forgetting to
await paramsin Next 15+ — runtime warning becomes an error. - Importing client-only libraries in Server Components — error at build time. Wrap in Client Component.
- Auth check in
middleware.tsonly — supplement with route-segment checks in App router for richer DX. - Moving everything to App router before testing — migrate one route, deploy, observe, then continue.
- Not testing the exact metadata output — App router's
metadatashape is different; SEO regressions silent. - Using
useEffectfor data fetching in App router — that's a Pages-router pattern. Use Server Components.
Verify it worked
- Both
pages/andapp/coexist; routes from both render correctly. - At least one route is migrated end-to-end (URL works, data fetched, rendering correct).
- First Load JS for a migrated route is smaller than the Pages-router version.
- Metadata (title, OG image) on migrated routes matches Pages router output.
- Auth still works on migrated routes (middleware applies; redirects correctly).
- No
getServerSideProps/getStaticPropsleft inapp/. - No
'use client'files importing fromnext/headersornext/cookies(server-only). - Streaming works on migrated SSR routes (visible via DevTools network tab — chunked response).
- Tests pass; visual regression tests pass.
- Production deploy: error rate flat or down; latency p95 stable or better.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

