Unverified Commit fbc0a0eb authored by Michael Novotny's avatar Michael Novotny Committed by GitHub

docs: Adds MDX Components file convention page and updates MDX page (#63568)

Co-authored-by: 's avatarLee Robinson <me@leerob.io>
Co-authored-by: 's avatarEthan Arrowood <ethan@arrowood.dev>
parent a5bb7c46
......@@ -24,25 +24,45 @@ Output:
Next.js can support both local MDX content inside your application, as well as remote MDX files fetched dynamically on the server. The Next.js plugin handles transforming markdown and React components into HTML, including support for usage in Server Components (the default in App Router).
## `@next/mdx`
> **Good to know**: View the [Portfolio Starter Kit](https://vercel.com/templates/next.js/portfolio-starter-kit) template for a complete working example.
The `@next/mdx` package is used to configure Next.js so it can process markdown and MDX. **It sources data from local files**, allowing you to create pages with a `.mdx` extension, directly in your `/pages` or `/app` directory.
## Install dependencies
Let's walk through how to configure and use MDX with Next.js.
The `@next/mdx` package, and related packages, are used to configure Next.js so it can process markdown and MDX. **It sources data from local files**, allowing you to create pages with a `.md` or `.mdx` extension, directly in your `/pages` or `/app` directory.
## Getting Started
Install packages needed to render MDX:
Install these packages to render MDX with Next.js:
```bash filename="Terminal"
npm install @next/mdx @mdx-js/loader @mdx-js/react @types/mdx
```
<AppOnly>
## Configure `next.config.mjs`
Update the `next.config.mjs` file at your project's root to configure it to use MDX:
```js filename="next.config.mjs" switcher
import createMDX from '@next/mdx'
/** @type {import('next').NextConfig} */
const nextConfig = {
// Configure `pageExtensions` to include markdown and MDX files
pageExtensions: ['js', 'jsx', 'md', 'mdx', 'ts', 'tsx'],
// Optionally, add any other Next.js config below
}
const withMDX = createMDX({
// Add markdown plugins here, as desired
})
// Merge MDX config with Next.js config
export default withMDX(nextConfig)
```
Create a `mdx-components.tsx` file at the root of your application (`src/` or the parent folder of `app/`):
This allows `.md` and `.mdx` files to act as pages, routes, or imports in your application.
> **Good to know**: `mdx-components.tsx` is required to use MDX with App Router and will not work without it.
## Add a `mdx-components.tsx` file
Create a `mdx-components.tsx` (or `.js`) file in the root of your project to define global MDX Components. For example, at the same level as `pages` or `app`, or inside `src` if applicable.
```tsx filename="mdx-components.tsx" switcher
import type { MDXComponents } from 'mdx/types'
......@@ -62,33 +82,87 @@ export function useMDXComponents(components) {
}
```
> **Good to know**:
>
> - `mdx-components.tsx` is **required** to use `@next/mdx` with App Router and will not work without it.
> - Learn more about the [`mdx-components.tsx` file convention](/docs/app/api-reference/file-conventions/mdx-components).
> - Learn how to [use custom styles and components](#using-custom-styles-and-components).
## Rendering MDX
You can render MDX using Next.js's file based routing or by importing MDX files into other pages.
### Using file based routing
When using file based routing, you can use MDX pages like any other page.
<AppOnly>
In App Router apps, that includes being able to use [metadata](/docs/app/building-your-application/optimizing/metadata).
Create a new MDX page within the `/app` directory:
```txt
my-project
├── app
│ └── mdx-page
│ └── page.(mdx/md)
|── mdx-components.(tsx/js)
└── package.json
```
</AppOnly>
Update the `next.config.js` file at your project's root to configure it to use MDX:
<PagesOnly>
```js filename="next.config.js"
const withMDX = require('@next/mdx')()
Create a new MDX page within the `/pages` directory:
/** @type {import('next').NextConfig} */
const nextConfig = {
// Configure `pageExtensions` to include MDX files
pageExtensions: ['js', 'jsx', 'mdx', 'ts', 'tsx'],
// Optionally, add any other Next.js config below
}
```txt
my-project
|── mdx-components.(tsx/js)
├── pages
│ └── mdx-page.(mdx/md)
└── package.json
```
module.exports = withMDX(nextConfig)
</PagesOnly>
You can use MDX in these files, and even import React components, directly inside your MDX page:
```mdx
import { MyComponent } from 'my-component'
# Welcome to my MDX page!
This is some **bold** and _italics_ text.
This is a list in markdown:
- One
- Two
- Three
Checkout my React component:
<MyComponent />
```
Navigating to the `/mdx-page` route should display your rendered MDX page.
### Using imports
<AppOnly>
Then, create a new MDX page within the `/app` directory:
Create a new page within the `/app` directory and an MDX file wherever you'd like:
```txt
your-project
my-project
├── app
│ └── my-mdx-page
│ └── page.mdx
|── mdx-components.(tsx/jsx)
│ └── mdx-page
│ └── page.(tsx/js)
├── markdown
│ └── welcome.(mdx/md)
|── mdx-components.(tsx/js)
└── package.json
```
......@@ -96,21 +170,24 @@ Then, create a new MDX page within the `/app` directory:
<PagesOnly>
Then, create a new MDX page within the `/pages` directory:
Create a new page within the `/pages` directory and an MDX file wherever you'd like:
```txt
your-project
my-project
├── pages
│ └── my-mdx-page.mdx
│ └── mdx-page.(tsx/js)
├── markdown
│ └── welcome.(mdx/md)
|── mdx-components.(tsx/js)
└── package.json
```
</PagesOnly>
Now you can use markdown and import React components directly inside your MDX page:
You can use MDX in these files, and even import React components, directly inside your MDX page:
```mdx
import { MyComponent } from 'my-components'
```mdx filename="markdown/welcome.mdx" switcher
import { MyComponent } from 'my-component'
# Welcome to my MDX page!
......@@ -127,37 +204,172 @@ Checkout my React component:
<MyComponent />
```
Navigating to the `/my-mdx-page` route should display your rendered MDX.
Import the MDX file inside the page to display the content:
## Remote MDX
<AppOnly>
If your markdown or MDX files or content lives _somewhere else_, you can fetch it dynamically on the server. This is useful for content stored in a separate local folder, CMS, database, or anywhere else. A popular community package for this use is [`next-mdx-remote`](https://github.com/hashicorp/next-mdx-remote#react-server-components-rsc--nextjs-app-directory-support).
```tsx filename="app/mdx-page/page.tsx" switcher
import Welcome from '@/markdown/welcome.mdx'
> **Good to know**: Please proceed with caution. MDX compiles to JavaScript and is executed on the server. You should only fetch MDX content from a trusted source, otherwise this can lead to remote code execution (RCE).
export default function Page() {
return <Welcome />
}
```
The following example uses `next-mdx-remote`:
```jsx filename="app/mdx-page/page.js" switcher
import Welcome from '@/markdown/welcome.mdx'
export default function Page() {
return <Welcome />
}
```
</AppOnly>
<PagesOnly>
```tsx filename="pages/mdx-page.tsx" switcher
import Welcome from '@/markdown/welcome.mdx'
export default function Page() {
return <Welcome />
}
```
```jsx filename="pages/mdx-page.js" switcher
import Welcome from '@/markdown/welcome.mdx'
export default function Page() {
return <Welcome />
}
```
</PagesOnly>
Navigating to the `/mdx-page` route should display your rendered MDX page.
## Using custom styles and components
Markdown, when rendered, maps to native HTML elements. For example, writing the following markdown:
```md
## This is a heading
This is a list in markdown:
- One
- Two
- Three
```
Generates the following HTML:
```html
<h2>This is a heading</h2>
<p>This is a list in markdown:</p>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
```
To style your markdown, you can provide custom components that map to the generated HTML elements. Styles and components can be implemented globally, locally, and with shared layouts.
### Global styles and components
Adding styles and components in `mdx-components.tsx` will affect _all_ MDX files in your application.
```tsx filename="mdx-components.tsx" switcher
import type { MDXComponents } from 'mdx/types'
import Image, { ImageProps } from 'next/image'
// This file allows you to provide custom React components
// to be used in MDX files. You can import and use any
// React component you want, including inline styles,
// components from other libraries, and more.
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
// Allows customizing built-in components, e.g. to add styling.
h1: ({ children }) => (
<h1 style={{ color: 'red', fontSize: '48px' }}>{children}</h1>
),
img: (props) => (
<Image
sizes="100vw"
style={{ width: '100%', height: 'auto' }}
{...(props as ImageProps)}
/>
),
...components,
}
}
```
```js filename="mdx-components.js" switcher
import Image from 'next/image'
// This file allows you to provide custom React components
// to be used in MDX files. You can import and use any
// React component you want, including inline styles,
// components from other libraries, and more.
export function useMDXComponents(components) {
return {
// Allows customizing built-in components, e.g. to add styling.
h1: ({ children }) => (
<h1 style={{ color: 'red', fontSize: '48px' }}>{children}</h1>
),
img: (props) => (
<Image
sizes="100vw"
style={{ width: '100%', height: 'auto' }}
{...props}
/>
),
...components,
}
}
```
### Local styles and components
You can apply local styles and components to specific pages by passing them into imported MDX components. These will merge with and override [global styles and components](#global-styles-and-components).
<AppOnly>
```tsx filename="app/my-mdx-page-remote/page.tsx" switcher
import { MDXRemote } from 'next-mdx-remote/rsc'
```tsx filename="app/mdx-page/page.tsx" switcher
import Welcome from '@/markdown/welcome.mdx'
export default async function RemoteMdxPage() {
// MDX text - can be from a local file, database, CMS, fetch, anywhere...
const res = await fetch('https://...')
const markdown = await res.text()
return <MDXRemote source={markdown} />
function CustomH1({ children }) {
return <h1 style={{ color: 'blue', fontSize: '100px' }}>{children}</h1>
}
const overrideComponents = {
h1: CustomH1,
}
export default function Page() {
return <Welcome components={overrideComponents} />
}
```
```jsx filename="app/my-mdx-page-remote/page.js" switcher
import { MDXRemote } from 'next-mdx-remote/rsc'
```jsx filename="app/mdx-page/page.js" switcher
import Welcome from '@/markdown/welcome.mdx'
export default async function RemoteMdxPage() {
// MDX text - can be from a local file, database, CMS, fetch, anywhere...
const res = await fetch('https://...')
const markdown = await res.text()
return <MDXRemote source={markdown} />
function CustomH1({ children }) {
return <h1 style={{ color: 'blue', fontSize: '100px' }}>{children}</h1>
}
const overrideComponents = {
h1: CustomH1,
}
export default function Page() {
return <Welcome components={overrideComponents} />
}
```
......@@ -165,62 +377,54 @@ export default async function RemoteMdxPage() {
<PagesOnly>
```tsx filename="pages/my-mdx-page-remote.tsx" switcher
import { serialize } from 'next-mdx-remote/serialize'
import { MDXRemote, MDXRemoteSerializeResult } from 'next-mdx-remote'
```tsx filename="pages/mdx-page.tsx" switcher
import Welcome from '@/markdown/welcome.mdx'
interface Props {
mdxSource: MDXRemoteSerializeResult
function CustomH1({ children }) {
return <h1 style={{ color: 'blue', fontSize: '100px' }}>{children}</h1>
}
export default function RemoteMdxPage({ mdxSource }: Props) {
return <MDXRemote {...mdxSource} />
const overrideComponents = {
h1: CustomH1,
}
export async function getStaticProps() {
// MDX text - can be from a local file, database, CMS, fetch, anywhere...
const res = await fetch('https:...')
const mdxText = await res.text()
const mdxSource = await serialize(mdxText)
return { props: { mdxSource } }
export default function Page() {
return <Welcome components={overrideComponents} />
}
```
```jsx filename="pages/my-mdx-page-remote.js" switcher
import { serialize } from 'next-mdx-remote/serialize'
import { MDXRemote } from 'next-mdx-remote'
```jsx filename="pages/mdx-page.js" switcher
import Welcome from '@/markdown/welcome.mdx'
export default function RemoteMdxPage({ mdxSource }) {
return <MDXRemote {...mdxSource} />
function CustomH1({ children }) {
return <h1 style={{ color: 'blue', fontSize: '100px' }}>{children}</h1>
}
export async function getStaticProps() {
// MDX text - can be from a local file, database, CMS, fetch, anywhere...
const res = await fetch('https:...')
const mdxText = await res.text()
const mdxSource = await serialize(mdxText)
return { props: { mdxSource } }
const overrideComponents = {
h1: CustomH1,
}
export default function Page() {
return <Welcome components={overrideComponents} />
}
```
</PagesOnly>
Navigating to the `/my-mdx-page-remote` route should display your rendered MDX.
## Layouts
### Shared layouts
<AppOnly>
To share a layout across MDX pages, you can use the [built-in layouts support](/docs/app/building-your-application/routing/pages-and-layouts#layouts) with the App Router.
```tsx filename="app/my-mdx-page/layout.tsx" switcher
```tsx filename="app/mdx-page/layout.tsx" switcher
export default function MdxLayout({ children }: { children: React.ReactNode }) {
// Create any shared layout or styles here
return <div style={{ color: 'blue' }}>{children}</div>
}
```
```jsx filename="app/my-mdx-page/layout.js" switcher
```jsx filename="app/mdx-page/layout.js" switcher
export default function MdxLayout({ children }) {
// Create any shared layout or styles here
return <div style={{ color: 'blue' }}>{children}</div>
......@@ -262,6 +466,154 @@ export default function MDXPage({ children }) {
</PagesOnly>
### Using Tailwind typography plugin
If you are using [Tailwind](https://tailwindcss.com) to style your application, using the [`@tailwindcss/typography` plugin](https://tailwindcss.com/docs/plugins#typography) will allow you to reuse your Tailwind configuration and styles in your markdown files.
The plugin adds a set of `prose` classes that can be used to add typographic styles to content blocks that come from sources, like markdown.
[Install Tailwind typography](https://github.com/tailwindlabs/tailwindcss-typography?tab=readme-ov-file#installation) and use with [shared layouts](#shared-layouts) to add the `prose` you want.
<AppOnly>
```tsx filename="app/mdx-page/layout.tsx" switcher
export default function MdxLayout({ children }: { children: React.ReactNode }) {
// Create any shared layout or styles here
return (
<div className="prose prose-headings:mt-8 prose-headings:font-semibold prose-headings:text-black prose-h1:text-5xl prose-h2:text-4xl prose-h3:text-3xl prose-h4:text-2xl prose-h5:text-xl prose-h6:text-lg dark:prose-headings:text-white">
{children}
</div>
)
}
```
```jsx filename="app/mdx-page/layout.js" switcher
export default function MdxLayout({ children }) {
// Create any shared layout or styles here
return (
<div className="prose prose-headings:mt-8 prose-headings:font-semibold prose-headings:text-black prose-h1:text-5xl prose-h2:text-4xl prose-h3:text-3xl prose-h4:text-2xl prose-h5:text-xl prose-h6:text-lg dark:prose-headings:text-white">
{children}
</div>
)
}
```
</AppOnly>
<PagesOnly>
To share a layout around MDX pages, create a layout component:
```tsx filename="components/mdx-layout.tsx" switcher
export default function MdxLayout({ children }: { children: React.ReactNode }) {
// Create any shared layout or styles here
return (
<div className="prose prose-headings:mt-8 prose-headings:font-semibold prose-headings:text-black prose-h1:text-5xl prose-h2:text-4xl prose-h3:text-3xl prose-h4:text-2xl prose-h5:text-xl prose-h6:text-lg dark:prose-headings:text-white">
{children}
</div>
)
}
```
```jsx filename="components/mdx-layout.js" switcher
export default function MdxLayout({ children }) {
// Create any shared layout or styles here
return (
<div className="prose prose-headings:mt-8 prose-headings:font-semibold prose-headings:text-black prose-h1:text-5xl prose-h2:text-4xl prose-h3:text-3xl prose-h4:text-2xl prose-h5:text-xl prose-h6:text-lg dark:prose-headings:text-white">
{children}
</div>
)
}
```
Then, import the layout component into the MDX page, wrap the MDX content in the layout, and export it:
```mdx
import MdxLayout from '../components/mdx-layout'
# Welcome to my MDX page!
export default function MDXPage({ children }) {
return <MdxLayout>{children}</MdxLayout>
}
```
## Frontmatter
Frontmatter is a YAML like key/value pairing that can be used to store data about a page. `@next/mdx` does **not** support frontmatter by default, though there are many solutions for adding frontmatter to your MDX content, such as:
- [remark-frontmatter](https://github.com/remarkjs/remark-frontmatter)
- [remark-mdx-frontmatter](https://github.com/remcohaszing/remark-mdx-frontmatter)
- [gray-matter](https://github.com/jonschlinkert/gray-matter)
`@next/mdx` **does** allow you to use exports like any other JavaScript component:
```mdx filename="content/blog-post.mdx" switcher
export const metadata = {
author: 'John Doe',
}
# Blog post
```
Metadata can now be referenced outside of the MDX file:
<AppOnly>
```tsx filename="app/blog/page.tsx" switcher
import BlogPost, { metadata } from '@/content/blog-post.mdx'
export default function Page() {
console.log('metadata': metadata)
//=> { author: 'John Doe' }
return <BlogPost />
}
```
```jsx filename="app/blog/page.js" switcher
import BlogPost, { metadata } from '@/content/blog-post.mdx'
export default function Page() {
console.log('metadata': metadata)
//=> { author: 'John Doe' }
return <Welcome />
}
```
</AppOnly>
<PagesOnly>
```tsx filename="pages/blog.tsx" switcher
import BlogPost, { metadata } from '@/content/blog-post.mdx'
export default function Page() {
console.log('metadata': metadata)
//=> { author: 'John Doe' }
return <BlogPost />
}
```
```jsx filename="pages/blog.js" switcher
import BlogPost, { metadata } from '@/content/blog-post.mdx'
export default function Page() {
console.log('metadata': metadata)
//=> { author: 'John Doe' }
return <Welcome />
}
```
</PagesOnly>
A common use case for this is when you want to iterate over a collection of MDX and extract data. For example, creating a blog index page from all blog posts. You can use packages like [Node's `fs` module](https://nodejs.org/api/fs.html) or [globby](https://www.npmjs.com/package/globby) to read a directory of posts and extract the metadata.
> **Good to know**:
>
> - Using `fs`, `globby`, etc. can only be used server-side.
> - View the [Portfolio Starter Kit](https://vercel.com/templates/next.js/portfolio-starter-kit) template for a complete working example.
## Remark and Rehype Plugins
You can optionally provide `remark` and `rehype` plugins to transform the MDX content.
......@@ -277,7 +629,7 @@ import createMDX from '@next/mdx'
/** @type {import('next').NextConfig} */
const nextConfig = {
// Configure `pageExtensions`` to include MDX files
pageExtensions: ['js', 'jsx', 'mdx', 'ts', 'tsx'],
pageExtensions: ['js', 'jsx', 'md', 'mdx', 'ts', 'tsx'],
// Optionally, add any other Next.js config below
}
......@@ -293,111 +645,84 @@ const withMDX = createMDX({
export default withMDX(nextConfig)
```
## Frontmatter
Frontmatter is a YAML like key/value pairing that can be used to store data about a page. `@next/mdx` does **not** support frontmatter by default, though there are many solutions for adding frontmatter to your MDX content, such as:
- [remark-frontmatter](https://github.com/remarkjs/remark-frontmatter)
- [remark-mdx-frontmatter](https://github.com/remcohaszing/remark-mdx-frontmatter)
- [gray-matter](https://github.com/jonschlinkert/gray-matter).
To access page metadata with `@next/mdx`, you can export a metadata object from within the `.mdx` file:
## Remote MDX
```mdx
export const metadata = {
author: 'John Doe',
}
If your MDX files or content lives _somewhere else_, you can fetch it dynamically on the server. This is useful for content stored in a separate local folder, CMS, database, or anywhere else. A popular community package for this use is [`next-mdx-remote`](https://github.com/hashicorp/next-mdx-remote#react-server-components-rsc--nextjs-app-directory-support).
# My MDX page
```
> **Good to know**: Please proceed with caution. MDX compiles to JavaScript and is executed on the server. You should only fetch MDX content from a trusted source, otherwise this can lead to remote code execution (RCE).
## Custom Elements
The following example uses `next-mdx-remote`:
One of the pleasant aspects of using markdown, is that it maps to native `HTML` elements, making writing fast, and intuitive:
<AppOnly>
```md
This is a list in markdown:
```tsx filename="app/mdx-page-remote/page.tsx" switcher
import { MDXRemote } from 'next-mdx-remote/rsc'
- One
- Two
- Three
export default async function RemoteMdxPage() {
// MDX text - can be from a local file, database, CMS, fetch, anywhere...
const res = await fetch('https://...')
const markdown = await res.text()
return <MDXRemote source={markdown} />
}
```
The above generates the following `HTML`:
```html
<p>This is a list in markdown:</p>
```jsx filename="app/mdx-page-remote/page.js" switcher
import { MDXRemote } from 'next-mdx-remote/rsc'
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
export default async function RemoteMdxPage() {
// MDX text - can be from a local file, database, CMS, fetch, anywhere...
const res = await fetch('https://...')
const markdown = await res.text()
return <MDXRemote source={markdown} />
}
```
When you want to style your own elements for a custom feel to your website or application, you can pass in shortcodes. These are your own custom components that map to `HTML` elements.
<AppOnly>
To do this, open the `mdx-components.tsx` file at the root of your application and add custom elements:
</AppOnly>
<PagesOnly>
To do this, create a `mdx-components.tsx` file at the root of your application (the parent folder of `pages/` or `src/`) and add custom elements:
</PagesOnly>
```tsx filename="pages/mdx-page-remote.tsx" switcher
import { serialize } from 'next-mdx-remote/serialize'
import { MDXRemote, MDXRemoteSerializeResult } from 'next-mdx-remote'
```tsx filename="mdx-components.tsx" switcher
import type { MDXComponents } from 'mdx/types'
import Image, { ImageProps } from 'next/image'
interface Props {
mdxSource: MDXRemoteSerializeResult
}
// This file allows you to provide custom React components
// to be used in MDX files. You can import and use any
// React component you want, including inline styles,
// components from other libraries, and more.
export default function RemoteMdxPage({ mdxSource }: Props) {
return <MDXRemote {...mdxSource} />
}
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
// Allows customizing built-in components, e.g. to add styling.
h1: ({ children }) => <h1 style={{ fontSize: '100px' }}>{children}</h1>,
img: (props) => (
<Image
sizes="100vw"
style={{ width: '100%', height: 'auto' }}
{...(props as ImageProps)}
/>
),
...components,
}
export async function getStaticProps() {
// MDX text - can be from a local file, database, CMS, fetch, anywhere...
const res = await fetch('https:...')
const mdxText = await res.text()
const mdxSource = await serialize(mdxText)
return { props: { mdxSource } }
}
```
```js filename="mdx-components.js" switcher
import Image from 'next/image'
```jsx filename="pages/mdx-page-remote.js" switcher
import { serialize } from 'next-mdx-remote/serialize'
import { MDXRemote } from 'next-mdx-remote'
// This file allows you to provide custom React components
// to be used in MDX files. You can import and use any
// React component you want, including inline styles,
// components from other libraries, and more.
export default function RemoteMdxPage({ mdxSource }) {
return <MDXRemote {...mdxSource} />
}
export function useMDXComponents(components) {
return {
// Allows customizing built-in components, e.g. to add styling.
h1: ({ children }) => <h1 style={{ fontSize: '100px' }}>{children}</h1>,
img: (props) => (
<Image
sizes="100vw"
style={{ width: '100%', height: 'auto' }}
{...props}
/>
),
...components,
}
export async function getStaticProps() {
// MDX text - can be from a local file, database, CMS, fetch, anywhere...
const res = await fetch('https:...')
const mdxText = await res.text()
const mdxSource = await serialize(mdxText)
return { props: { mdxSource } }
}
```
</PagesOnly>
Navigating to the `/mdx-page-remote` route should display your rendered MDX.
## Deep Dive: How do you transform markdown into HTML?
React does not natively understand markdown. The markdown plaintext needs to first be transformed into HTML. This can be accomplished with `remark` and `rehype`.
......
---
title: mdx-components.js
description: API reference for the mdx-components.js file.
related:
title: Learn more about MDX Components
links:
- app/building-your-application/configuring/mdx
---
The `mdx-components.js|tsx` file is **required** to use [`@next/mdx` with App Router](/docs/app/building-your-application/configuring/mdx) and will not work without it. Additionally, you can use it to [customize styles](/docs/app/building-your-application/configuring/mdx#using-custom-styles-and-components).
Use the file `mdx-components.tsx` (or `.js`) in the root of your project to define MDX Components. For example, at the same level as `pages` or `app`, or inside `src` if applicable.
```tsx filename="mdx-components.tsx" switcher
import type { MDXComponents } from 'mdx/types'
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
}
}
```
```js filename="mdx-components.js" switcher
export function useMDXComponents(components) {
return {
...components,
}
}
```
## Exports
### `useMDXComponents` function
The file must export a single function, either as a default export or named `useMDXComponents`.
```tsx filename="mdx-components.tsx" switcher
import type { MDXComponents } from 'mdx/types'
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
}
}
```
```js filename="mdx-components.js" switcher
export function useMDXComponents(components) {
return {
...components,
}
}
```
## Params
### `components`
When defining MDX Components, the export function accepts a single parameter, `components`. This parameter is an instance of `MDXComponents`.
- The key is the name of the HTML element to override.
- The value is the component to render instead.
> **Good to know**: Remember to pass all other components (i.e. `...components`) that do not have overrides.
## Version History
| Version | Changes |
| --------- | -------------------- |
| `v13.1.2` | MDX Components added |
---
title: middleware.js
description: API reference for the middleware.js file.
description: API reference for the middleware.js file.
related:
title: Learn more about Middleware
links:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment