How to create a next.js blog application with mdx and static generation
Prerequisites
To create a next.js application you need to have Node.js installed on your machine. You can download Node.js from the official website here.
Creating a next.js blog application
In this post we will create a next.js blog application with markdown support and static generation at build time. First it is important to understand the differences between server-side rendering (SSR) and static generation (SG).
- Server-side rendering (SSR): The html for a page is generated on the server on each request. This means that the content is always up-to-date but the page load time is slower.
- Static generation (SG): The html for a page is generated at build time. This means that the content is static and the page load time is faster.
These two types can also be mixed, meaning your blog can have some pages that are statically generated and some that are server-side rendered.
Having that in mind, let's create our next.js blog application by running the following command:
We will select the default configuration.
Now we can navigate to the newly created directory.
Dependencies
To add markdown support to our next.js blog application we need to install the following dependencies:
Our first blog post
Create a _posts
directory in the root of your project and add a markdown file my-first-blog-post.mdx
with the following content:
Between the ---
is the front matter, which is metadata about the post. In this case we have the title and the date of the post.
Reading the blog posts from the file system
For now we will read the blog posts from the file system. Create a getPosts
function in a new file src/lib/posts.ts
:
Displaying the blog posts
Now we can display the blog posts on the home page. Create a new file src/app/page.tsx
with the following content:
The function getAllPostsSortedByDate
will get all posts from the file system and display the title and date of each post aswell as offering a link to go to the blog article.
Speaking of which we should create the blog page now.
Displaying a single blog post
Create a new file src/pages/blog/[slug].tsx
with the following content:
With remarkPlugins
and rehypePlugins
you can add plugins to the markdown parser.
Some popular plugins are remark-gfm
for GitHub flavored markdown and rehype-pretty-code
for syntax highlighting.
Now when you build your next.js application with npm run build
you will see that the blog posts are statically generated at build time.
Great! We built a next.js blog application with markdown support and static generation at build time. 🚀
In the next read we will explore how to deploy our next.js blog application on docker hub. Stay tuned!
Read Next
How to create a next.js application with create-next-app
In this post we will explore how to create a next.js application using create next app.
How to deploy a next.js application on docker hub
Explore how to deploy a next.js application on docker hub.
How to work with a next.js application
In this post we will explore how to with a next.js application to add pages, support routing and dynamic routes.