← back to blog

MDX in Next.js: the simplest blog setup that actually scales

Skip the CMS for now. MDX files in your repo get you 90% of what you need, version-controlled content, and zero hosting bill. Here's the full setup.

by Horizech Team3 min read

This blog runs on MDX. This very post is an MDX file, committed to a Git repo, rendered by Next.js 14.

If you're starting a developer blog — especially one where you're the only writer — this is the setup I'd recommend every time. Here's how to replicate it.

Why MDX over a CMS

The case for MDX is simple:

  • Zero cost. No monthly CMS bill. Hosting on Vercel's free tier is realistic for a new blog.
  • Version controlled content. Every post has a Git history. Rollback, branching, pull requests — your standard workflow applies to your writing.
  • MDX = Markdown + React. You can embed live components inside posts. Code demos, interactive widgets, whatever.
  • Co-located with code. The content and the renderer live in the same repo, deployed together.

The downside: there's no nice web UI. You write in your editor, commit, push. If that sounds fine, read on.

Project structure

/posts
  my-first-post.mdx
  another-post.mdx
/src
  /app
    /blog
      page.tsx         ← blog index
      /[slug]
        page.tsx       ← individual post
  /lib
    posts.ts           ← MDX utilities

The posts utility

The key file is lib/posts.ts. It reads MDX files from disk, parses frontmatter with gray-matter, and calculates reading time:

import fs from 'fs'
import path from 'path'
import matter from 'gray-matter'
import readingTime from 'reading-time'

const postsDir = path.join(process.cwd(), 'posts')

export function getAllPosts() {
  const files = fs.readdirSync(postsDir).filter(f => f.endsWith('.mdx'))
  return files.map(file => {
    const slug = file.replace('.mdx', '')
    const raw = fs.readFileSync(path.join(postsDir, file), 'utf8')
    const { data, content } = matter(raw)
    return { slug, ...data, readingTime: readingTime(content).text }
  }).sort((a, b) => new Date(b.date) - new Date(a.date))
}

Rendering a post

Use next-mdx-remote for server components:

import { MDXRemote } from 'next-mdx-remote/rsc'

export default function PostPage({ params }) {
  const post = getPostBySlug(params.slug)
  return (
    <article>
      <h1>{post.title}</h1>
      <MDXRemote source={post.content} />
    </article>
  )
}

That's essentially it. Add syntax highlighting with rehype-highlight, slug-based heading links with rehype-slug, and GitHub Flavored Markdown tables with remark-gfm.

A sample MDX frontmatter

Every post starts with a frontmatter block:

---
title: "Your post title"
date: "2025-05-01"
excerpt: "One sentence teaser shown in listings."
tags: ["nextjs", "tutorial"]
author: "Your Name"
coverEmoji: "🚀"
featured: false
---

The posts.ts utility reads all of these fields and makes them available as typed objects throughout the app.

When to migrate to a CMS

MDX scales well to 50–100 posts. Beyond that, or when you want:

  • A nice editing UI
  • Multiple authors without Git access
  • Scheduled publishing
  • Real-time preview

...it's time for a headless CMS. We're planning to migrate this blog to Horizech Workspace when the time comes, with a custom MDX import widget to make the migration easy.

For now: ship fast, iterate, and keep the infrastructure simple.


The full source code for this blog will be shared in a follow-up post. Subscribe below.

⚡
Written by Horizech Team

Part of the team at Horizech — we build Flutter apps, AI integrations, web platforms, and games. If you want this done for you, get in touch.

hire us →
← all posts