# How this site works

2026-12-02

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 with entirely different APIs.

Intentionally by design, there's absolutely no framework, just a simple custom builder, a few standard plugins, and some Nunjucks templates.

The stack:

- Content is written in [Contentful](https://www.contentful.com)
- Pages are rendered with [Nunjucks](https://mozilla.github.io/nunjucks/)
- Markdown is rendered with [markdown-it](https://github.com/markdown-it/markdown-it)
- Comments are powered by [giscus](https://giscus.app)
- Typeset in [Fira Mono](https://mozilla.github.io/Fira/)
- Hosted on [Render](https://render.com)
- Served through [Cloudflare](https://cloudflare.com)

The source is on [GitHub](https://github.com/nathanbuchar/n8.engineer).

## File tree

```
.
├── 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
```

## Build config

A build is defined in `config.js`. It exports only three things: a render function, a set of plugins, and an array of targets.

```js
export default {
  render,
  plugins: [
    clean('dist'),
    contentful({ 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`**: each plugin's `run` hook is called in sequence. 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`**: once all targets are rendered, each plugin's `done` hook is called (if it has one).

## Plugins

A plugin is a function that returns an object with up to two hooks. Most only export `run`:

```js
const posts = () => ({
  async run(ctx) {
    ctx.posts = await fetchPosts();
  },
});
```

Because plugins run in order, later ones can leverage the context modified by earlier ones. The ones this site uses (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

A target renders one template to one file. It can only access the properties of `ctx` it specifies in `include` (or `'*'` for everything), merged with `extraContext` (if defined):

```js
{
  template: 'journal.njk',
  dest: dist('journal/index.html'),
  include: ['pages', 'blurbs'],
}
```

A target can also be a function of `ctx` that return a target or array of targets. That's how one template becomes one page per post:

```js
(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:

```njk
{% 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](https://github.com/markdown-it/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:

```sh
npm run build   # run the build once
npm run server  # serve dist/ on http://localhost:3000
```

`build` takes two flags:

```sh
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](https://render.com/docs/deploy-hooks) which rebuilds the site with fresh data.
	