Skip to content

Commit cf7e12e

Browse files
committed
[README] Update reference links, add more details on frontend dev
* add generate UI declarations
1 parent e709dd7 commit cf7e12e

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Henry Chan
3+
Copyright (c) 2023 Henry Chan
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
This project provides a simple starter template for Dfinity Internet Computer using Next.js framework as frontend.
44

5+
**The Most Recent Updates**
6+
7+
- NextJS 13.3
8+
- DFX 0.13.1
9+
- NodeJS 18.16
10+
511
**Backend**
612

713
- A simple greeting hello world canister written in Motoko
@@ -22,13 +28,13 @@ https://u4gun-5aaaa-aaaah-qabma-cai.raw.ic0.app
2228

2329
Install:
2430

25-
- NodeJS 16.\* or higher https://nodejs.org/en/download/
26-
- Internet Computer dfx CLI https://smartcontracts.org/docs/current/developer-docs/quickstart/local-quickstart/
31+
- NodeJS 18.\* or higher https://nodejs.org/en/download/
32+
- Internet Computer dfx CLI https://internetcomputer.org/docs/current/developer-docs/setup/install/
2733
- Visual Studio Code (Recommended Code Editor) https://code.visualstudio.com/Download
2834
- VSCode extension - Motoko (Recommended) https://marketplace.visualstudio.com/items?itemName=dfinity-foundation.vscode-motoko
2935

3036
```bash
31-
sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
37+
sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"
3238
```
3339

3440
Clone this Git repository:
@@ -55,7 +61,6 @@ dfx deploy
5561
npm run dev
5662
```
5763

58-
Open in Chrome the following URL to try the demo app:
5964
http://localhost:3000/
6065

6166
Cleanup - stop dfx server running in background:
@@ -76,17 +81,67 @@ Canister configurations are stored in dfx.json.
7681

7782
### Backend
7883

79-
Backend code is inside /backend/ written in [Motoko language](https://smartcontracts.org/docs/current/developer-docs/build/languages/motoko/). Motoko is a type-safe language with modern language features like async/await and actor build-in. It also has [Orthogonal persistence](https://smartcontracts.org/docs/current/developer-docs/build/languages/motoko/#orthogonal-persistence) which I find very interesting.
84+
Backend code is inside /backend/ written in [Motoko language](https://internetcomputer.org/docs/current/motoko/main/motoko-introduction). Motoko is a type-safe language with modern language features like async/await and actor build-in. It also has [Orthogonal persistence](https://internetcomputer.org/docs/current/motoko/main/motoko/#orthogonal-persistence) which I find very interesting.
8085

8186
Image canister is introduced from release v0.2.0. It makes use of orthogonal persistence through stable variables and provides functions for create, delete and get image. See /backend/service/Image.mo.
8287

8388
### Frontend
8489

85-
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 /ui/styles. React Components are stored in /ui/components
90+
Frontend code follows Next.js folder convention with /pages storing page React code, /public storing static files including images. This project uses CSS modules for styling which is stored in /ui/styles. React Components are stored in /ui/components
91+
92+
Entry page code is inside /pages/index.js where the magic starts. With the DFX UI declarations generated code, frontend can use RPC style call to server side actor and its functions without worrying about HTTP request and response parsing.
93+
94+
To generate UI declarations:
95+
96+
```
97+
dfx generate
98+
```
99+
100+
It will generate files in src/declarations for each canister. In our case, it is image, hello and hello_assets but we only need the backend canister image and hello UI declarations here.
101+
102+
The next step is to adapt it to work with Next.js.
103+
The final adapted code is in ui/declaration/hello/index.js.
104+
You can also follow the steps below to update it.
105+
106+
Basically, copy image.did.js and index.js from src/declarations/image/
107+
108+
```
109+
cp src/declarations/image/image.did.js ui/declarations/image/image.did.js
110+
cp src/declarations/image/index.js ui/declarations/image/index.js
111+
```
112+
113+
Repeat the same for hello.
114+
115+
```
116+
cp src/declarations/hello/hello.did.js ui/declarations/hello/hello.did.js
117+
cp src/declarations/hello/index.js ui/declarations/hello/index.js
118+
```
119+
120+
The next step is to update the canister ID env variable in each canister index.js to use NEXT_PUBLIC prefix so that NextJS can recognize when compiling it.
121+
122+
Open ui/declarations/hello/index.js and look for the line:
123+
124+
```
125+
export const canisterId = process.env.HELLO_CANISTER_ID;
126+
```
127+
128+
Update HELLO_CANISTER_ID to NEXT_PUBLIC_HELLO_CANISTER_ID:
129+
130+
```
131+
export const canisterId = process.env.NEXT_PUBLIC_HELLO_CANISTER_ID
132+
```
133+
134+
Also delete the export line at the bottom so that it won't create actor during NextJS server side compilation when you run in next dev mode.
135+
136+
```
137+
export const hello = createActor(canisterId);
138+
```
139+
140+
Repeat the same for ui/declarations/image/index.js.
86141

87-
Entry page code is inside /pages/index.js where the magic starts. 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.
142+
To see the final code, check the original ui/declarations in the Git repo.
88143

89-
Starting with DFX 0.8.0, we start using the DFX generated front end code located in .dfx/local/canisters/hello/index.js and adapt it to work with Next.js. The adapted code is in ui/declaration/hello/index.js .
144+
The generate UI declarations also support TypeScript if you prefer TypeScript.
90145

91146
We use a service locator pattern through actor-locator.js that will handle the dfx agent host using env var NEXT_PUBLIC_IC_HOST.
92147

@@ -175,24 +230,24 @@ Notice both files are identical if we want the Next.js dapp to interact with the
175230

176231
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).
177232

178-
**.env.ic** is included for deployment to Internet Computer ic network which would be covered below.
233+
**.env.icprod** is included for deployment to Internet Computer ic network which would be covered below.
179234

180235
## Deploy to IC Network Canister
181236

182237
The most exciting part is to deploy your Next.js / Internet Computer Dapp to production Internet Computer mainnet blockchain network.
183238

184239
To do that you will need:
185240

186-
- ICP tokens and convert it to [cycles](https://smartcontracts.org/docs/current/concepts/tokens-cycles/)
241+
- ICP tokens and convert it to [cycles](https://internetcomputer.org/docs/current/concepts/tokens-cycles/)
187242
- Cycles wallet
188243

189-
Follow the [Network Deployment](https://smartcontracts.org/docs/current/developer-docs/quickstart/network-quickstart/) guide to create a wallet.
244+
Follow the [Network Deployment](https://internetcomputer.org/docs/current/developer-docs/setup/cycles/cycles-wallet/) guide to create a wallet.
190245
Dfinity offers [free cycle](https://faucet.dfinity.org/) to developers.
191246

192247
Now, you can deploy your Next.js Dapp to Internet Computer IC network by adding **--network ic** to the dfx subcommand. We will first update our env var to point to IC network host. Then deploy the backend canister first, export Next.js static code and deploy frontend canister **hello_assets**.
193248

194249
```bash
195-
cp .env.ic .env.production
250+
cp .env.icprod .env.production
196251
dfx deploy --network ic
197252
```
198253

0 commit comments

Comments
 (0)