You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+36-28Lines changed: 36 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,15 @@
2
2
3
3
This project provides a simple starter template for Dfinity Internet Computer using Next.js framework as frontend.
4
4
5
-
**Backend**
6
-
* A simple greeting hello world canister written in Motoko
7
-
* ImageBucket canister written in Motoko with create image, delete image and getImageById
5
+
**Backend**
8
6
9
-
**Frontend**
10
-
* A simple React HTML form with name input, sending it to greet canister in backend and showing the returned result
11
-
* A Image Upload HTML form with Pick a Image button, upload the image to image canister in backend, loading the image back from canister and display it using useImageObject React Hook
7
+
- A simple greeting hello world canister written in Motoko
8
+
- ImageBucket canister written in Motoko with create image, delete image and getImageById
9
+
10
+
**Frontend**
11
+
12
+
- A simple React HTML form with name input, sending it to greet canister and showing the returned result
13
+
- A Image Upload HTML form with Pick a Image button, upload the image to image canister, loading the image back from canister and display it using useImageObject React Hook
sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
@@ -64,24 +66,27 @@ dfx stop
64
66
65
67
## Project Structure
66
68
67
-
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:
69
+
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 3 canisters:
68
70
69
-
- hello (backend)
70
-
- hello_assets (frontend)
71
+
- hello (backend)
72
+
- image (backend)
73
+
- hello_assets (frontend)
71
74
72
75
Canister configuration are stored in dfx.json.
73
76
74
77
### Backend
75
78
76
-
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.
77
-
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.
79
+
Backend code is inside /backend/ written 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.
80
+
81
+
Image canister is introduced from release v0.2.0. It makese use of orthogonal persistence through stable variable and provides functions for create, delete and get image. See /backend/service/Image.mo.
78
82
79
83
### Frontend
80
84
81
-
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.
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
86
+
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.
82
88
83
-
Starting with DFX 0.8.0, we start using the DFX generated front end code locateing in .dfx/local/canisters/hello/index.js
84
-
and adapt it to work with Next.js. The adapted code is in ui/declaration/hello/index.js .
89
+
Starting with DFX 0.8.0, we start using the DFX generated front end code locateing 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 .
85
90
86
91
We use a service locator pattern through actor-locator.js that will handle the dfx agent host using env var NEXT_PUBLIC_IC_HOST.
87
92
@@ -98,17 +103,22 @@ Calling hello actor:
98
103
constgreeting=awaithello.greet(name)
99
104
```
100
105
101
-
The beautiful part is you can invoke the hello actor greet function with async/await style as if they are on the same platform.
106
+
The beautiful part is you can invoke the hello actor greet function with async/await style as if they are on the same platform. For details, see React Components GreetingSection.js and ImageSection.js in /ui/components/.
102
107
103
108
Webpack configuration:
104
109
In Next.js, it's located in next.config.js.
105
110
111
+
## React Hook
112
+
113
+
By using React Hook with actor UI declaration, it can greatly simplify frontend dev. It encourages component based composable logic. A great example is useImageObject.js React Hook in /ui/hooks. Given a imageId, useImageObject can load the image binary and convert it to HTML image source object ready for use in <img>.
114
+
If you look closer, useImageObject.js depends on image-serivce.js which depends on actor-locator.js. When you open ImageSection.js, you can find how useImageObject is being used to greatly reduce the complexity and the underlying calls with Canister. This is the pattern I used very often in my Content Fly Dapp project.
115
+
106
116
## Backend dev
107
117
108
118
After marking changes in backend code e.g main.mo in /backend/service/hello, you can deploy it to the local DFX server using:
109
119
110
120
```bash
111
-
dfx deploy
121
+
dfx deploy hello
112
122
```
113
123
114
124
**hello** is the backend canister name defined in dfx.json.
@@ -119,7 +129,7 @@ Next.js developers are familar with the handy hot code deploy in Next.js dev env
119
129
120
130
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.
121
131
122
-
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.
132
+
One thing to note is we use Next.js static code export here for hosting in Internet Computer so we can't use any features of Next.js that require server side NodeJS. Potentially, there might be ways to use Internet Computer canister as backend while deploying Next.js dapp to a hosting like Vercel that supports NodeJS server in the future. Further research is needed on that aspect. However, if you do want to run everything decentralized on blockchain including the frontend, you would want to deploy the exported static code to Internet Computer as well.
123
133
124
134
## Deploy and run frontend in local DFX server
125
135
@@ -168,23 +178,21 @@ Note **NEXT_PUBLIC** is the prefix used by Next.js to make env vars available to
168
178
169
179
## Deploy to IC Network Canister
170
180
171
-
The most exciting part is to deploy your Next.js / Internet Computer Dapp to production Internet Computer IC blockchain network.
181
+
The most exciting part is to deploy your Next.js / Internet Computer Dapp to production Internet Computer mainnet blockchain network.
172
182
173
183
To do that you will need:
174
184
175
-
-ICP tokens and convert it to [cycles](https://sdk.dfinity.org/docs/developers-guide/concepts/tokens-cycles.html)
176
-
-Cycles wallet
185
+
- ICP tokens and convert it to [cycles](https://sdk.dfinity.org/docs/developers-guide/concepts/tokens-cycles.html)
186
+
- Cycles wallet
177
187
178
188
Follow the [Network Deployment](https://sdk.dfinity.org/docs/quickstart/network-quickstart.html) guide to create wallet.
179
189
Dfiniy offers [free cycle](https://faucet.dfinity.org/) to developers.
180
190
181
-
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 backend canister**hello** first, export Next.js static code and deploy frontend canister **hello_assets**.
191
+
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 backend canister first, export Next.js static code and deploy frontend canister **hello_assets**.
182
192
183
193
```bash
184
194
cp .env.ic .env.production
185
-
dfx deploy --network ic hello
186
-
npm run build
187
-
dfx deploy --network ic hello_assets
195
+
dfx deploy --network ic
188
196
```
189
197
190
198
Open Chrome and go to https://[canisterId].raw.ic0.app/
0 commit comments