Frontend Development on the Internet Computer
Jan 15, 2021
Comment on current events
Before I get started, I'm writing this between January 6, when Trump supporters stormed the US Capitol, and January 20th, when Joe Biden is set to be inaugurated as president. As unsettling as it is to see unfold, it is a reminder that the United States is still suffering under institutions that coddle white supremacy and racial resentment, stoked by income inequality and unscrupulous politicians. I'd like to simply state that Black and Indigenous lives matter, LGBTQIA+ lives matter, and that systems of power that benefit some over the good of the many are wrong, even when I am part of the group benefiting.Curious about how frontend code works on the Internet Computer? Here's what I've learned after two weeks on the job.
What you get in the starter
If you're following the Quick Start guide, you'll get some useful instructions on how to install the
How your project looks immediately after init
Things will largely look familiar to you if you've been working with frontend code for a while. You've got
There is also a
// dfx.json
{
"canisters": {
"hello": {
"main": "src/hello/main.mo",
"type": "motoko"
},
"hello_assets": {
"dependencies": ["hello"],
"frontend": {
"entrypoint": "src/hello_assets/public/index.js"
},
"source": ["src/hello_assets/assets", "dist/hello_assets/"],
"type": "assets"
}
},
"defaults": {
"build": {
"packtool": ""
}
},
"dfx": "0.6.17",
"networks": {
"ic": {
"providers": ["https://gw.dfinity.network"],
"type": "persistent"
},
"local": {
"bind": "127.0.0.1:8000",
"type": "ephemeral"
}
},
"version": 1
}
Inside, you'll see that the template has provided you with two "canisters":
Those canisters point to two directories under your
// src/hello_assets/public/index.js
import hello from "ic:canisters/hello";
hello.greet(window.prompt("Enter your name:")).then(greeting => {
window.alert(greeting);
});
The
Build your starter
To get your starter running, you'll need to run a few commands. First, run
In the new window, you need to create your on a local version of the internet computer, then compile your code.
dfx canister create hello
dfx canister create hello_assets
dfx build
Now, you should see that you have a
new dfx folder
You'll see that the canisters compile into
This code is what enables the
If you look into the
If you're looking closely at
Still, you can navigate to your browser, pull up
- An asset canister bootstraps your identity with html that initializes a global
ic object and an anonymous identity. - That anonymous identity allows you to request static assets, including your JS bundle.
From there, you have a workflow that will allow you to run a compile job and deploy a JavaScript application to the Internet Computer. You can work with any JavaScript technology that compiles with WebPack, and build your application.
Wait, I have to manually build and deploy every time? That sounds slow and bad.
Yeah, I agree with you on that one, and I think we can make some big improvements. In my next post, I'll get into how you can use the