September 28, 2024
O. Wolfson
Currently Reading: Routing, from the Next.js Docs.
The bare bones of every web application is its routing system. This article introduces the fundamental concepts of web routing and the specific methodologies used in Next.js 14, focusing on its powerful and flexible routing system.
Before diving deeper, it's essential to understand common terms you will encounter in routing documentation:
Next.js 14 enhances its routing capabilities with the App Router, built on React Server Components. This router supports:
In Next.js, the app
directory works alongside the traditional pages
directory, allowing for incremental adoption. This setup means you can gradually transition routes from the pages
directory to take advantage of the new features without disrupting existing routes.
The App Router takes precedence over the Pages Router. Ensure that routes across directories do not resolve to the same URL path to prevent conflicts.
Next.js employs a file-system-based router where:
page.js
file.Each folder in a route corresponds to a segment in the URL path. For nested routes, folders are nested within each other. For example, a /dashboard/settings
route involves nesting two folders within the app
directory, each corresponding to a segment of the URL.
Next.js introduces special files within routes to handle various aspects of routing:
layout.js
: Shared UI for a segment and its children.page.js
: Defines the unique UI of a route, making it publicly accessible.loading.js
: Provides a loading UI for a segment.not-found.js
: Handles UI for unmatched routes.error.js
: Serves as a React error boundary.global-error.js
: Handles global errors.route.js
: Defines server-side API endpoints.template.js
: Used for re-rendering layout UI.default.js
: Acts as a fallback UI for parallel routes.Files can use .js
, .jsx
, or .tsx
extensions.
A static route is a predefined, fixed path in the URL, without any dynamic segments or variables. It always matches the same URL and does not change based on user input or variables.
Example: /about
matches only /about
and no other variants.
Folder Structure:
app
└── about
└── page.tsx
In this case, visiting the /about
path will render the content from the page.tsx
file located in the about
folder.
A nested static route contains multiple predefined URL segments, representing a hierarchy of static paths. Each segment corresponds to a folder in the file structure, and the nested folders represent deeper levels of the URL hierarchy.
Example: /dashboard/settings
matches only /dashboard/settings
and no other variants.
Folder Structure:
app
└── dashboard
└── settings
└── page.tsx
In this case, visiting the /dashboard/settings
path will render the content from the page.tsx
file located inside the settings
folder, which is nested within the dashboard
folder.
Next.js also supports advanced routing patterns such as:
With a foundational understanding of Next.js routing, you're now equipped to explore and implement complex routing patterns that enhance your application's navigation and structure. In Next.js with the app router, you can create several types of dynamic routes beyond the simple [slug]
pattern. Here are the various kinds of dynamic routing patterns you can utilize:
[slug]
)/blog/[slug]
matches /blog/hello-world
, /blog/another-post
, etc.app
└── blog
└── [slug]
└── page.tsx
[...slug]
)/docs/[...slug]
matches /docs/intro
, /docs/guides/installation
, or even /docs/guides/installation/linux
.app
└── docs
└── [...slug]
└── page.tsx
[[...slug]]
)Optional catch-all segments match zero or more URL segments. It’s like a catch-all route, but it also matches the base route.
Example: /docs/[[...slug]]
matches /docs
, /docs/intro
, or /docs/guides/installation/linux
.
Folder Structure:
app
└── docs
└── [[...slug]]
└── page.tsx
(auth)
)(auth)/login/page.tsx
renders a layout specific to authentication-related pages.app
└── (auth)
└── login
└── page.tsx
(group)
)(admin)/users/page.tsx
allows the grouping of admin-related routes, but the URL remains /users
.app
└── (admin)
└── users
└── page.tsx
@auth
, @marketing
)https://nextjs.org/docs/app/building-your-application/routing/parallel-routes
app/@auth/login/page.tsx
and app/@marketing/landing/page.tsx
can render in parallel in different parts of the layout.app
└── @auth
└── login
└── page.tsx
└── @marketing
└── landing
└── page.tsx
layout.tsx
file that wraps the page.tsx
file inside it.app
└── blog
├── layout.tsx
└── [slug]
└── page.tsx
/blog/[year]/[month]/[slug]
matches /blog/2024/09/hello-world
.With these patterns, Next.js provides a robust and flexible routing system, allowing for dynamic, nested, and deeply structured routes. Each pattern can be combined to build complex and scalable routing architectures while keeping code organized.