Skip to content

Commit 46e4fa0

Browse files
committed
[README] Upgrade to dfx 0.8.0
1 parent 2dd525f commit 46e4fa0

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

README.md

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,43 @@ A simple greeting hello world actor function coded in Motoko.
88
**Frontend**
99
A simple React HTML form with name input, sending it to greet actor in backend and showing the returned result.
1010

11-
## Live Demo in IC Mainnet 🥳
11+
## Live Demo in IC Mainnet 🥳
12+
1213
https://u4gun-5aaaa-aaaah-qabma-cai.raw.ic0.app
1314

1415
![Screenshot](/public/demo-screenshot.png)
1516

1617
## Quick Start (Run locally)
18+
1719
Install:
1820

19-
* NodeJS 14.* or higher https://nodejs.org/en/download/
20-
* Internet Computer dfx CLI https://sdk.dfinity.org/docs/quickstart/local-quickstart.html
21-
* Visual Studio Code (Recommended Code Editor) https://code.visualstudio.com/Download
22-
* VSCode extension - Motoko (Recommended) https://marketplace.visualstudio.com/items?itemName=dfinity-foundation.vscode-motoko
21+
- NodeJS 14.\* or higher https://nodejs.org/en/download/
22+
- Internet Computer dfx CLI https://sdk.dfinity.org/docs/quickstart/local-quickstart.html
23+
- Visual Studio Code (Recommended Code Editor) https://code.visualstudio.com/Download
24+
- VSCode extension - Motoko (Recommended) https://marketplace.visualstudio.com/items?itemName=dfinity-foundation.vscode-motoko
2325

2426
```bash
2527
sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
2628
```
2729

28-
Clone this Git repository:
30+
Clone this Git repository:
31+
2932
```bash
3033
git clone https://github.com/dappblock/nextjs-ic-starter
3134
```
3235

3336
Open command terminal:
34-
Enter the commands to start dfx local server in background:
37+
Enter the commands to start dfx local server in background:
38+
3539
```bash
3640
cd nextjs-ic-starter
3741
dfx start --background
3842
```
3943

4044
Note: If you run it in MacOS, you may be asked allow connections from dfx local server.
4145

42-
Enter the commands to install dependencies, deploy canister and run Next.js dev server:
46+
Enter the commands to install dependencies, deploy canister and run Next.js dev server:
47+
4348
```bash
4449
npm install
4550
dfx deploy
@@ -49,47 +54,55 @@ npm run dev
4954
Open in Chrome the following URL to try the demo app:
5055
http://localhost:3000/
5156

52-
Cleanup - stop dfx server running in background:
57+
Cleanup - stop dfx server running in background:
58+
5359
```bash
5460
dfx stop
5561
```
5662

5763
## Project Structure
64+
5865
Internet Computer has the concept of [Canister](https://sdk.dfinity.org/docs/developers-guide/concepts/canisters-code.html) which is a computation unit. This project has 2 canisters:
5966

60-
* hello (backend)
61-
* hello_assets (frontend)
67+
- hello (backend)
68+
- hello_assets (frontend)
6269

6370
Canister configuration are stored in dfx.json.
6471

65-
### Backend
72+
### Backend
73+
6674
Backend code is inside /src/hello/main.mo writting in [Motoko language](https://sdk.dfinity.org/docs/language-guide/motoko.html). Motoko is a type-safe language with modern language features like async/await and actor build-in. It also has [Orthogonal persistence](https://sdk.dfinity.org/docs/language-guide/motoko.html) which I find it very interesting.
6775
Frontend code follows Next.js folder convention with /pages storing all React code, /public storing static files including images. This project uses CSS modules for styling which is stored in /styles.
6876

6977
### Frontend
78+
7079
Frontend code is inside /pages/index.js where the magic happens. With the generated code inside /.dfx, frontend can use RPC style call to server side actor and its functions without worrying about HTTP request and response parsing.
7180

81+
Starting with DFX 0.8.0, we start using the DFX generated front end code locateing in .dfx/local/canisters/hello/index.js
82+
and adapt it to work with Next.js. The adapted code is in ui/declaration/hello/index.js .
7283

73-
Initializing hello actor:
74-
```javascript
75-
import { Actor, HttpAgent } from '@dfinity/agent';
76-
import { idlFactory as hello_idl, canisterId as hello_id } from 'dfx-generated/hello';
84+
We use a service locator pattern through actor-locator.js that will handle the dfx agent host using env var NEXT_PUBLIC_IC_HOST.
7785

78-
const agent = new HttpAgent({ host: process.env.NEXT_PUBLIC_IC_HOST });
79-
const hello = Actor.createActor(hello_idl, { agent, canisterId: hello_id });
86+
Creating hello actor:
87+
88+
```javascript
89+
import { makeHelloActor } from "../ui/service/actor-adapter"
90+
const hello = makeHelloActor()
8091
```
8192

8293
Calling hello actor:
94+
8395
```javascript
84-
const greeting = await hello.greet(name);
96+
const greeting = await hello.greet(name)
8597
```
8698

8799
The beautiful part is you can invoke the hello actor greet function with async/await style as if they are on the same platform.
88100

89101
Webpack configuration:
90-
In the code above, the import module **'dfx-generated/hello'** is alias created dynamically through Webpack custom configuration. It will make reference to /.dfx/local/canisters/hello/hello.js or /.dfx/ic/canisters/hello/hello.js depending if you deploy to local DFX server or remote Internet Computer IC network. In Next.js, it's located in next.config.js. That is why before you run Next.js server with **npm run dev**, **dfx deploy** command must be run first in order to generate the required JavaScript code in /.dfx.
102+
In Next.js, it's located in next.config.js.
91103

92104
## Backend dev
105+
93106
After marking changes in backend code e.g main.mo in /src/hello, you can deploy it to the local DFX server using:
94107

95108
```bash
@@ -99,14 +112,16 @@ dfx deploy hello
99112
**hello** is the backend canister name defined in dfx.json.
100113

101114
## Frontend dev - Next.js Static Code
102-
Next.js developers are familar with the handy hot code deploy in Next.js dev environment when making changes in frontend code.
115+
116+
Next.js developers are familar with the handy hot code deploy in Next.js dev environment when making changes in frontend code.
103117

104118
After deploying your backend code as shown above, you can run Next.js local dev server **npm run dev** and edit your frontend code with all the benefits of hotcode deploy.
105119

106120
One thing to note is we use Next.js static code export here so we can't use any features of Next.js that require server side NodeJS. I think potentially there would be ways to use Internet Computer canister as backend while deploying Next.js dapp to a hosting like Vercel that supports NodeJS server. Further research is needed on that aspect.
107121

108122
## Deploy and run frontend in local DFX server
109-
In order to simulate the whole Internet Computer experience, you can deploy and run frontend code to local DFX server by running:
123+
124+
In order to simulate the whole Internet Computer experience, you can deploy and run frontend code to local DFX server by running:
110125

111126
```bash
112127
dfx start --background
@@ -121,48 +136,42 @@ dfx deploy hello_assets
121136
When it completes, you can open Chrome and browse to:
122137
http://localhost:8000/?canisterId=[canisterId]
123138

124-
Replace [canisterId] with the hello_assets canister ID which you can find by running:
139+
Replace [canisterId] with the hello_assets canister ID which you can find by running:
125140

126141
```bash
127142
dfx canister id hello_assets
128143
```
129144

130145
## Environment Configuration
131-
There are three key configs following Next.js [Environment Variables](https://nextjs.org/docs/basic-features/environment-variables) configuration:
146+
147+
There are three key configs following Next.js [Environment Variables](https://nextjs.org/docs/basic-features/environment-variables) configuration:
132148

133149
**.env.development** stores configs for use in local dev.
134150

135151
```
136152
NEXT_PUBLIC_IC_HOST=http://localhost:8000
137-
DFX_NETWORK=local
138153
```
139154

140155
**.env.production** is used when building and exporting static code using **npm run buld**
141156

142157
```
143158
NEXT_PUBLIC_IC_HOST=http://localhost:8000
144-
DFX_NETWORK=local
145159
```
146160

147161
Notice both files are identical if we want the Next.js dapp to interact with local dfx server.
148162

149-
**.env** stores the common configs used in all environments.
150-
151-
```
152-
NEXT_PUBLIC_DFX_NETWORK=$DFX_NETWORK
153-
```
154-
155163
Note **NEXT_PUBLIC** is the prefix used by Next.js to make env vars available to client side code through [build time inlining](https://nextjs.org/docs/basic-features/environment-variables).
156164

157165
**.env.ic** is included for deployment to Internet Computer ic network which would be covered below.
158166

159167
## Deploy to IC Network Canister
160-
The most exciting part is to deploy your Next.js / Internet Computer Dapp to production Internet Computer IC blockchain network.
161168

162-
To do that you will need:
169+
The most exciting part is to deploy your Next.js / Internet Computer Dapp to production Internet Computer IC blockchain network.
170+
171+
To do that you will need:
163172

164-
* ICP tokens and convert it to [cycles](https://sdk.dfinity.org/docs/developers-guide/concepts/tokens-cycles.html)
165-
* Cycles wallet
173+
- ICP tokens and convert it to [cycles](https://sdk.dfinity.org/docs/developers-guide/concepts/tokens-cycles.html)
174+
- Cycles wallet
166175

167176
Dfiniy will offer [free cycle](https://dfinity.org/developers/) to developers soon at the time of writting. In the meantime, you can buy ICP from [crypto exchanges](https://coinmarketcap.com/currencies/internet-computer/markets/) like what I did and transfer the ICP tokens to your wallet.
168177

@@ -177,8 +186,8 @@ npm run build
177186
dfx deploy --network ic hello_assets
178187
```
179188

180-
Open Chrome and go to https://[canisterId].raw.ic0.app/
181-
Replace [canisterId] by the hello_assets canister id in IC network. You can find it by running:
189+
Open Chrome and go to https://[canisterId].raw.ic0.app/
190+
Replace [canisterId] by the hello_assets canister id in IC network. You can find it by running:
182191

183192
```bash
184193
dfx canister --network ic id hello_assets
@@ -187,23 +196,26 @@ dfx canister --network ic id hello_assets
187196
Congratulations !! Well Done !! 👏 🚀 🎉
188197

189198
## Troubleshooting
199+
190200
Use Chrome Dev Tools / Console / Network. Check if the dapp uses the right canister id and hostname.
191201

192202
## Tricks
203+
193204
To start local canister with no [artifical delay](https://sdk.dfinity.org/docs/release-notes/0.7.1-rn.html):
194205

195206
```bash
196207
dfx start --no-artificial-delay --background
197208
```
198209

199210
## Author
211+
200212
Henry Chan, henry@controlaltdevelop.com
201213
Twitter: @kinwo
202214

203215
## Contributing
216+
204217
Pleaes feel free to raise issue or submit a pull request.
205218

206219
## License
207-
MIT
208-
209220

221+
MIT

0 commit comments

Comments
 (0)