Migrating Pages to Next.js App Router Without Downtime
Learn a safe, incremental strategy for migrating large Next.js codebases from the Pages router to the App router without risking production stability.
Summarize this Blog with AI
On this page
Migrating a large production codebase from the Next.js Pages router to the App router can easily destabilize your application if approached as a single rewrite. Because both routing paradigms can coexist in the same project, you do not have to move everything over at once. By adopting an incremental migration strategy, your team can ship changes continuously while gradually converting routes.
This guide covers how to structure your directory, share layouts safely, and rewrite paths without exposing broken views to users. You will learn how to verify your routing boundaries and establish a clean fallback strategy during the transition.
Understanding Path Precedence in Coexisting Routers
Next.js evaluates routing paths based on a specific resolution order when both routers are active. Understanding this resolution prevents accidental routing collisions where a Pages route intercepts a request intended for the App router, or vice versa.
The App router always takes precedence over the Pages router for matching file paths. If you have both pages/dashboard.tsx and app/dashboard/page.tsx, Next.js serves the App router version. However, shared roots like pages/index.tsx and app/page.tsx will conflict immediately and throw build errors.
Follow these rules to avoid path collision errors during migration:
- Never create an App router file that matches an existing active Pages route unless you are ready to delete the Pages file.
- Migrate leaf nodes, such as deeply nested settings or detail pages, before attempting to migrate root layouts.
- Keep a shared mapping document that tracks which routes live in the Pages directory and which have moved to the App directory.
Refactoring Shared Layouts and Global State
The Pages router relies on a single _app.tsx file for wrapping every page with providers, global styles, and persistent navigation. The App router replaces this model with nested layouts that apply specifically to route segments. Moving state management across this boundary requires careful planning.
State providers that rely strictly on React context must be wrapped inside client components when used in the App router. Because App router pages default to server components, placing a context provider at the root layout requires the 'use client' directive at the top of that layout file.
'use client';
import { ThemeProvider } from '@/context/theme';
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
{children}
);
}Evaluate your providers before moving them. If a provider only serves a subset of pages, restrict it to an App router segment layout rather than polluting the global root.
Rewriting Routes Incrementally with Next.js Config
When migrating large applications, certain complex paths may need to remain on the Pages router while you update surrounding sections. You can use Next.js rewrites in your configuration file to explicitly direct traffic or handle transitional redirects.
If you need to temporarily route specific legacy subpaths back to the Pages directory while the parent directory runs on the App router, configure your redirects carefully. However, relying too heavily on config-level rewrites can obscure your directory structure. Use rewrites only as a temporary safety net for legacy URLs that cannot be refactored immediately.
Verification and Testing Checklist
Before deploying any migrated segment to production, run through a strict verification checklist to catch subtle regressions in data fetching and rendering behavior.
- Verify that server-side data fetching functions like
getServerSidePropshave been correctly rewritten into async React server component data calls. - Check that meta tags, titles, and OpenGraph data are correctly generated using the App router metadata object export instead of
next/head. - Test all authentication cookies and authorization headers within the new layout structure to ensure middleware intercepts requests accurately.
- Audit client-side navigation transitions to ensure links using
next/linkresolve to the correct router without full page reloads.
Frequently asked questions

Dipen Nagani
Senior UI/UX Designer
As a User Experience Designer at Seven Square®, I contribute to enhancing digital experiences by leveraging my skills in UI/UX, CSS, and GSAP. My focus is on creating intuitive and visually engaging user interfaces that align with user needs and business goals.



