build blog app in nextjs using mdx and credentials
- User
build blog app in nextjs using mdx and credentials
- Assistant
build blog app in nextjs using mdx and credentials
In this tutorial, you will learn how to build a basic blog application using Next.js, MDX (Markdown for the component era), and credentials.
npx create-next-app my-blog
cd my-blog
npm install @next/mdx next-mdx-remote remark remark-html gray-matter
Create a folder called "content" in your project's root directory. Inside "content", create a sample markdown file called "my-first-post.mdx":
---
title: My first blog post
date: 2022-01-01
---
Hello, this is my first blog post using Next.js and MDX!
You can also use React components in your posts:
```jsx
import { Box } from '@chakra-ui/react';
export default function CustomComponent() {
return (
<Box
display="flex"
justifyContent="center"
alignItems="center"
backgroundColor="blue.400"
color="white"
padding="4"
borderRadius="sm"
>
Custom Component in Blog Post
</Box>
);
}
Now include the component in your markdown as if it were HTML:
Create a new file called "next.config.js" in your project's root directory. In this file, configure Next.js to use the MDX plugin:
const withMDX = require('@next/mdx')({
extension: /\.mdx$/,
});
module.exports = withMDX({
pageExtensions: ['js', 'jsx', 'md', 'mdx'],
});
In the "lib" folder, create a file called "mdxUtils.js":
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
const CONTENT_PATH = path.join(process.cwd(), 'content');
export function getPosts() {
const fileNames = fs.readdirSync(CONTENT_PATH);
return fileNames.map((fileName) => {
const fullPath = path.join(CONTENT_PATH, fileName);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { data } = matter(fileContents);
return {
fileName: fileName.replace('.mdx', ''),
data,
};
});
}
In the "lib" folder, add the following function to your "mdxUtils.js" file:
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import { serialize } from 'next-mdx-remote/serialize';
import remark from 'remark';
import html from 'remark-html';
const CONTENT_PATH = path.join(process.cwd(), 'content');
// ...
export async function getPostBySlug(slug) {
const fullPath = path.join(CONTENT_PATH, `${slug}.mdx`);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { content, data } = matter(fileContents);
const mdxSource = await serialize(content, {
mdxOptions: {
remarkPlugins: [remark().use(html)],
},
});
return {
mdxSource,
data,
};
}
Update "pages/index.js" to fetch and display a list of your blog posts:
import { getPosts } from '../lib/mdxUtils';
import Link from 'next/link';
export default function Home({ posts }) {
return (
<div>
<h1>Blog</h1>
<ul>
{posts.map((post) => (
<li key={post.fileName}>
<Link href={`/blog/${post.fileName}`}>
<a>{post.data.title}</a>
</Link>
</li>
))}
</ul>
</div>
);
}
export function getStaticProps() {
const posts = getPosts();
return {
props: { posts },
};
}
Create a new file "pages/blog/[slug].js":
import { getPostBySlug, getPosts } from '../../lib/mdxUtils';
import { MDXRemote } from 'next-mdx-remote';
export default function BlogPost({ mdxSource, data }) {
return (
<article>
<h1>{data.title}</h1>
<MDXRemote {...mdxSource} />
</article>
);
}
export function getStaticProps({ params }) {
const { mdxSource, data } = getPostBySlug(params.slug);
return {
props: {
mdxSource,
data,
},
};
}
export function getStaticPaths() {
const posts = getPosts();
return {
paths: posts.map((post) => ({
params: { slug: post.fileName },
})),
fallback: false,
};
}
That's it! You have created a basic blog application using Next.js, MDX, and credentials. You can now run npm run dev to start the development server and view your blog.
You can further customize the components, layout, and styling to match your design. Consider integrating with a headless CMS or using a source folder under source control for your content.