How to build your own static site generator

Static site generators have quickly grown to become the preferred way to create a simple website or documentation.

Unlink dynamic websites, static sites do not require any server-side programming and come with minimal server-side management. Also, static site hosting services such as GitHub pages and Netlify make it easy to host and manage them.

This is because static sites are mainly made up of HTML, CSS, and rarely Javascript.

👉 Static Site Generators (SSG)

The browser is only able to parse HTML, CSS, and Javascript. Even with dynamic websites, the backend code acts as a templating tool (somewhat like a mixer) that puts content into an HTML structure and then sends it over to the browser to be rendered.

With a static site generator, this process is done once during a build step and the HTML and CSS assets are stored which are sent to the browser whenever a request is made.

Hugo, Jeklly and Next.JS are examples of static site generators. You usually end up picking one based on your programming language preference or ease of use.

One of the challenges with picking any static site generator is that you have to structure your content and HTML template to suit the format of the tool. Not to mention the fact that there is a learning curve to each one. Some static site generators come with very limited features as well.

One of the beauties of a dynamic website is you have the option to create a web-based editor where a non-technical editor can write their content. Most static site generators however require some level of technical know-how to work with them.

👉 Creating your own static site generator

We already established the fact that dynamic sites, in the end, generate static assets to be sent to the browser, what if we can crawl our dynamic website once and then save all the assets to later be served to the browser?

This is not even a new concept, CDNs use this technique to cache dynamic websites and there are many tools out there to convert a dynamic website to a static one.

So what if you just create your usual dynamic website with the nice admin editor area where anyone can edit then run a tool like wget to download the static assets to be hosted on a server.

You get the benefit of working in whatever framework and programming language you love or choose. You can create whatever feature you desire and even better should you need to revert to running a dynamic version of your website may be because you now need a contact form in there or a comment section you already have one ready. If you started with a statically generated website and end up needing some dynamic element you either have to rely on APIs or rebuild your site into a dynamic one.

This truly frees you from having to decide as to which static site generator to use and being locked in for better or for worse. It also eliminates the need to introduce a new tool to your workflow.

My site https://eddymens.com is currently built using the approach I described. Its a Laravel app which I have on my local machine with a beautiful admin area and all. When am done with a post I just run the command below then upload the generated HTML files onto Netlify and am done!

01: wget -mpEk -nH  -P eddymens-static http://localhost:8000

Here is a link to explainshell detailing what each option flag does.

You might still need static site generators but at least now you know you have a second option 😊.