Skip to content

Server Side Includes

I was reading my RSS feed this morning when I came across this Lobsters discussion about HTML includes. Reading through the comments section, I discovered this really nifty technology called Server Side Includes. Turns out this is quite an old technology, and probably went out of style with the rise of full-blown server-side scripting languages. Admittedly it is quite limited in what it can do - for instance it doesn’t have loops. It does have basic logic using if statements though, and it can even execute CGI scripts. It happens to be supported natively by nginx, my web server of choice. It is also quite simple to use:

<!--#include file="header.html" -->

As you can see, they are just HTML comments, which means that they are completely compatible with HTML, and if anything goes wrong, they simply won’t render. You won’t get any weird error messages, and it won’t prevent the entire page from loading.

When I build websites for my clients, I typically subscribe to the YAGNI philosophy. Most of my websites start life as hand-written HTML and CSS files (perhaps with a bit of client-side JS if it’s needed). When things get beyond a few pages and I find myself doing a lot of copy-pasting, I’ll usually migrate to a Static Site Generator. When things start to get really complex - requiring server-side logic or databases for example - I’ll move to a server-rendered app running behind a reverse proxy. But each transition to the right adds significant complexity, and therefore I only make the jump when it is necessary.

The reason why I think Server Side Includes are so cool is that it moves the threshold for moving from hand-written HTML to using an SSG further to the right. And this is a good thing. Moving from hand-written HTML to using an SSG is a big jump because it introduces a compile step, which has a bunch of consequences. As a freelancer who manages many websites, every bit of complexity saved counts. So the more I can delay moving to the next stage in complexity, the better. And Server Side Includes do just that.

Websites where I might reach for an SSG just because there are multiple pages which need to include a common snippet of HTML such as a header or footer, can now be completely hand-written and compile-free again. Server Side Includes are very simple and limited in functionality, but that’s why they’re so useful. If I want looping, or complex logic, I’ll bite the bullet and use an SSG. But if I just want to avoid copy-pasting a few HTML snippets, Server Side Includes are the perfect tool for the job.

It often amazes me how old, forgotten technologies can actually be incredibly useful, and often dramatically simplify your workflow. It’s a lesson in stepping back and evaluating technology without the bias of norms and current trends. Often technological progress is accompanied by a large increase in complexity, and it’s important to step back and asses the trade-off.