Hosting a static website on IPFS
In the previous blog post, I covered the basic principles of IPFS and its features. In this article, I’m going to explore a more advanced use case – hosting a static website on IPFS.
Before diving in, I’d like to briefly explain the difference between dynamic and static websites.
For dynamic websites, content is generated on the fly by a program. These programs are typically written in languages like PHP, JavaScript, Python, or Go. When a browser makes a request, a web server processes it and forwards it to the program, which then dynamically generates an HTML page and returns it to the user. This means that requests from different users to the same URL can result in different responses. This approach makes sense when the same URL needs to serve personalized content. For example, in social networks, the same news feed page shows different updates to different users.
For static websites, all HTML files are pre-generated and stored in the server’s file system. All requests are handled by the web server, just like with a dynamic site, but instead of running additional programs, the web server simply reads and returns HTML files directly from the disk. As a result, different users requesting the same URL receive exactly the same response. This is the simplest, fastest, and most reliable way to serve data, making it an excellent choice for blogs like this one (though there are far more complex examples of static websites out there).
IPFS is a distributed file storage system; it can’t serve dynamic websites, but it works fine for static ones. Essentially, a static website is just a directory with HTML, CSS, JS files, and images. To host such a site on IPFS, all you need to do is add the files to the system using a command like ipfs add ...
. However, there are a few nuances I’d like to cover.
To make a static website accessible via IPFS, follow these four steps:
- Run an ipfs daemon.
- Use relative links to local resources in HTML files.
- Add dnslink.
- Use IPNS and enable auto-refresh.
Actually, only the first two steps are required, but the remaining two make accessing the site more convenient. I’ll go into more detail about each step later in this blog post.