Slugs, Filenames, and the Art of Not Breaking Your Blog

June 3, 2025 (2mo ago)

"Why do I need a slug in my frontmatter if my file is already named my-cool-post.mdx?"

I asked myself this at 2am, right after my build broke for the third time because I renamed a file.
Let's talk about why slugs matter, and how to make your blog system both flexible and future-proof.


The "Just Use the Filename" Approach

It's tempting.
You name your file my-cool-post.mdx, and your blog loader just uses that as the slug.
Simple, right?

But then...

Suddenly, your URLs break.
Old links 404.
Google gets sad.
You get sad.


Enter: The slug Field

By adding a slug field to your frontmatter, you decouple the URL from the filename.

---
title: "My Cool Post"
slug: my-cool-post
---

Now you can:


The Best of Both Worlds: Slugify Fallback

But what if you forget to add a slug?
Or you're migrating old posts?

That's where the slugify fallback comes in:

This way, you get:


How My Loader Works (Nerd Flow)

Here's the nerdy bit:

+---------------------+
|   Read .mdx files   |
+---------------------+
           |
           v
+----------------------------+
|   Parse frontmatter/meta  |
+----------------------------+
           |
           v
     +--------------------+
     | Slug specified?    |
     +--------+-----------+
              | Yes                    | No
              v                        v
  +-------------------+     +----------------------+
  | Use frontmatter   |     |  Slugify filename    |
  | slug (safe/clean) |     |  (e.g., kebab-case)  |
  +-------------------+     +----------------------+
              \              /
               \            /
                v          v
           +------------------------+
           |   Map slug to route    |
           +------------------------+
                    |
                    v
           +------------------------+
           |  Serve at /blog/[slug] |
           +------------------------+

Result:
You can have files named 2024-06-05 My Cool Post.mdx with a slug of my-cool-post, and your URLs stay clean and unbreakable.


Why This Matters


TL;DR


"Build systems that work for you, not against you.
Slugify, decouple, and geek out."