This is a static site generated by a minimal build system I wrote in Node.js. My goal was to have it be evergreen enough so that I can pick it back up again a year later and not have to pull my hair out upgrading a bunch of outdated libraries to new major versions that use entirely different APIs.
I very intentionally built this site without using a framework. Instead, it's just a custom lightweight builder, a few plugins, and some Nunjucks templates.
The stack:
- Content is written in Contentful
- Pages are rendered with Nunjucks
- Markdown is rendered with markdown-it
- Comments are powered by giscus
- Typeset in Fira Mono
- Hosted on Render
- Served through Cloudflare
The source is on GitHub.
Overview
At a high level, pages on this site are defined in the CMS and their content is written in markdown. The build system fetches the page data, renders each page, and then writes the static HTML files to an output directory. Most pages on this site are essentially just markdown files, like this one.
The file tree is below ↴
.
├── dist/ # build output
├── lib/
│ ├── plugins/ # clean, contentful, copy, watch, …
│ ├── builder.js # runs plugins, renders targets
│ ├── markdown.js # markdown renderer
│ └── nunjucks.js # nunjucks environment
├── scripts/
│ ├── build.js # npm run build
│ └── serve.js # npm run server
├── src/
│ ├── parts/ # nav, comments, …
│ ├── static/ # css, js, img
│ ├── layout.njk # common layout
│ └── page.njk # dynamic page template
└── config.js # build configuration
The Build
A build is defined in config.js. It exports only three things: a render function, a set of plugins, and an array of targets.
export default {
render,
plugins: [
clean('dist'),
cms({ client, sources }),
copy({
from: 'src/static',
to: 'dist',
}),
watch({
dir: 'src',
enabled: isDevelopment,
async handler(ctx, builder) {
await builder.build();
},
}),
],
targets: [/* … */],
};
Every build runs the same three steps, in order:
run(ctx) → render(targets) → done(ctx)
run: all of therunplugin hooks are called in order. Plugins all share a singlectxobject and modify it.render: each target renders a template with data fromctxand writes it todist/.done: all of thedonehooks are called in order.
Plugins
A plugin is a function that returns an object with at least one hook. Most export just run.
const posts = () => ({
async run(ctx) {
ctx.posts = await fetchPosts();
},
});
Because plugins run sequentially, those that are run later can leverage the context modified by the plugins which run earlier. This site uses the following plugins (in order):
cleandeletesdist/so every build starts fresh.contentfulfetches every entry of each content type and adds them toctx(ctx.pages,ctx.blurbs). In development it caches the response in.cache/for a day, so rebuilding doesn't clobber the API and eat into my free tier limits.copycopiessrc/static/(CSS, JS, images) intodist/.watchis the only plugin that usesdone. After all targets have rendered, it watchessrc/and rebuilds when something changes.
Targets
In its simplest form, a target defines only two things: a template and an output destination. Targets are rendered using the available context passed to the Nunjucks templates, and then the output is written to the specified output location. They can only access the properties of ctx it specifies in include (or '*' for everything), merged with extraContext (if defined):
{
template: 'journal.njk',
dest: dist('journal/index.html'),
include: ['posts'],
}
A target can also be a function. The function makes ctx available as it's first parameter, and the target may return another target or an array of targets. That's how I can use a single template to build multiple pages, which is great for blog posts.
(ctx) => ctx.pages.map((page) => [
{
template: 'page.njk',
dest: dist(`${page.fields.url}/index.html`),
include: '*',
extraContext: page.fields,
},
])
Templates
Pages are rendered with Nunjucks templates. Each page extends a common layout and inserts content into its blocks:
{% extends "layout.njk" %}
{% block content %}
{{ content | markdown | safe }}
{% endblock %}
Post content is written in Contentful as markdown. The markdown filter runs it through markdown-it with plugins for heading anchors, footnotes, superscript, KaTeX for math, a table of contents, and highlight.js for code.
Scripts
There are two npm scripts:
npm run build # run the build once
npm run server # serve dist/ on http://localhost:3000
build takes two flags:
npm run build -- --watch # rebuild when src/ changes
npm run build -- --no-cache # always fetch fresh data from Contentful
Deployment
I just push to origin master.
To deploy, Render runs npm run build and serves dist/.
There are two ways a deploy gets triggered:
- Code changes: Render is connected to GitHub, so any pushes to
masterdeploy automatically. - Content changes: When CMS content is published, Contentful calls a Render deploy hook which rebuilds the site with fresh data.
Comments