One Week Challenge: Building a Modern Blog Website with Next.js
During a week-long break from work, I replaced my five year old static website with a modern, feature-rich blog. This post covers building a bleeding-edge blog using Next.js, Contentlayer, MDX, Tailwind CSS, and TypeScript.
Why Next.js?
I recently completed a months-long work project to convert a multi-tenanted web application from Create React App to Next.js 13.
We're excited to adopt the app
directory once it's out of beta, but in the meantime I wanted to gain some personal experience with the new app router by creating a personal blog website.
Next.js is known for its versatility, ease of use, powerful features, and tight integration into Vercel's services. Next.js offers server-side rendering, static site generation, and API route handlers, making it an ideal choice for a modern blog website.
Why Tailwind CSS?
Initially, as someone with a background in using pre-processors and semantic naming for writing CSS, I was hesitant to adopt Tailwind CSS due to its seemingly never-ending class
attributes.
I recently decided to see what all the fuss was about by using Tailwind to recreate some design I'd previously implemented in Sass. The benefits Tailwind CSS offers quickly became apparent.
Why Contentlayer?
I chose Contentlayer for managing static content in my Next.js project for several reasons:
-
Ease of integration: Contentlayer integrates seamlessly with Next.js, making it easy to fetch and manage content from various sources like Markdown, YAML, or JSON files.
-
MDX support: Contentlayer works well with MDX, enabling me to write my blog posts in Markdown while leveraging the power of React components, rehype plugins , and remark plugins .
-
Type safety: Contentlayer generates TypeScript types based on your content schema, providing type safety for your content. This feature helps catch potential errors early in the development process, improving code quality and maintainability.
-
Hot Module Replacement (HMR): Contentlayer supports Hot Module Replacement, allowing for real-time updates to your content without needing to refresh the page. This feature enhances the developer experience by providing instant feedback during content creation and editing.
-
Flexible content modelling: Contentlayer allows for flexible content modelling through its configuration file. You can define custom content types, fields, and relationships, making it suitable for various use cases and content structures.
By using Contentlayer, I was able to efficiently manage my static content, optimise the build process, and create more engaging and dynamic blog posts with MDX support.
Setting Up the Project
To get started, create a new Next.js project using the following command:
This command set up a new Next.js project using the experimental app
directory, TypeScript, and Tailwind CSS.
Now's a great time to set up deployments with Vercel . Simply sign in using via GitHub and follow the instructions.
@tailwind/typography
Plugin
Install the At this stage, Tailwind CSS will have already been installed by the create-next-app
command via the --tailwind
option.
Add the @tailwind/typography plugin for access to a set of prose
classes that are ideal for formatting your blog posts.
First, install the plugin from npm:
Then, add it to your tailwind.config.js
file:
Integrating Contentlayer and MDX with Next.js
To integrate Contentlayer and MDX, follow these steps:
1. Installing Contentlayer
Install Contentlayer, the next-contentlayer
plugin, and the MDX TypeScript types.
2. Update the Next.js Configuration
Your Next.js configuration file needs to be updated to use the withContentlayer
wrapper function. Here's an example next.config.js
file with Contentlayer configured:
3. Update the TypeScript Configuration
Update your tsconfig.json
file to include the following lines.
4. Configure Contentlayer
Add the root of your project, add a contentlayer.config.ts
file with the following starter configuration:
5. Add Some Content
The above Contentlayer configuration looks for .mdx
files located within the root posts
directory.
Create a posts
folder, and add the following to posts/post-01.mdx
:
6. Add the Posts Page
Add a post list page by creating a file named app/posts/page.tsx
and adding the following contents:
7. Add the Post Details Page
Add a post details page by creating a file named app/posts/[slug]/page.tsx
and adding the following contents:
Ready to Go!
You're now ready to run your new blog application! Simply run the following command:
And navigate to http://localhost:3000/posts to see your posts.
Next Steps
You've created a modern Next.js application with Tailwind CSS, Contentlayer, MDX, and TypeScript. This is a great starting point for your new blog website.
Some next steps you may want to consider:
- Create a custom root layout for your site.
- Update the home page (
app/page.tsx
) to include a link to your blog posts. - Add some rehype and remark plugins to super-charge your MDX - check out remark-gfm for GitHub Flavoured Markdown and rehype-pretty-code for beautiful code syntax highlighting.
- Add a link to your GitHub profile.
Happy coding!