How to Build a Blog with Next.js and MDX
nextjsmdxblogtutorial

How to Build a Blog with Next.js and MDX

Learn how to create a modern, fast, and SEO-friendly blog using Next.js and MDX. This comprehensive guide covers everything from setup to deployment.

Author logo

Khaled Saifullah

8 min read

šŸ’” What You'll Learn

By the end of this tutorial, you'll have a fully functional blog with MDX support, SEO optimization, and modern styling.

šŸš€ Introduction

Building a blog with Next.js and MDX combines the power of React components with the simplicity of Markdown. This approach gives you the best of both worlds: easy content creation and dynamic interactivity.

In this comprehensive guide, we'll walk through creating a modern blog that's fast, SEO-friendly, and easy to maintain.

āš™ļø Project Setup

Let's start by creating a new Next.js project with TypeScript:

npx create-next-app@latest my-blog --typescript --tailwind --eslint --app

Next, install the necessary MDX dependencies:

npm install @next/mdx @mdx-js/loader @mdx-js/react @types/mdx

Make sure to use the App Router (--app flag) for the best experience with Next.js 13+ features.

šŸ“ MDX Configuration

Configure Next.js to handle MDX files by updating your next.config.js:

const withMDX = require('@next/mdx')({
  extension: /\.mdx?$/,
  options: {
    remarkPlugins: [],
    rehypePlugins: [],
  },
})

module.exports = withMDX({
  pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'],
})

This configuration allows Next.js to process both Markdown and MDX files as pages.

šŸ“ Blog Structure

Create a well-organized structure for your blog posts:

src/
ā”œā”€ā”€ app/
│   ā”œā”€ā”€ blog/
│   │   ā”œā”€ā”€ [slug]/
│   │   │   └── page.tsx
│   │   └── page.tsx
│   └── layout.tsx
ā”œā”€ā”€ content/
│   └── posts/
│       ā”œā”€ā”€ first-post.mdx
│       └── second-post.mdx
└── components/
    ā”œā”€ā”€ MDXComponents.tsx
    └── BlogPost.tsx

The [slug] folder creates a dynamic route that will handle individual blog posts.

This structure keeps your content separate from your application logic, making it easier to manage and maintain.

šŸŽØ Styling & Components

Create custom components for your MDX content to ensure consistent styling:

// components/MDXComponents.tsx
import type { MDXComponents } from 'mdx/types'

export function useMDXComponents(components: MDXComponents): MDXComponents {
  return {
    h1: ({ children }) => (
      <h1 className="text-4xl font-bold mb-6">{children}</h1>
    ),
    h2: ({ children }) => (
      <h2 className="text-3xl font-semibold mb-4 mt-8">{children}</h2>
    ),
    p: ({ children }) => (
      <p className="mb-4 leading-relaxed">{children}</p>
    ),
    ...components,
  }
}

This approach gives you full control over how your content is rendered while maintaining the simplicity of Markdown.

šŸš€ Deployment

Deploy your blog easily with Vercel:

  1. Push your code to GitHub
  2. Connect your repository to Vercel
  3. Deploy with zero configuration

šŸ’” Pro Tip

Vercel automatically optimizes your Next.js blog for performance and SEO.

šŸŽÆ Conclusion

You now have a fully functional blog with Next.js and MDX! This setup provides:

  • Fast performance with Next.js optimization
  • Easy content creation with MDX
  • SEO-friendly structure
  • Customizable styling and components
  • Simple deployment process

Start writing your first blog post and share your knowledge with the world!

Last updated on 10/12/2024

Khaled Saifullah - Software Engineer | Shopify & WordPress Developer