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
Enter the commands to start dfx local server in background:
37
+
Enter the commands to start dfx local server in background:
38
+
35
39
```bash
36
40
cd nextjs-ic-starter
37
41
dfx start --background
38
42
```
39
43
40
44
Note: If you run it in MacOS, you may be asked allow connections from dfx local server.
41
45
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
+
43
48
```bash
44
49
npm install
45
50
dfx deploy
@@ -49,47 +54,55 @@ npm run dev
49
54
Open in Chrome the following URL to try the demo app:
50
55
http://localhost:3000/
51
56
52
-
Cleanup - stop dfx server running in background:
57
+
Cleanup - stop dfx server running in background:
58
+
53
59
```bash
54
60
dfx stop
55
61
```
56
62
57
63
## Project Structure
64
+
58
65
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:
59
66
60
-
* hello (backend)
61
-
* hello_assets (frontend)
67
+
- hello (backend)
68
+
- hello_assets (frontend)
62
69
63
70
Canister configuration are stored in dfx.json.
64
71
65
-
### Backend
72
+
### Backend
73
+
66
74
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.
67
75
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.
68
76
69
77
### Frontend
78
+
70
79
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.
71
80
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 .
The beautiful part is you can invoke the hello actor greet function with async/await style as if they are on the same platform.
88
100
89
101
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.
91
103
92
104
## Backend dev
105
+
93
106
After marking changes in backend code e.g main.mo in /src/hello, you can deploy it to the local DFX server using:
94
107
95
108
```bash
@@ -99,14 +112,16 @@ dfx deploy hello
99
112
**hello** is the backend canister name defined in dfx.json.
100
113
101
114
## 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.
103
117
104
118
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.
105
119
106
120
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.
107
121
108
122
## 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:
110
125
111
126
```bash
112
127
dfx start --background
@@ -121,48 +136,42 @@ dfx deploy hello_assets
121
136
When it completes, you can open Chrome and browse to:
122
137
http://localhost:8000/?canisterId=[canisterId]
123
138
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:
125
140
126
141
```bash
127
142
dfx canister id hello_assets
128
143
```
129
144
130
145
## 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:
132
148
133
149
**.env.development** stores configs for use in local dev.
134
150
135
151
```
136
152
NEXT_PUBLIC_IC_HOST=http://localhost:8000
137
-
DFX_NETWORK=local
138
153
```
139
154
140
155
**.env.production** is used when building and exporting static code using **npm run buld**
141
156
142
157
```
143
158
NEXT_PUBLIC_IC_HOST=http://localhost:8000
144
-
DFX_NETWORK=local
145
159
```
146
160
147
161
Notice both files are identical if we want the Next.js dapp to interact with local dfx server.
148
162
149
-
**.env** stores the common configs used in all environments.
150
-
151
-
```
152
-
NEXT_PUBLIC_DFX_NETWORK=$DFX_NETWORK
153
-
```
154
-
155
163
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).
156
164
157
165
**.env.ic** is included for deployment to Internet Computer ic network which would be covered below.
158
166
159
167
## 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.
161
168
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:
163
172
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
166
175
167
176
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.
168
177
@@ -177,8 +186,8 @@ npm run build
177
186
dfx deploy --network ic hello_assets
178
187
```
179
188
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:
182
191
183
192
```bash
184
193
dfx canister --network ic id hello_assets
@@ -187,23 +196,26 @@ dfx canister --network ic id hello_assets
187
196
Congratulations !! Well Done !! 👏 🚀 🎉
188
197
189
198
## Troubleshooting
199
+
190
200
Use Chrome Dev Tools / Console / Network. Check if the dapp uses the right canister id and hostname.
191
201
192
202
## Tricks
203
+
193
204
To start local canister with no [artifical delay](https://sdk.dfinity.org/docs/release-notes/0.7.1-rn.html):
194
205
195
206
```bash
196
207
dfx start --no-artificial-delay --background
197
208
```
198
209
199
210
## Author
211
+
200
212
Henry Chan, henry@controlaltdevelop.com
201
213
Twitter: @kinwo
202
214
203
215
## Contributing
216
+
204
217
Pleaes feel free to raise issue or submit a pull request.
0 commit comments