Skip to content

Commit bc2c1ed

Browse files
committed
manifests and routing
1 parent 01d7e07 commit bc2c1ed

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

docs/develop/routing.md

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,10 @@ id: routing
66
import Tabs from '@theme/Tabs';
77
import TabItem from '@theme/TabItem';
88

9+
## Routing on Swarm
910

10-
Swarm's serverless architecture obviously doesn't support server-side routing, but there are several alternative approaches which work well for different circumstances: hash-based client-side routing, manifest-based clean URLs, or directory-style routing (also requires manifest manipulation). Each comes with its own trade-offs in simplicity, flexibility, and URL aesthetics. Here’s the quick overview before we dive into the details.
11+
Swarm does not behave like a traditional web server — there is **no server-side routing**, and every route must correspond to a real file inside the site [manifest](/docs/develop/manifests).
1112

12-
| Routing Type | How It Works | Pros | Cons |
13-
| --------------------------- | --------------------------------------------------------------- | ---------------------------------------------------------- | ------------------------------------------------------------ |
14-
| **Hash Routing** | Routes live after `/#/` and are handled entirely in the browser | Easiest to implement, zero manifest edits, highly reliable | URLs use `/#/`, not the cleanest visually |
15-
| **Manifest Clean URLs** | Add aliases or rewrite paths directly in the manifest | Real clean URLs like `/about`, flexible control | Requires manual manifest manipulation |
16-
| **Directory-Style Routing** | Use folder structures with `index.html` | Clean URLs with familiar static-site patterns | More structure in your build, still needs manifest awareness |
17-
18-
Now let’s look at each method, starting with the simplest and most Swarm-friendly option: hash-based client-side routing.
19-
20-
21-
## Client-Side Hash Routing
22-
23-
This section explains how to add hash based client side routing to your Swarm hosted site so that you can have clean URLs for each page of your website. See the [routing project in the examples repo](https://github.com/ethersphere/examples/tree/main/routing) for a full working example implementation.
24-
25-
### Why Hash Based Client Side Routing?
26-
27-
Swarm does not behave like a traditional web server — there is **no server-side routing**, and every route must correspond to a real file inside the site manifest.
2813
If you try to use typical "clean URLs" like:
2914

3015
```bash
@@ -41,12 +26,20 @@ contact
4126
dashboard/settings
4227
```
4328

44-
...which obviously don’t exist unless you manually manipulate the manifest.
45-
This is theoretically possible, but is tricky and complex to do manually, and there is currently not (yet) any tooling to make it easier.
29+
...which obviously don’t exist.
4630

47-
### How to Add Routing
31+
There are two main strategies for addressing routing:
4832

49-
If you want multiple pages on a Swarm-hosted website, you should use a client-side router. Swarm has no server backend running code and so can’t rewrite paths, so we use **React Router’s `HashRouter`**, which keeps all routing inside the browser.
33+
* **Hash-Based Client-Side Routing**
34+
* **Manifest-Based Routing with Aliases or Index Files**
35+
36+
Now let’s look at each method:
37+
38+
## Client-Side Hash Routing
39+
40+
This section explains how to add hash based client side routing to your Swarm hosted site so that you can have clean URLs for each page of your website. See the [routing project in the examples repo](https://github.com/ethersphere/examples/tree/main/routing) for a full working example implementation.
41+
42+
Swarm has no server backend running code and so can’t rewrite paths, so we can use **React Router’s `HashRouter`**, which keeps all routing inside the browser.
5043

5144
Below is the simplest way to set this up using **create-swarm-app** and then adding your own pages.
5245

@@ -303,11 +296,15 @@ You now have:
303296
* A React 404 for invalid hash paths
304297
* Stable, versioned deployments using feed manifests
305298

306-
## Add transition here
299+
## Manifest Based Routing
300+
301+
The second routing method involves directly manipulating the manifest so that routes resolve properly to the intended content. There are two primary approaches to manifest based routing:
302+
303+
* Alias based routing with arbitrary file names
304+
* Directory based with index files
307305

308-
## Website Routing
309306

310-
#### 1. Upload the Site
307+
### 1. Upload the Site
311308

312309
Start by uploading the site:
313310

@@ -325,11 +322,11 @@ Without manifest edits, routes only work via exact file paths like:
325322

326323
Trying to access `/about` or `/about/` will fail.
327324

328-
#### 2. Fix Routing With Manifest Manipulation
325+
### 2. Fix Routing With Manifest Manipulation
329326

330327
You can fix clean URLs using **two strategies**:
331328

332-
##### Strategy A: Add Aliases to the Manifest
329+
#### Strategy A: Add Aliases to the Manifest
333330

334331
```ts
335332
node.addFork('about', referenceForAbout, metadata)
@@ -338,7 +335,7 @@ node.addFork('about/', referenceForAbout, metadata)
338335

339336
Now `/about` and `/about/` work like `/about.html`.
340337

341-
##### Strategy B: Use Directory Index Files
338+
#### Strategy B: Use Directory Index Files
342339

343340
Restructure files like:
344341

@@ -355,7 +352,7 @@ node.addFork('about/', referenceForAboutIndex, metadata)
355352

356353
This gives you full directory-style clean URLs.
357354

358-
#### 3. Removal and Redirect
355+
## Remove and Redirect Routes
359356

360357
To "delete" a page you would need to remove all entries for it from the manifest to remove it entirely:
361358

@@ -399,7 +396,7 @@ Once you understand manifest-based routing, you can dynamically:
399396
* Add new paths (e.g. blog posts, product pages)
400397
* Create custom routes
401398
* Redirect old paths
402-
*
399+
* Remove unwanted paths
403400

404-
Next, learn how manifest manipulation plays a key role in enabling the [dynamic content](/docs/develop/dynamic-content) on Swarm. It will allow you to turn Swarm into a decentralized CMS and decouple your front end from your back end.
401+
Next, learn how to combine all the previously covered concepts to enable [dynamic content](/docs/develop/dynamic-content) on Swarm. It will allow you to turn Swarm into a decentralized CMS and decouple your front end from your back end.
405402

0 commit comments

Comments
 (0)