Next.js Pages and Routing
Next.js uses file-system routing, meaning the pages in your application are automatically determined by the file system structure; each file and folder corresponds to a route, without needing to explicitly configure each path as in traditional React routing, which makes the development process more intuitive and simple.
-
The Relationship Between Pages and Routes:In Next.js, each page is automatically mapped to a route by a file in the app/ or pages/ directory, and each file's path and filename determine that page's URL.
-
Basic Page Structure:In Next.js, a page is any .tsx or .js file in the app/ or pages/ directory; each page's file automatically becomes a route of the application.
Next.js's routing system is very flexible, supporting static pages, dynamic routes, and nested routes, enabling developers to easily implement complex application structures.
- File-System Routing: Pages and routes are mapped through the
app/orpages/directory's file structure. - Dynamic Route: You can create dynamic routes using square brackets (
[ ]) to handle dynamic parameters in the URL. - Nested Routing: You can create nested routes through subdirectories to implement multi-level route structures.
- Route Navigation: When using
next/linkcomponent implements navigation between pages.
Next.js 13 introduced the new app directory, which coexists with the traditional pages directory and provides more flexible routing and layout organization.
Assume the project directory structure is as follows:
app/
├── page.tsx # 对应根路径 /
├── about/
│ └── page.tsx # 对应 /about
├── [slug]/
│ └── page.tsx # 对应动态路径,如 /[slug]
page.tsxFile: Each page has apage.tsxfile, which is the component content for that route.- Dynamic Route: If the page path has dynamic parameters, you can use square brackets (
[ ]) to define dynamic routes. For example[slug]indicates that the path can accept any value asslugparameter was added.
Default Pages and Routes
Next.js automatically creates routes for files in your app/ or pages/ folder.
The following are some common routing examples:
app/page.tsxCorresponds to/path.app/about/page.tsxCorresponds to/aboutpath.app/[slug]/page.tsxCorresponds to/[slug], which matches any URL path, such as/post/1or/product/xyz.
Dynamic Route
Dynamic routing allows you to dynamically render pages based on different parts of the URL.
In Next.js, you can implement dynamic routing by creating folders or files with square brackets.
For example, suppose you want to render different content based on different article IDs; you can do this:
app/
├── [id]/
│ └── page.tsx # 动态路由,匹配任何 `/[id]` 的路径
[id] is a placeholder for dynamic routing; it can match any id parameter was added.
Get Dynamic Route Parameters
In Next.js, you can access dynamic route parameters via the useParams hook. For example:
Example
'use client'
;
import
{
useParams
}
from
'next/navigation'
;
export
default
function
Post
(
)
{
const
params
=
useParams
(
)
;
const
{
id
}
=
params
;
// Get dynamic route parameters id
return
<
div
Post ID
:
{
id
}
div
;
}
When a user visits /post/123, id is automatically assigned the value 123 and displayed on the page.
Nested Routing
Next.js also supports nested routing, that is, creating sub-routes under a route. For example:
app/
├── about/
│ ├── page.tsx # /about 路由页面
│ ├── team/
│ │ └── page.tsx # /about/team 子路由页面
│ └── contact/
│ └── page.tsx # /about/contact 子路由页面
This approach gives your application a hierarchical structure while preserving the readability of URL paths.
Custom Routing Rules
If you want to customize routing behavior, you can configure it via the next.config.js file. But for most common routing needs, Next.js already provides out-of-the-box functionality without extra configuration.
Special Pages and Routing Features
_app.jsand_document.js: In Next.js,_app.jsis the root component of the application, allowing you to provide shared layout and state for all pages._document.jsis mainly used to modify the HTML document structure (for exampleheadandbodytags)._error.js: You can customize the error page to handle 404 or other types of errors.
Example
Create a Static Page
Let's take creating a simple static page as an example: create an about/page.tsx file with the following content:
Example
export
default
function
AboutPage
(
)
{
return
<
h1
About Us
h1
;
}
When you visit the /about path, Next.js automatically renders this page.

Using Dynamic Routes
Suppose you want to render a page based on the article ID; create an app/[id]/page.tsx file with the following content:
Example
import
{
useParams
}
from
'next/navigation'
;
export
default
function
PostPage
(
)
{
const
{
id
}
=
useParams
(
)
;
return
<
h1
Post ID
:
{
id
}
h1
;
}
Visiting /123 will display "Post ID: 123".
Route Navigation
Next.js uses the component provided by next/link for route navigation. You can add links in a page to jump to other pages.
Example
import
Link from
'next/link'
;
export
default
function
HomePage
(
)
{
return
(
<
div
<
h1Welcome to Next.
js
!
h1
<
Link href
=
"/about"
About Us
Link
div
)
;
}
The component is used to navigate between pages while maintaining the single-page application (SPA) experience.