By the end of this article, you’ll be able to take your single-page frontend app, and host it within the Inter Planetary File System (IPFS) network.
In our scenario, we’ll use Pinata, to keep our files alive (pinned) in IPFS. Our setup features a short-lived IPFS node, that is online only for a short period of time — until our deployment is finished — by successfully pinning our files with Pinata.
Pinata offers a free tier up to 1GB of pinned data
In theory, any. As long as they provide a static file output like Angular, React or Vue does, most likely through webpack or any other modern bundler.
Our deployment script is gonna use pinata-sdk for the pinning part, while the final example can be seen here.
We’ll use docker to reduce the environment setup hassle, let’s start by cloning the SDK’s repository.
Clone the pinata-sdk repository
Our repository features a pretty simple Dockerfile, which looks roughly like this
Make sure to use the latest version from your locally cloned repository
Let’s spin up a development container, where we can run our deployment scripts.
This will build an image using the Dockerfile we’ve seen previously, and start an interactive bash shell where we will continue with our example.
Interactive shell started successfully
Now that we have a nice and uniform development environment, let’s move forward.
Our app is generated with reason-react, and displays a greeting message when build successfully. You can find the actual app and the deployment script at pinata-sdk/examples/deployment-dapp-frontend.
How the app should look like if build successfully
You can build the app as shown below.
Webpack output in case of a successful build
Result of the build steps above, can be found at the build/ folder.
Now that we know how to build our frontend app, we’re ready to deploy it using our deployment script.
Our app will be hosted via the IPFS Network, let’s walk through what that means in practice.
We’ve built our app in the previous step, and as a result we’ve got the build/ folder, that should get deployed to the IPFS network to make our app available for others to use.
PinataSDK provides an example on how to do that rather easily, trough Pinata’s pinHashToIPFS API.
We will use a simple deployment script, the original source code can be found here. But we’ll walk through it in the tutorial.
We’ll start by registering an account with Pinata (or logging in if you already have one) so we can use the service for our deployment.
You can register your account here. Then head to the account section once logged in. You’ll see your API keys there.
Those are your API keys.
Save your keys under [examples/deployment-dapp-frontend/credentials.js](https://github.com/maht0rz/pinata-sdk/blob/master/examples/deployment-dapp-frontend/credentials.js)
in your editor.
Before we run the existing deployment script, which you can find at [examples/deployment-dapp-frontend/deploy.ts](https://github.com/maht0rz/pinata-sdk/blob/master/examples/deployment-dapp-frontend/deploy.ts)
, let’s walk through what it does.
pinata-sdk
with our two api keysjs-ipfs
, and wait for it to warm up for a predefined amount of time (e.g. 3 minutes)
First let’s edit out [src/Welcome.re](https://github.com/maht0rz/pinata-sdk/blob/master/examples/deployment-dapp-frontend/src/Welcome.re)
component, so we can be sure our deployed app, is really the one we have built locally.
Once we’ve done that, we can run our deployment script using npm run deploy
. After the script finishes, it will output the IPFS hash pinned at Pinata, which we can use to view our website.
Deployment successful!
You can see my deployment at: http://gateway.pinata.cloud/ipfs/QmNTrW433oWNG3iL3AqWjssJ88FLy3a4UL8Pkt6pnYDtzU/
Your very own DAPP’s frontend, deployed on the IPFS network
Got any questions? Trouble running the examples? You can reach out to me on telegram at t.me/maht0rz.