Learn how to implement next-mdx-remote in Next.js 15 using the App Router to dynamically render raw MDX content. This guide covers setup, serialization, and rendering for a seamless MDX integration.
Markdown with JSX (MDX) is a powerful way to write content-rich applications in Next.js. The next-mdx-remote
library allows us to load MDX from various sources, such as CMSs or APIs, and render it dynamically. In this tutorial, we will explore how to integrate next-mdx-remote
in a Next.js 15 application using the App Router and load raw MDX content.
Before getting started, ensure you have:
npx create-next-app@latest
)Run the following command to install the required dependencies:
npm install next-mdx-remote next-mdx-remote/serialize remark-gfm remark-math rehype-slug
Create a new component MDXRenderer.tsx
inside the components
folder:
"use client";
import { MDXRemote, MDXRemoteSerializeResult } from "next-mdx-remote";
import React from "react";
interface MDXRendererProps {
source: MDXRemoteSerializeResult;
}
const MDXRenderer: React.FC<MDXRendererProps> = ({ source }) => {
return <MDXRemote {...source} />;
};
export default MDXRenderer;
Next, create a helper function to serialize and fetch the MDX content in lib/mdx.ts
:
import { serialize } from "next-mdx-remote/serialize";
import rehypeSlug from "rehype-slug";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
export async function getMdxContent(content: string) {
return await serialize(content, {
mdxOptions: {
remarkPlugins: [remarkGfm, remarkMath],
rehypePlugins: [rehypeSlug],
},
});
}
Now, create a dynamic page to display the MDX content. Inside app/mdx/page.tsx
, add the following code:
import MDXRenderer from "@/components/MDXRenderer";
import { getMdxContent } from "@/lib/mdx";
const mdxContent = `
# Hello MDX
This is a sample MDX content with **bold text** and a [link](https://nextjs.org/).
> Blockquotes work too!
\`\`\`tsx
console.log("Hello, Next.js 15!");
\`\`\`
`;
export default async function MdxPage() {
const mdxSource = await getMdxContent(mdxContent);
return (
<div className="prose mx-auto p-5">
<MDXRenderer source={mdxSource} />
</div>
);
}
Start the development server:
npm run dev
Navigate to http://localhost:3000/mdx
, and you should see the rendered MDX content!
Using next-mdx-remote
, you can load and render raw MDX dynamically in Next.js 15 with the App Router. This approach enables fetching MDX from various sources, making it ideal for blog engines, CMS integrations, and dynamic documentation sites.
Read more
Implementing next-mdx-remote in Next.js
Learn how to implement next-mdx-remote in Next.js 15 using the App Router to dynamically render raw MDX content. This guide covers setup, serialization, and rendering for a seamless MDX integration.
2025-02-08
How to Update Tailwind CSS to v4 in Next.js
Learn how to update Tailwind CSS to version 4 in your Next.js 15 project using the App Router. A step-by-step guide with easy-to-follow instructions.
2025-02-07