Skip to content

Commit 9267fc0

Browse files
committed
try to use new way
1 parent 96fca06 commit 9267fc0

File tree

1 file changed

+235
-1
lines changed

1 file changed

+235
-1
lines changed

docs/advanced/routing.mdx

Lines changed: 235 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,238 @@ title: Advanced Routing
33
description: "Learn how Docusaurus handles routing across docs, blog, and pages. Understand route structure, path mapping, and SPA transitions."
44
sidebar_label: Routing
55
tags: [routing, docusaurus, react-router, docs, pages, blog]
6-
---
6+
---
7+
8+
```mdx-code-block
9+
import Link from '@docusaurus/Link';
10+
import {useActiveDocContext} from '@docusaurus/plugin-content-docs/client';
11+
import {useLocation} from '@docusaurus/router';
12+
import BrowserOnly from '@docusaurus/BrowserOnly';
13+
```
14+
15+
Docusaurus uses a **single-page application (SPA)** routing system — meaning every route corresponds to **one component**.
16+
17+
In this guide, we’ll explore how Docusaurus manages routing for **Docs**, **Blog**, and **Pages**, and then dive deeper into how the routing system itself works behind the scenes.
18+
19+
---
20+
21+
## Routing in Content Plugins {#routing-in-content-plugins}
22+
23+
Each content plugin defines a `routeBasePath`, which tells Docusaurus **where to mount routes**.
24+
25+
- **Docs plugin:** `/docs` by default
26+
- **Blog plugin:** `/blog`
27+
- **Pages plugin:** `/`
28+
29+
Here’s how these routes connect in the URL hierarchy:
30+
31+
```mermaid
32+
graph LR;
33+
A(["https://codeharborhub.github.io/"])
34+
B(["/base-url/"])
35+
C(["/docs/"])
36+
D(["/blog/"])
37+
E(["/"])
38+
F["Docs Routes"]
39+
G["Blog Routes"]
40+
H["Page Routes"]
41+
A---B;
42+
B---C;
43+
B---D;
44+
B---E;
45+
C---F;
46+
D---G;
47+
E---H;
48+
```
49+
50+
When a user visits `/docs/configuration`, Docusaurus finds the `/docs` branch, and then loads the corresponding document route.
51+
52+
You can fully customize your route structure. For instance, setting `routeBasePath: '/'` in **Docs-only mode** removes the `/docs` prefix while keeping all other plugins intact.
53+
54+
---
55+
56+
## Pages Routing {#pages-routing}
57+
58+
**Pages** are the simplest form of routes — file paths directly map to URLs.
59+
For example:
60+
61+
```
62+
src/pages/about.mdx → /about
63+
src/pages/index.js → /
64+
```
65+
66+
- Markdown pages render using `@theme/MDXPage`.
67+
- React components render directly as route components.
68+
69+
See [Creating Pages](../guides/creating-pages.mdx#routing) for more details.
70+
71+
---
72+
73+
## Blog Routing {#blog-routing}
74+
75+
The blog plugin auto-generates several types of routes:
76+
77+
| Route Type | Example URL | Customizable Option | Component |
78+
| ------------------- | ----------------------------- | ------------------- | -------------------------- |
79+
| **Posts List** | `/`, `/page/2`, `/page/3` | `pageBasePath` | `@theme/BlogListPage` |
80+
| **Individual Post** | `/2025/10/08/launch-post` | `slug` front matter | `@theme/BlogPostPage` |
81+
| **Tags List** | `/tags` | `tagsBasePath` | `@theme/BlogTagsListPage` |
82+
| **Tag Pages** | `/tags/education`, `/tags/ai` | `permalink` in tag | `@theme/BlogTagsPostsPage` |
83+
| **Archive Page** | `/archive` | `archiveBasePath` | `@theme/BlogArchivePage` |
84+
85+
Each route is generated automatically, but you can override any path with custom front matter.
86+
87+
---
88+
89+
## Docs Routing {#docs-routing}
90+
91+
Docs routing is **hierarchical** and **versioned**. Each version has its own route tree, sidebar, and context.
92+
93+
For example:
94+
95+
```
96+
/docs/ → Current version
97+
/docs/next → Next version
98+
/docs/1.0.0 → Past version
99+
```
100+
101+
This allows seamless version switching while preserving sidebar state.
102+
103+
```mdx-code-block
104+
export const URLPath = () => <code>{useLocation().pathname}</code>;
105+
export const FilePath = () => {
106+
const currentVersion = useActiveDocContext('default').activeVersion.name;
107+
return <code>{currentVersion === 'current' ? './docs/' : `./versioned_docs/version-${currentVersion}/`}advanced/routing.md</code>;
108+
};
109+
```
110+
111+
This page, <BrowserOnly>{()=> <URLPath /> }</BrowserOnly>, is generated from <BrowserOnly>{()=> <FilePath />}</BrowserOnly>. The doc content is displayed inside `@theme/DocPage`, which manages layout, sidebar, and navigation.
112+
113+
---
114+
115+
## File Paths vs URL Paths {#file-paths-and-url-paths}
116+
117+
In Docusaurus, **file paths map to URL paths**, unless overridden using the `slug` front matter.
118+
119+
### Example Mapping
120+
121+
| File Path | URL Path |
122+
| ------------------------------ | ------------------------- |
123+
| `./docs/advanced/routing.md` | `/docs/advanced/routing` |
124+
| `./blog/2025-10-08-launch.mdx` | `/blog/2025/10/08/launch` |
125+
126+
### Rules for Markdown Links
127+
128+
- `@site` prefix → Asset file path
129+
- `http(s)://` prefix → External URL
130+
- No extension → URL path
131+
- `.md(x)` extension → Converts file path to URL
132+
- Other extensions → Treated as [assets](../guides/markdown-features/markdown-features-assets.mdx)
133+
134+
---
135+
136+
## Routes Become HTML Files {#routes-become-html-files}
137+
138+
Every route in Docusaurus compiles into a **static HTML file** during build. For instance, the route `/docs/advanced/routing` maps to:
139+
140+
```
141+
/build/docs/advanced/routing/index.html
142+
```
143+
144+
If `trailingSlash` is disabled, the same route becomes `routing.html`.
145+
146+
This allows hosting on any static server (like GitHub Pages or Vercel) — Docusaurus handles **server-side rendering → static HTML conversion** automatically.
147+
148+
### Example
149+
150+
```bash
151+
build/
152+
├── docs/
153+
│ └── advanced/
154+
│ └── routing/
155+
│ └── index.html # /docs/advanced/routing
156+
└── index.html # /
157+
```
158+
159+
When using a custom `baseUrl`, ensure assets resolve correctly (e.g., `/base/assets/js/...`).
160+
161+
---
162+
163+
## Generating and Accessing Routes {#generating-and-accessing-routes}
164+
165+
Use the `addRoute` lifecycle method to programmatically add routes:
166+
167+
```js title="plugin-example.js"
168+
actions.addRoute({
169+
path: "/custom",
170+
component: "@site/src/pages/CustomPage.js",
171+
});
172+
```
173+
174+
All routes are aggregated in `.docusaurus/routes.js`.
175+
You can inspect them in the [Debug Routes Panel](/__docusaurus/debug/routes).
176+
177+
### Accessing Routes in React
178+
179+
You can access route data using `@docusaurus/router`, a wrapper around React Router.
180+
181+
```jsx title="RouteInfo.js"
182+
import React from "react";
183+
import { useLocation } from "@docusaurus/router";
184+
185+
export function PageRoute() {
186+
const location = useLocation();
187+
return (
188+
<span>
189+
You are currently on <code>{location.pathname}</code>
190+
</span>
191+
);
192+
}
193+
```
194+
195+
```mdx-code-block
196+
<BrowserWindow>
197+
<span>
198+
You are currently on <code>{location.pathname}</code>
199+
</span>
200+
</BrowserWindow>
201+
```
202+
203+
---
204+
205+
## Escaping SPA Redirects {#escaping-from-spa-redirects}
206+
207+
Because Docusaurus is an SPA, it handles navigation through React Router. However, if you link to static HTML files that aren’t part of Docusaurus routes, use the special `pathname://` protocol to perform a **non-SPA redirect**.
208+
209+
```md
210+
- [pathname:///static-page](pathname:///static-page)
211+
```
212+
213+
<BrowserWindow>
214+
215+
- [`pathname:///static-page`](pathname:///static-page)
216+
217+
</BrowserWindow>
218+
219+
This ensures Docusaurus doesn’t attempt to render missing routes or show a 404 page.
220+
221+
You can also use `pathname://` for static assets:
222+
223+
```md title="my-doc.md"
224+
![Static image](pathname:///img/codeharborhub-banner.png)
225+
226+
[Download PDF](pathname:///files/tutorial.pdf)
227+
```
228+
229+
---
230+
231+
## Summary
232+
233+
- Docusaurus follows a **SPA routing system** built on React Router.
234+
- Every content plugin defines its **route base path**.
235+
- **Docs** support **nested and versioned** routes.
236+
- URLs map directly from files but can be customized using `slug`.
237+
- During build, routes become **static HTML files** for deployment.
238+
- Use `pathname://` for non-SPA links or static assets.
239+
240+
With this routing power, our **CodeHarborHub** site can scale efficiently — from a simple landing page to a complex multi-version documentation ecosystem.

0 commit comments

Comments
 (0)