Back to blogs
4 Ways to Add a Blog to Your Next.js Site: A Guide for Developers

4 Ways to Add a Blog to Your Next.js Site: A Guide for Developers

NextJS 2025-02-12

Next.js has become one of the most popular frameworks for building fast, scalable, and SEO-friendly web applications. Whether you’re building a personal portfolio, a business website, or a content-driven site, integrating a blog into your Next.js project is a great way to engage with your audience and boost your site's SEO.

In this post, we’ll explore several ways to add a Next.js blog to your website, from using static files to integrating with a Next.js CMS or a headless CMS. These methods will help you improve blog SEO, drive more traffic, and enhance the performance of your site.

1. Static Markdown Files for Your Next.js Blog

One of the simplest and most efficient ways to add a Next.js blog is by using static Markdown files. This method is especially useful for smaller blogs where content doesn't change frequently. Since the content is static, you can serve the blog posts as static HTML pages, which improves both performance and SEO.

How to Set It Up:

  • Create a Blog Folder: In your pages directory, create a folder called blog (or any name you prefer).

  • Use Markdown Files: Store your blog posts as Markdown files in a dedicated folder (e.g., content/posts). Each Markdown file will represent one blog post.

  • Install a Markdown Parser: To render the Markdown content into HTML, use libraries like remark, remark-html, and gray-matter for parsing the Markdown content and metadata like the title, date, and slug.

  • Dynamic Routing for Posts: Utilize Next.js’s dynamic routing feature to create a page for each blog post based on the file name or slug.

This approach is ideal for websites that prioritize performance and want to keep things simple with minimal external dependencies, all while making the site SEO-friendly through static generation.

Example Code Snippet:

// pages/blog/[slug].js

import fs from 'fs';
import path from 'path';
import { parse } from 'gray-matter';
import { remark } from 'remark';
import html from 'remark-html';

const Post = ({ post }) => {
  return (
    <article>
      <h1>{post.title}</h1>
      <div dangerouslySetInnerHTML={{ __html: post.content }} />
    </article>
  );
};

export async function getStaticPaths() {
  const files = fs.readdirSync(path.join(process.cwd(), 'content/posts'));
  const paths = files.map((file) => ({
    params: { slug: file.replace(/\.md$/, '') },
  }));

  return { paths, fallback: false };
}

export async function getStaticProps({ params }) {
  const filePath = path.join(process.cwd(), 'content/posts', `${params.slug}.md`);
  const fileContent = fs.readFileSync(filePath, 'utf-8');
  const { data, content } = parse(fileContent);
  const processedContent = await remark().use(html).process(content);

  return {
    props: {
      post: {
        title: data.title,
        content: processedContent.toString(),
      },
    },
  };
}

export default Post;

By using this approach, your Next.js blog will be rendered at build time, ensuring fast page loads and better blog SEO

2. Integrating a Next.js CMS (Headless CMS)

For more dynamic and scalable content management, integrating a Next.js CMS or headless CMS can be a game-changer. A headless CMS separates the content from the presentation layer, giving you greater flexibility in how you manage and serve content. This setup is perfect for teams or solo developers who want an easy-to-use content management system without sacrificing performance.

Popular headless Next.js CMS options include:

These CMS platforms allow you to define your blog post content types and access the data via APIs. By combining a Next.js CMS with Next.js’s static generation, you can create a fast, SEO-friendly blog.

Setting up EasyBlog CMS:

  • Sign up for a free account at https://app.easyblog.io/

  • Generate a secret key to authenticate your application

  • Add the code snippet to your application

// your-project/app/blogs/page.tsx

import { NextBlogListPage } from '@weekend-studio/easyblog-next/app';

const config = {
  apiKey: process.env.EASYBLOG_API_KEY!,
  projectId: process.env.EASYBLOG_PROJECT_ID!,
  apiUrl: process.env.EASYBLOG_API_URL!
};

const easyblogOptions = { // Customize based on your need
  showThumbnail: true,
  showReadingTime: true,
  showExcerpt: true,
  showTags: true,
  showDate: true,
  showAuthor: true,
  showCategory: true,
}

export default async function BlogPage({
  searchParams
}: {
  searchParams: Promise<{ [key: string]: string | string[] | undefined }>
}) {
  const resolvedParams = await searchParams;
  
  return (
      <NextBlogListPage 
        config={config}
        type="grid"
        darkMode={true}
        searchParams={resolvedParams}
        displayOptions={easyblogOptions}
      />
  );
}

This method allows you to manage blog content easily through an admin panel, without having to worry about writing custom back-end code or managing databases.

By integrating a EasyBlog CMS, you can efficiently manage your blog’s content, and with Next.js’s SEO optimizations, you’ll have a lightning-fast and SEO-friendly blog.

3. Traditional CMS (e.g., WordPress + Next.js)

If you’re already using a traditional CMS like WordPress for your website, you can still integrate it with Next.js via the WordPress REST API. This allows you to leverage the content management capabilities of WordPress while taking full advantage of Next.js’s static generation and performance benefits.

This approach is ideal if you have an existing WordPress blog and want to migrate it to Next.js without losing SEO rankings or content.

How to Set It Up:

  • Enable WordPress REST API: Ensure that the WordPress REST API is enabled on your WordPress website.

  • Fetch Posts Using REST API: Use Next.js’s getStaticProps to fetch blog posts from WordPress via the API.

  • Static Page Generation: Generate static pages for each blog post at build time, allowing Next.js to handle SEO optimization.

By leveraging the WordPress REST API, you can continue using WordPress for content management while benefiting from Next.js’s SEO optimizations and static site generation.

Example Code

// pages/blog/[slug].js

import fetch from 'node-fetch';

const Post = ({ post }) => {
  return (
    <article>
      <h1>{post.title.rendered}</h1>
      <div dangerouslySetInnerHTML={{ __html: post.content.rendered }} />
    </article>
  );
};

export async function getStaticPaths() {
  const res = await fetch('https://your-wordpress-site.com/wp-json/wp/v2/posts');
  const posts = await res.json();
  const paths = posts.map((post) => ({
    params: { slug: post.slug },
  }));

  return { paths, fallback: false };
}

export async function getStaticProps({ params }) {
  const res = await fetch(`https://your-wordpress-site.com/wp-json/wp/v2/posts?slug=${params.slug}`);
  const post = await res.json();

  return {
    props: {
      post: post[0],
    },
  };
}

export default Post;

4. Static Site Generators + Next.js

For developers who prefer using a static site generator like Gatsby or Hexo, you can integrate these generators with Next.js to produce a fast and SEO-friendly blog. Static site generators work well for blogs that don’t require dynamic content but need a flexible structure for content creation.

By generating static HTML or JSON files with a static site generator, you can integrate them with Next.js via APIs or static directories.

SEO-Optimized Blogs with Next.js

Integrating a blog into your Next.js site can be done in several ways, each with its own benefits. Whether you’re using static Markdown files, integrating a Next.js CMS, or leveraging the power of WordPress, you can ensure that your blog is fast, scalable, and SEO-friendly.

For blog SEO, static generation is a key factor, as it ensures fast page load times and improved search engine rankings. Using a headless CMS or traditional CMS integration allows you to create and manage content efficiently, all while maintaining the performance benefits of Next.js.