Next.js App Router: File-System Based Routing
Next.js 13 introduced the App Router, a new paradigm for building React applications. A core feature of the App Router is its file-system based routing, which simplifies the creation of routes and layouts by leveraging the file structure of your project. This approach makes it intuitive to define pages, layouts, loading states, and error handling.
Understanding `page.js`
The
page.js
app
page.js
page.js
file in Next.js App Router?It exports the React component that renders the content for a specific route segment.
Leveraging `layout.js` for Shared UI
The
layout.js
Layouts enable shared UI across route segments.
A layout.js
file exports a React component that receives children
as a prop, representing the nested UI. This allows for common elements like navigation bars or footers to be rendered consistently across multiple pages within a route segment.
When you create a layout.js
file in a directory, it automatically becomes the default layout for that segment and all its children. The component exported from layout.js
must accept a children
prop, which will be the rendered page.js
or a nested layout.js
from a subdirectory. This creates a hierarchy of layouts, enabling you to build complex, nested UIs efficiently. For example, a root layout.js
in the app
directory can define the overall HTML structure, while a layout.js
in a /dashboard
directory can add dashboard-specific navigation.
Handling Loading States with `loading.js`
The
loading.js
loading.js
The loading.js
file exports a React component that serves as the loading UI. This component is displayed while the associated page or layout is loading, improving the perceived performance of your application.
Managing Errors with `error.js`
The
error.js
loading.js
error.js
The error.js
file exports a React component that accepts an error
object and a reset
function. The error
object contains details about the error that occurred, and the reset
function can be used to attempt to re-render the segment. This provides a structured way to handle runtime errors and offer recovery options to the user.
Text-based content
Library pages focus on text content
File-System Routing Conventions
The App Router follows specific file naming conventions to define routes and their associated behaviors. Understanding these conventions is key to effectively structuring your Next.js application.
File Name | Purpose | Example Usage |
---|---|---|
page.js | Defines the UI for a specific route segment. | Renders the main content of a page. |
layout.js | Defines shared UI for a route segment and its children. | Includes navigation, headers, footers. |
loading.js | Defines the loading UI for a route segment. | Shows a spinner or skeleton while data loads. |
error.js | Defines the error UI for a route segment. | Displays an error message and a retry button. |
Example Directory Structure
Consider this example directory structure within the
app
Loading diagram...
In this structure:
- is the homepage.code/app/page.js
- is the root layout.code/app/layout.js
- is the about page.code/app/about/page.js
- is a layout specific to the about section.code/app/about/layout.js
- is a layout for the dashboard section.code/app/dashboard/layout.js
- is the settings page within the dashboard.code/app/dashboard/settings/page.js
Learning Resources
The official and most comprehensive guide to the Next.js App Router, covering all aspects of file-system routing.
Detailed explanation of how routing works in the App Router, including conventions and best practices.
Learn how to implement loading states using the `loading.js` file and React Suspense.
Understand how to handle errors gracefully with the `error.js` file and React Error Boundaries.
An insightful article from Vercel explaining the motivations and benefits behind the App Router and its features.
A video tutorial demonstrating the core concepts of the Next.js App Router and file-system routing.
A more in-depth video exploring the nuances of the App Router, including layouts and routing patterns.
A comprehensive tutorial focusing on Next.js 14 features, with a strong emphasis on the App Router and its routing system.
A practical guide from freeCodeCamp explaining how file-based routing works in the Next.js App Router.
A focused video tutorial covering the essential files like `page.js`, `layout.js`, `loading.js`, and `error.js` within the Next.js App Router.