This template is designed to easily build applications deployed on ICP using Motoko + Next.js + Internet Identity
Create a .env file:
cp frontend/.env-codespaces-example frontend/.env
Get environment values:
# Create all canisters
dfx canister create --all
# Get backend canister id
dfx canister id test
# Get internet-identity canister id
dfx canister id internet-identity
# Get your Codespace name
echo $CODESPACE_NAME
Replace values in the frontend/.env
file:
# Replace YOUR_CODESPACE_NAME with your Codespace name
NEXT_PUBLIC_IC_HOST_URL=https://YOUR_CODESPACE_NAME-4943.app.github.dev/
# Replace YOUR_TEST_CANISTER_ID with your test canister id
NEXT_PUBLIC_TEST_CANISTER_ID=YOUR_TEST_CANISTER_ID
# Replace YOUR_INTERNET_IDENTITY_CANISTER_ID with your internet-identity canister id
NEXT_PUBLIC_INTERNET_IDENTITY_URL=https://YOUR_CODESPACE_NAME-4943.app.github.dev/?canisterId=YOUR_INTERNET_COMPUTER_CANISTER_ID
Generate did files:
dfx generate test
Deploy your canisters:
dfx deploy
You will receive a result similar to the following (ids could be different four you):
URLs:
Frontend canister via browser
frontend:
- http://127.0.0.1:4943/?canisterId=bkyz2-fmaaa-aaaaa-qaaaq-cai
- http://bkyz2-fmaaa-aaaaa-qaaaq-cai.localhost:4943/
internet-identity:
- http://127.0.0.1:4943/?canisterId=bd3sg-teaaa-aaaaa-qaaba-cai
- http://bd3sg-teaaa-aaaaa-qaaba-cai.localhost:4943/
Backend canister via Candid interface:
internet-identity: http://127.0.0.1:4943/?canisterId=br5f7-7uaaa-aaaaa-qaaca-cai&id=bd3sg-teaaa-aaaaa-qaaba-cai
test: http://127.0.0.1:4943/?canisterId=br5f7-7uaaa-aaaaa-qaaca-cai&id=be2us-64aaa-aaaaa-qaabq-cai
To interact with the frontend the url can be obtained as follows:
echo https://$CODESPACE_NAME-4943.app.github.dev/?canisterId=$(dfx canister id frontend)
Ensure the following are installed on your system:
Clone the project
git clone https://github.com/adrian-d-hidalgo/motoko-nextjs.git
Go to the project directory
cd motoko-nextjs
Install dependencies
npm install
Create a .env file:
cp frontend/.env-example frontend/.env
Start a ICP local replica:
dfx start --background --clean
Get your canister ids:
# Create canisters
dfx canister create --all
# Get backend canister id
dfx canister id test
# Get internet-identity canister id
dfx canister id internet-identity
Replace values in the .env file:
# Replace port if needed
NEXT_PUBLIC_IC_HOST_URL=http://localhost:4943
# Replace YOUR_TEST_CANISTER_ID with your test canister id
NEXT_PUBLIC_TEST_CANISTER_ID=YOUR_TEST_CANISTER_ID
# Replace YOUR_INTERNET_IDENTITY_CANISTER_ID with your internet-identity canister id
NEXT_PUBLIC_INTERNET_IDENTITY_URL=http://YOUR_INTERNET_IDENTITY_CANISTER_ID.localhost:4943
Generate did files:
dfx generate test
Deploy your canisters:
dfx deploy
You will receive a result similar to the following (ids could be different four you):
URLs:
Frontend canister via browser
frontend:
- http://127.0.0.1:4943/?canisterId=bkyz2-fmaaa-aaaaa-qaaaq-cai
- http://bkyz2-fmaaa-aaaaa-qaaaq-cai.localhost:4943/
internet-identity:
- http://127.0.0.1:4943/?canisterId=bd3sg-teaaa-aaaaa-qaaba-cai
- http://bd3sg-teaaa-aaaaa-qaaba-cai.localhost:4943/
Backend canister via Candid interface:
internet-identity: http://127.0.0.1:4943/?canisterId=br5f7-7uaaa-aaaaa-qaaca-cai&id=bd3sg-teaaa-aaaaa-qaaba-cai
test: http://127.0.0.1:4943/?canisterId=br5f7-7uaaa-aaaaa-qaaca-cai&id=be2us-64aaa-aaaaa-qaabq-cai
Open your web browser and enter the Frontend URL to view the web application in action.
Comment the next line into frontend/next.config.mjs
file:
// output: "export",
Then, navitate to frontend
folder:
cd frontend
Run the following script:
npm run dev