Why this blog reads from a headless CMS

Posts like this one are written in Directus and rendered by the website on the server. Here is how drafts stay private, and why raw HTML never reaches the page.

Metal letterpress type sorted into the compartments of a wooden case.

Photo by Natalia Y. on Unsplash

On this page4 sections
  1. The content model
  2. Drafts stay drafts
  3. No raw HTML
  4. Rendered on the server

We wanted writing a post to be as easy as writing a document, and publishing one to be safe by default. A headless CMS gives us both: an editor for writing, and an API for the website to read from.

Diagram of a post moving from the CMS, through a public read rule that only allows published, already-dated posts, to the server and then the page.
Diagram of a post moving from the CMS, through a public read rule that only allows published, already-dated posts, to the server and then the page.

We use Directus, running on its own with a Postgres database.

The content model#

There are three collections:

  • Posts: title, slug, status, publish date, summary, a Markdown body, a cover image with alt text, an author, and topics.
  • Authors: name, role, bio, and an optional photo.
  • Topics: a name and a slug, linked to posts many-to-many.

The whole model is created by a script that is checked into the repository, so a fresh CMS can be set up the same way every time.

Drafts stay drafts#

A post appears on the site only when two things are true: its status is Published, and its publish date has already passed.

That rule is enforced in two places. The CMS's public role is only allowed to read posts that match it, so even a direct request to the CMS API can't see a draft. The website's client applies the same filter again, so the site stays correct even if it is ever given a key that can see more.

No raw HTML#

Post bodies are written in Markdown. When the site renders them, any raw HTML in the source is ignored. A post can have headings, lists, links, quotes, code, and tables, but it can't inject a script or a tracking pixel into the page.

Rendered on the server#

The website asks the CMS for posts on the server, then sends finished HTML to your browser. The CMS address stays private, pages work without JavaScript, and search engines see the full article.

If the CMS is ever unreachable, the rest of the site keeps working, and the blog says so plainly instead of showing an empty page.

Filed under

Hexcomb LabsResearch, product, design, and engineering

Notes from the team building Hexcomb Labs in Kathmandu.