About

This is a static site, generated by a minimal custom Node.js build system.

The source is on GitHub.

More on how this site works.

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:

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)
  1. run: all of the run plugin hooks are called in order. Plugins all share a single ctx object and modify it.
  2. render: each target renders a template with data from ctx and writes it to dist/.
  3. done: all of the done hooks 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):

  • clean deletes dist/ so every build starts fresh.
  • contentful fetches every entry of each content type and adds them to ctx (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.
  • copy copies src/static/ (CSS, JS, images) into dist/.
  • watch is the only plugin that uses done. After all targets have rendered, it watches src/ 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 master deploy automatically.
  • Content changes: When CMS content is published, Contentful calls a Render deploy hook which rebuilds the site with fresh data.

Comments