Next.js 13 introduced the new App Router built on top of React Server Components with support for layouts, nested routing, loading states, error handling, and more.
This page will introduce you to the fundamental concepts of the new routing model.
You will see these terms being used throughout the documentation. Here's a quick reference:
app
DirectoryThe new 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.
Good to know: 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. However, you can also use Client Components.
Recommendation: Check out the Server and Client Components page if you're new to Server Components.
app
In the app
directory:
page.js
file.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.js
, except a new component instance is mounted on navigation. Use layouts unless you need this behavior.loading.js
wraps a page or child segment in a React Suspense Boundary, showing the loading UI while they load.error.js
wraps a page or child segment in a React Error Boundary, showing the error UI if an error is caught.error.js
, but specifically for catching errors in the root layout.js
.notFound
function is thrown within a route segment.Good to know:
.js
,.jsx
, or.tsx
file 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.js
template.js
error.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 inside folders. For example, stylesheets, tests, components, and more.
Unlike the pages
directory which uses client-side routing, the new router in the app
directory uses server-centric routing to align with Server Components and data fetching on the server. With server-centric routing, the client does not have to download a route map and the same request for Server Components can be used to look up routes. This optimization is useful for all applications, but has a larger impact on applications with many routes.
Although routing is server-centric, the router uses client-side navigation with the Link Component - resembling the behavior of a Single-Page Application. This means when a user navigates to a new route, the browser will not reload the page. Instead, the URL will be updated and Next.js will only render the segments that change.
Additionally, as users navigate around the app, the router will store the result of the React Server Component payload in an in-memory client-side cache. The cache is split by route segments which allows invalidation at any level and ensures consistency across concurrent renders. This means that for certain cases, the cache of a previously fetched segment can be re-used, further improving performance.
Checkout the Linking and Navigating page to learn how to use the Link component.
When navigating between sibling routes (e.g. /dashboard/settings
and /dashboard/analytics
below), Next.js will only fetch and render the layouts and pages in routes that change. It will not re-fetch or re-render anything above the segments in the subtree. This means that in routes that share a layout, the layout will be preserved when a user navigates between sibling pages.
Without partial rendering, each navigation would cause the full page to re-render on the server. Rendering only the segment that’s updating reduces the amount of data transferred and execution time, leading to improved performance.
In the future, the Next.js Router will provide a set of conventions to help you implement more advanced routing patterns. These include:
These patterns and conventions will allow you to build richer, more complex UIs in your Next.js applications.