The skeleton of every application is routing. This page will introduce you to the fundamental concepts of routing for the web and how to handle routing in Next.js.
First, you will see these terms being used throughout the documentation. Here's a quick reference:




In version 13, Next.js introduced a new App Router built on React Server Components, which supports shared layouts, nested routing, loading states, error handling, and more.
The App Router works in a new directory named app. The app directory works alongside the pages directory to allow for incremental adoption. This allows you to opt some routes of your application into the new behavior while keeping other routes in the pages directory for previous behavior. If your application uses the pages directory, please also see the Pages Router documentation.
Good to know: The App Router takes priority over the Pages Router. Routes across directories should not resolve to the same URL path and will cause a build-time error to prevent a conflict.


By default, components inside app are React Server Components. This is a performance optimization and allows you to easily adopt them, and you can also use Client Components.
Recommendation: Check out the Server page if you're new to Server Components.
Next.js uses a file-system based router where:
page.js file. See Defining Routes.Each folder in a route represents a route segment. Each route segment is mapped to a corresponding segment in a URL path.


To create a nested route, you can nest folders inside each other. For example, you can add a new /dashboard/settings route by nesting two new folders in the app directory.
The /dashboard/settings route is composed of three segments:
/ (Root segment)dashboard (Segment)settings (Leaf segment)Next.js provides a set of special files to create UI with specific behavior in nested routes:
layout |
Shared UI for a segment and its children |
page |
Unique UI of a route and make routes publicly accessible |
loading |
Loading UI for a segment and its children |
not-found |
Not found UI for a segment and its children |
error |
Error UI for a segment and its children |
global-error |
Global Error UI |
route |
Server-side API endpoint |
template |
Specialized re-rendered Layout UI |
default |
Fallback UI for Parallel Routes |
Good to know:
.js,.jsx, or.tsxfile extensions can be used for special files.
The React components defined in special files of a route segment are rendered in a specific hierarchy:
layout.jstemplate.jserror.js (React error boundary)loading.js (React suspense boundary)not-found.js (React error boundary)page.js or nested layout.js


In a nested route, the components of a segment will be nested inside the components of its parent segment.


In addition to special files, you have the option to colocate your own files (e.g. components, styles, tests, etc) inside folders in the app directory.
This is because while folders define routes, only the contents returned by page.js or route.js are publicly addressable.


Learn more about Project Organization and Colocation.
The App Router also provides a set of conventions to help you implement more advanced routing patterns. These include:
These patterns allow you to build richer and more complex UIs, democratizing features that were historically complex for small teams and individual developers to implement.
Now that you understand the fundamentals of routing in Next.js, follow the links below to create your first routes:
© 2024 Vercel, Inc.
Licensed under the MIT License.
https://nextjs.org/docs/app/building-your-application/routing