Create a static blog with Next.js and Tailwind CSS
Estimated reading time: 5 minutes
22/10/2023
Introduction
I recently decided to create a static blog to share my knowledge and experiences. I chose Next.js for its simplicity and flexibility. In this article, I will show you how to create a static blog with Next.js and Tailwind CSS.
You can check out my github to see the code of my blog.
Tour of existing tools
There are many tools to create a static blog. Here is a non-exhaustive list:
Docusaurus
Docusaurus is an open source static site generator created by Facebook. It is mainly used to create documentation sites but it is possible to use it to create a static blog.
Nextra
Nextra is an open source static site generator based on Next.js. It is very thought out to create documentations like Docusaurus.
Why not use these tools?
To be honest the first version of my site was based on Docusaurus. It's a great tool and I recommend it for anyone who wants to create a turnkey documentation site.
Nevertheless, I still had the feeling that I had an over ingineered setup for a simple blog. And I wanted to start from scratch to better master the features of my site.
So I decided to rewrite my site with Next.js only.
I could have used Remix as well, this could be the subject of a future article.
Project setup
I started from a blank Next.js project. I needed a design system. So I chose Tailwind CSS and shadcn which is a design system based on Tailwind CSS and radix ui to manage accessibility.
With these tools, I was able to create a complete and customizable design system and easily implement the landing page of my site.
Now I just need to create the pages of my blog.
Blog articles
To create the articles of my blog, I chose to use markdown. It's a very simple format to write and read. It is also very easy to transform it into HTML.
I also wanted to have a translation system. So I created a posts
folder in which I created a folder for each article. Each folder contains one markdown file per language.
posts
βββ my-first-post
β βββ en.md
β βββ fr.md
βββ my-second-post
βββ en.md
βββ fr.md
Import articles
In order to import the articles, I chose to use raw-loader
to import the markdown files and marked
to parse the markdown into HTML.
You have to modify the next.config.js
file to add the raw-loader
loader:
Then, you have to create a posts.ts
file which will be a server action. This file will import the markdown files and parse them into HTML.
To get the list of articles, I created a getPostList
function that will import all the articles and parse them into an object.
Now we just have to create a component to display a post.
We create a post folder for the router and we create a child folder [slug]
.
The post/[slug]
page will get the content of the post and display it.
And voila, we have a static blog with Next.js.
Well, on the other hand it's not super beautiful yet. We'll have to add some style. And for that, we'll use Tailwind CSS.
Add style
To add style, we will use a Tailwind CSS extension: @tailwindcss/typography
.
Now we can add a layout to our page.
Now, we have a static blog with a style that we will be able to improve as we wish.
Conclusion
If you want to create a static blog, Next.js complemented by Tailwind CSS is a winning combo. You have a simple and flexible setup that will allow you to create a static blog in your image. Plus with server components, you have the best development experience possible.
I'll give you one last hack for writing your articles. Use copilot to assist you in writing your articles. It's a huge time saver. Just activate it in the VSCode settings.
I'm Simon Boisset, freelance fullstack developer. I mainly work with React, React Native and Node.js. I'm available for development or consulting missions. Feel free to contact me on my website.