Introduction
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.
Prerequisites
Before getting started, ensure you have:
- Node.js 18+ installed
- A Next.js 15 project set up (
npx create-next-app@latest
) - Basic knowledge of Next.js and MDX
Step 1: Install Dependencies
Run the following command to install the required dependencies:
Step 2: Create an MDX Rendering Component
Create a new component MDXRenderer.tsx
inside the components
folder:
Step 3: Create an MDX Fetching Function
Next, create a helper function to serialize and fetch the MDX content in lib/mdx.ts
:
Step 4: Implement Dynamic MDX Rendering in a Page
Now, create a dynamic page to display the MDX content. Inside app/mdx/page.tsx
, add the following code:
Step 5: Test the MDX Integration
Start the development server:
Navigate to http://localhost:3000/mdx
, and you should see the rendered MDX content!
Conclusion
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.