Skip to content

Commit 30d6995

Browse files
committed
bee-js instructions draft
1 parent f2a17f7 commit 30d6995

File tree

2 files changed

+192
-28
lines changed

2 files changed

+192
-28
lines changed

docs/develop/host-your-website.md

Lines changed: 189 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,36 @@ id: host-your-website
66
import Tabs from '@theme/Tabs';
77
import TabItem from '@theme/TabItem';
88

9-
This guide explains how to host a website on Swarm using `swarm-cli` and make it accessible through [Ethereum Name Service (ENS)](https://ens.domains/).
9+
There are two primary methods most developers should use for uploading a website to Swarm - `swarm-cli` and `bee-js`. Depending on the specific use case, it may make more sense to pick one or the other.
10+
11+
For a simple website such as a personal blog or company page, using `swarm-cli` is simplest and fastest way to get your site uploaded and accessible on Swarm.
12+
13+
However for developers who need finer grained control over the process or who wish to build a more complex application which require programmatically spinning up new pages, `bee-js` is required.
14+
15+
:::tip
16+
The guides below assume you already have a registered ENS domain name. By using an ENS domain name, you can make your Swarm hosted website accessible through an easy to remember human-readable name rather than a Swarm hash. If you don't have an ENS domain name registered, you can get one using the official ENS application at [app.ens.domains](https://app.ens.domains/). Refer to their support section for a step-by-step guide to [register an ENS domain](https://support.ens.domains/en/articles/7882582-how-to-register-a-eth-name).
17+
:::
18+
19+
:::tip FIX FOR ENS NOT WORKING ON LOCALHOST
20+
If the site doesn’t load from localhost, it’s probably an with the resolver RPC (the RPC endpoint for the Ethereum node used to resolve your ENS domain name).
21+
22+
Some endpoints, such as:
23+
24+
```
25+
https://cloudflare-eth.com
26+
```
27+
28+
may not resolve properly on localhost.
29+
30+
As of the writing of this guide, both of these free and public endpoints work reliably for localhost resolution:
31+
32+
```
33+
https://mainnet.infura.io/v3/<infura-api-key>
34+
https://eth-mainnet.public.blastapi.io
35+
```
36+
37+
Alternatively, you can run your own Ethereum node and use that as the RPC.
38+
:::
1039

1140
## Host a Site With **swarm-cli**
1241

@@ -18,15 +47,17 @@ This three part guide shows you how to get your website hosted on Swarm with jus
1847

1948
**Part three** shows you how to use feeds to make you website accessible at a static hash, and then use that hash to connect with your ENS domain. This is highly recommended for any site which will have future updates - without this step you would need to re-register your ENS domain every time you updated the site.
2049

21-
### 1. Upload & Access by Hash
22-
23-
#### Prerequisites
50+
### Prerequisites
2451

2552
* A running Bee node (either a [standard installation](/docs/bee/installation/quick-start) or [Swarm Desktop](/docs/desktop/install))
2653
* A valid postage batch
2754
* [`swarm-cli` installed](https://docs.ethswarm.org/docs/bee/working-with-bee/swarm-cli)
2855
* A valid postage stamp batch
2956
* Your static website files (you can also use the example website files provided below)
57+
* (Optional for part one - "Upload & Access by Hash") An ENS domain which you [previously registered](https://support.ens.domains/en/articles/7882582-how-to-register-a-eth-name)
58+
59+
### 1. Upload & Access by Hash
60+
3061

3162
You can download the example website files from the [ethersphere/examples](https://github.com/ethersphere/examples/tree/main/basic-static-website) repository.
3263

@@ -122,26 +153,6 @@ or through your own node:
122153
http://localhost:1633/bzz/yourname.eth/
123154
```
124155

125-
:::tip
126-
If the site doesn’t load from localhost, it’s probably an with the resolver RPC (the RPC endpoint for the Ethereum node used to resolve your ENS domain name).
127-
128-
Some endpoints, such as:
129-
130-
```
131-
https://cloudflare-eth.com
132-
```
133-
134-
may not resolve properly on localhost.
135-
136-
As of the writing of this guide, both of these free and public endpoints work reliably for localhost resolution:
137-
138-
```
139-
https://mainnet.infura.io/v3/<infura-api-key>
140-
https://eth-mainnet.public.blastapi.io
141-
```
142-
143-
Alternatively, you can run your own Ethereum node and use that as the RPC.
144-
:::
145156

146157
#### Using the Official ENS Guide
147158

@@ -326,4 +337,157 @@ swarm-cli feed upload .\website `
326337

327338
:::tip
328339
It may take a few minutes for caches to reload and make your changes accessible, especially with ENS gateway services like `eth.limo` and `bzz.link`.
329-
:::
340+
:::
341+
342+
## Host a Website with **bee-js**
343+
344+
This guide explains how to host a website on Swarm using the `bee-js` JavaScript SDK instead of the CLI.
345+
346+
For developers building apps, tools, or automated deployments, `bee-js` offers programmatic control over uploading and updating content on Swarm. This three part guide explains how to host your site on Swarm using `bee-js`:
347+
348+
1. Uploading a website and getting its Swarm hash.
349+
2. Registering the hash with your ENS domain.
350+
3. Using feeds to keep an ENS record up-to-date through a static reference.
351+
352+
### Prerequisites
353+
354+
* A running Bee node (either a [standard installation](/docs/bee/installation/quick-start) or [Swarm Desktop](/docs/desktop/install))
355+
* A valid postage stamp batch
356+
* Node.js (18+) and `@ethersphere/bee-js` installed in your project
357+
* Static website files (HTML, CSS, etc.) - feel free to use the [provided example site](https://github.com/ethersphere/examples/tree/main/basic-static-website)
358+
* (Optional for part one - "Upload & Access by Hash") An ENS domain which you [previously registered](https://support.ens.domains/en/articles/7882582-how-to-register-a-eth-name)
359+
360+
361+
### 1. Upload and Access by Hash
362+
363+
Install bee-js:
364+
365+
```bash
366+
npm install @ethersphere/bee-js
367+
```
368+
369+
Website upload script:
370+
371+
```js
372+
import { Bee } from "@ethersphere/bee-js";
373+
374+
const bee = new Bee("http://localhost:1633");
375+
const batchId = "<POSTAGE_BATCH_ID>"; // Replace with your batch ID
376+
377+
const result = await bee.uploadFilesFromDirectory(batchId, ".", {
378+
indexDocument: "index.html",
379+
errorDocument: "404.html"
380+
});
381+
382+
console.log("Swarm hash:", result.reference);
383+
```
384+
385+
After running the script, copy the Swarm hash output to the console and then use it to open:
386+
387+
```
388+
http://localhost:1633/bzz/<swarm-hash>/
389+
```
390+
391+
### 2. (Recommended) Connect Site to ENS Domain
392+
393+
*These steps are identical to
394+
395+
Once the site is uploaded, you can make it accessible via an easy to remember ENS domain name:
396+
397+
```
398+
https://yourname.eth.limo/
399+
https://yourname.bzz.link/
400+
```
401+
402+
or through your own node:
403+
404+
```
405+
http://localhost:1633/bzz/yourname.eth/
406+
```
407+
408+
409+
#### Using the Official ENS Guide
410+
411+
The ENS team provides a clear walkthrough with screenshots showing how to add a content hash to your domain with their [easy to use app](https://app.ens.domains/):
412+
413+
[How to add a Decentralized website to an ENS name](https://support.ens.domains/en/articles/12275979-how-to-add-a-decentralized-website-to-an-ens-name)
414+
415+
The guide covers:
416+
417+
* Opening your ENS domain in the ENS Manager
418+
* Navigating to the Records tab
419+
* Adding a Content Hash
420+
* Confirming the transaction
421+
422+
423+
#### Swarm-Specific Step
424+
425+
When you reach Step 2 in the ENS guide (“Add content hash record”), enter your Swarm reference in the following format:
426+
427+
```
428+
bzz://<swarm-hash>
429+
```
430+
431+
Example:
432+
433+
```
434+
bzz://cf50756e6115445fd283691673fa4ad2204849558a6f3b3f4e632440f1c3ab7c
435+
```
436+
437+
This works across:
438+
439+
* eth.limo and bzz.link
440+
* localhost (with a compatible RPC)
441+
* any ENS-compatible Swarm resolver
442+
443+
You do not need to encode the hash or use any additional tools. `bzz://<hash>` is sufficient.
444+
445+
### 3. Use Feeds for Seamless Updates
446+
447+
Feeds allow you to anchor an ENS domain to a static reference while changing the underlying content.
448+
449+
#### a) Generate a Feed Identity
450+
451+
```js
452+
import { PrivateKey } from "@ethersphere/bee-js";
453+
454+
const privateKey = new PrivateKey("0xYOUR_PRIVATE_KEY");
455+
const owner = privateKey.publicKey().address();
456+
```
457+
458+
#### b) Upload and Create Feed Manifest
459+
460+
```js
461+
import { Topic } from "@ethersphere/bee-js";
462+
463+
const topic = Topic.fromString("website");
464+
const writer = bee.makeFeedWriter(topic, privateKey);
465+
466+
const upload = await bee.uploadFilesFromDirectory(batchId, ".", {
467+
indexDocument: "index.html",
468+
errorDocument: "404.html"
469+
});
470+
471+
await writer.upload(batchId, upload.reference);
472+
473+
const manifestRef = await bee.createFeedManifest(batchId, topic, owner);
474+
console.log("Feed Manifest:", manifestRef);
475+
```
476+
477+
#### c) Set ENS Record to Feed Manifest
478+
479+
Set this in ENS as:
480+
481+
```
482+
bzz://<manifestRef>
483+
```
484+
485+
Future updates just re-run:
486+
487+
```js
488+
await writer.upload(batchId, newUpload.reference);
489+
```
490+
491+
Your ENS domain will always point to the latest upload via the feed manifest.
492+
493+
You’ve now got a programmatic way to deploy and update your Swarm-hosted site with ENS support using `bee-js`!

docusaurus.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
from: '/docs/api-reference/',
3030
},
3131
{
32-
to: '/docs/develop/access-the-swarm/host-your-website',
32+
to: '/docs/develop/host-your-website',
3333
from: '/docs/access-the-swarm/host-your-website',
3434
},
3535
{
@@ -41,7 +41,7 @@ module.exports = {
4141
from: '/docs/develop/access-the-swarm/keep-your-data-alive',
4242
},
4343
{
44-
to: '/docs/develop/access-the-swarm/upload-and-download',
44+
to: '/docs/develop/upload-and-download',
4545
from: '/docs/develop/access-the-swarm/upload-a-directory',
4646
},
4747
{
@@ -202,7 +202,7 @@ module.exports = {
202202
label: 'Getting Started'
203203
},
204204
{
205-
to: 'docs/develop/access-the-swarm/introduction',
205+
to: 'docs/develop/introduction',
206206
label: 'Building on Swarm'
207207
},
208208
{

0 commit comments

Comments
 (0)