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
sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
37
+
sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"
32
38
```
33
39
34
40
Clone this Git repository:
@@ -55,7 +61,6 @@ dfx deploy
55
61
npm run dev
56
62
```
57
63
58
-
Open in Chrome the following URL to try the demo app:
59
64
http://localhost:3000/
60
65
61
66
Cleanup - stop dfx server running in background:
@@ -76,17 +81,67 @@ Canister configurations are stored in dfx.json.
76
81
77
82
### Backend
78
83
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.
80
85
81
86
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.
82
87
83
88
### Frontend
84
89
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/
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:
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.
86
141
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.
88
143
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.
90
145
91
146
We use a service locator pattern through actor-locator.js that will handle the dfx agent host using env var NEXT_PUBLIC_IC_HOST.
92
147
@@ -175,24 +230,24 @@ Notice both files are identical if we want the Next.js dapp to interact with the
175
230
176
231
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).
177
232
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.
179
234
180
235
## Deploy to IC Network Canister
181
236
182
237
The most exciting part is to deploy your Next.js / Internet Computer Dapp to production Internet Computer mainnet blockchain network.
183
238
184
239
To do that you will need:
185
240
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/)
187
242
- Cycles wallet
188
243
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.
190
245
Dfinity offers [free cycle](https://faucet.dfinity.org/) to developers.
191
246
192
247
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**.
0 commit comments