How to Create a Blog Using NextJS v14 and MDX: A Comprehensive Guide
It is very effective to develop a blog since it helps to showcase and share acquired knowledge, pertinent experiences, and updates to a larger audience. More recently, I integrated a blog functionality on my web site with NextJS v14, MDX, and other libs. This blog post will guide you through the steps that I followed to incorporate the blog featuring updating parts, as well as configuring Next. js and also adding all the needed dependencies.
Table of Contents
Why I Chose to Blog
Therefore, experiencing real-life projects, I began this blog to give an open look into the work process and describe my thinking and actions when turning concepts into tangible products as well as my reasons for those actions. Being a developer myself, most of the time I dwell on projects where an application begins as a mere idea in the developer’s mind and in concrete reality in a fairly short time. Documenting this journey serves several purposes:
-
🕵️♂️ Transparency: To elaborate, the following paragraphs describe actions I choose to incorporate in ensuring that my projects are efficient and effective. For the aspiring developer on the one hand, oftentimes it is useful to observe how a more experienced coworker or a fellow student, as the case may be, is going about solving the problem.
-
💡 Inspiration: Sharing my motivations and the reasons behind my projects can inspire others to pursue their own ideas. Understanding the "why" behind a project often provides the fuel needed to push through challenges and stay committed.
-
📚 Documentation: Keeping a record of my development journey helps me track my progress and reflect on the lessons learned. It's a valuable resource for future reference and continuous improvement.
-
🗣️ Community Engagement: A blog makes it easy to have two-way communication, getting feedback from the readers. There is also interaction by making comments of different stories and articles to get new ideas, collaboration and fellowship from other readers.
My objective is to note down and share all the stages of idea formation and turning it into an actual project and thus motivate people sharing their ideas. It is my way of putting something back into the developer population and helping to build the knowledge base.
So... How i did it?
Adding and Configuring the dependencies
Next.js supports MDX (Markdown for JSX) out of the box with some additional configuration. Here’s how I set it up:
Install Required Packages
I installed the necessary MDX packages and related dependencies:
next.config.js
Update We must add the withMDX plugin to the Next.js configuration file.
tailwindcss.config.ts
Update I'm using tailwind so i must add the typography plugin to the tailwind config file.
Create required components to render the MDX
We will create a mdxComponents file that will allow us to define how each element of the MDX will be rendered.
For example in the code above, we are defining how the pre
tag will be rendered, we are using the Code
component from the bright
library to render the code blocks with syntax highlighting. We are also defining how the a
tag will be rendered, we are using the Link
component from Next.js to render the links. We are also defining a custom File
component that will render a file path above the code block.
The File component is exactly what im using to render the code blocks in this blog post!
Let's create the component to render the MDX!
This is the component that will render the blog post, it uses the MDXRemote
component from next-mdx-remote/rsc
to render the MDX content. We also pass some options to the MDXRemote
component to enable support for GitHub Flavored Markdown, accessible emojis, and table of contents generation. The mdxComponents
object contains the components that will be used to render the MDX content.
Create the logic to fetch the blog posts
We will define the types for the blog posts and create a function to fetch all the posts.
We will store our .mdx
files under the /posts path, and we will use the gray-matter
library to parse the front matter of each file.
Create a file to define the functions to fetch all blog posts.
The getPosts
function reads all the files in the /posts directory, filters out the non-MDX files, and parses the front matter using gray-matter
. It then returns an array of objects containing the post data and body content. The getPost
function fetches a specific post based on its slug.
Create a Blog Page
Now let's create the page where we wll display all the blog posts, I created a group to be able to define a layout for the blog pages, you dont need to if you dont need it.
The PostPage
component fetches all the blog posts using the getPosts
function and maps over them to render a BlogCard
component for each post. The BlogCard
component will display the post title, description, and tags.
And then we can create the page for the specific post.
The PostPage
component fetches the specific post based on the slug provided in the URL. If the post is not found, it returns a 404 page using the notFound
function from next/navigation
. The BlogPost
component renders the post content using the MDXRemote
component.
Create a Sitemap
Creating a sitemap is essential for search engine optimization (SEO) and helps search engines discover and index your website's pages. We can create a sitemap using a serverless function in Next.js.
The sitemap
function defines the routes that should be included in the sitemap. It includes the homepage and the blog page. It then fetches all the blog posts and creates a sitemap route for each post, including the post URL and last modified date.
Conclusion
In this guide, we covered the complete process of setting up a blog using NextJS v14 and MDX. From installing and configuring necessary dependencies to creating and displaying blog posts, each step is crucial for a functional and aesthetically pleasing blog. By following these steps, you can easily set up a blog that is not only easy to manage but also SEO-friendly.