What's the easiest way for me a to add a static blog to my existing Next.js site?
Assistant
User
What's the easiest way for me a to add a static blog to my existing Next.js site use app router?
Assistant
Adding a static blog to your existing Next.js site can be a straightforward process. Here’s a step-by-step guide to help you get started:
Set Up Your Blog Directory:
Create a directory called blog (or whatever you'd like to name it) in your Next.js project folder. This will contain all your blog-related files.
Organize Your Posts:
Create a posts directory inside your blog directory. This is where you’ll store your markdown files (e.g., post1.md, post2.md). Each markdown file will represent a blog post.
Install Required Packages:
You’ll need some packages to parse markdown files. Install gray-matter for frontmatter support and remark/remark-html for markdown-to-HTML conversion. Run:
bash
npm install gray-matter remark remark-html
Create a Utility Function for Fetching Posts:
Inside your lib directory, create a file such as posts.js to fetch, parse, and return your markdown posts. Here is an example setup:
Place some markdown content files in blog/posts with front matter.
markdown
---
title: "My First Post"
date: "2023-10-01"
---
This is my first blog post!
Link to Your Blog:
Ensure that your main site has a way to navigate to your new blog section—perhaps by adding a link to your site's navigation menu.
That's it! With these steps, you’ll have a static blog set up on your Next.js site. You can expand this by adding more styling, pagination, or other features as needed.
Using the Next.js App Router is a modern approach to structuring your Next.js application. Here's how you can add a static blog using the App Router in your existing Next.js site:
Setup Directories:
Navigate to your project's app directory. If it does not exist, create one.
Inside the app directory, create a new folder named blog, where you'll manage your blog pages.
Create a posts folder outside of the app directory to store your markdown files.
Install Required Packages:
You'll need gray-matter for parsing YAML front matter and remark/remark-html for converting markdown to HTML. Install these by running:
bash
npm install gray-matter remark remark-html
Create a Utility Function for Handling Posts:
Create a new folder called lib at the root level if you don't have one, and add a posts.js file for fetching and processing the markdown files:
In the app/blog directory, create a new folder named [id] for dynamic routes.
Inside this [id] folder, create a page.jsx file to render individual blog posts.
jsx
import { getPostData } from '@/lib/posts';
// Dynamic import for data fetching, typical in experimental React Server Components
export async function generateStaticParams() {
const allPostsData = getSortedPostsData();
return allPostsData.map((post) => ({ id: post.id }));
}
export default async function PostPage({ params }) {
const postData = await getPostData(params.id);
return (
<div>
<h1>{postData.title}</h1>
<div dangerouslySetInnerHTML={{ __html: postData.contentHtml }} />
</div>
);
}
Add Markdown Content:
Inside the posts directory, add your markdown files. Ensure they contain front matter:
markdown
---
title: "My First Post"
date: "2023-10-01"
---
This is my first blog post!
Add Navigation to Your Blog:
Ensure you have a way to navigate to your blog from your main site, such as adding a link in your primary navigation menu.
With these steps, you should have a simple static blog setup using the Next.js App Router. This setup leverages the capabilities of Next.js, enabling you to expand and customize your blog functionality further as needed.