Create FasterCreate Faster
Content

MDX

Pre-configured MDX support with frontmatter parsing, custom components, and dynamic routing.

Supported frameworks

Next.js installs @mdx-js/loader, @mdx-js/react and @next/mdx. TanStack Start installs @mdx-js/rollup. Both install remark-frontmatter and remark-mdx-frontmatter.

→ MDX Documentation

TanStack Start

Documents in contents/ are compiled at build time by @mdx-js/rollup and loaded lazily with import.meta.glob, one chunk per document. YAML frontmatter is exported as frontmatter. The example route /mdx/{-$slug} renders /mdx as home.mdx and /mdx/cool as cool.mdx.

Files added:

src/
├── lib/
│   └── mdx.ts                  # Lazy document loader
├── mdx-components.tsx          # Custom MDX component mappings
├── styles/
│   └── mdx.css                 # MDX-specific styling
└── routes/mdx/
    └── {-$slug}.tsx            # MDX route example

contents/
├── home.mdx                    # Example content
└── cool.mdx                    # Example content

Modified files:

  • vite.config.ts - Adds the @mdx-js/rollup plugin with remark-frontmatter and remark-mdx-frontmatter
  • src/styles.css - Imports styles/mdx.css
  • src/routes/index.tsx - Links to the example page

Next.js

Documents in contents/ are compiled at build time by @next/mdx, with remark-frontmatter and remark-mdx-frontmatter exporting the YAML frontmatter as frontmatter. The example route /mdx/[[...slug]] finds the document of the slug with import.meta.glob, so nothing reads the filesystem while a page renders and the route also runs on a Cloudflare Worker. It prerenders one page per document, /mdx for home.mdx and /mdx/cool for cool.mdx, and returns 404 for any other slug (dynamicParams = false). It also builds with cloudflare-static, where each document is exported as an HTML page. To add a document, create the file in contents/: the page needs no edit.

Files added:

src/
├── lib/
│   └── mdx.ts                  # MdxFrontmatter and MdxModule types
├── mdx-components.tsx          # Custom MDX component mappings
├── mdx.d.ts                    # Types the frontmatter export of a document
├── styles/
│   └── mdx.css                 # MDX-specific styling
└── app/mdx/[[...slug]]/
    └── page.tsx                # MDX route example

contents/
├── home.mdx                    # Example content
└── cool.mdx                    # Example content

Modified files:

  • next.config.ts - Adds MDX support with createMDX, the frontmatter plugins and .mdx/.md extensions

mdx.ts

Types shared by the page (MdxModule is the shape of a compiled document):

src/lib/mdx.ts
import type { ComponentType } from 'react';

export type MdxFrontmatter = {
  title: string;
  summary: string;
  publishedAt: string;
  imageUrl?: string;
};

export type MdxModule = {
  default: ComponentType;
  frontmatter: MdxFrontmatter;
};

page.tsx

MDX route prerendered for every document, with metadata generation. import.meta.glob bundles the documents, so no file is read at build time or while a page renders:

src/app/mdx/[[...slug]]/page.tsx
import { notFound } from 'next/navigation';
import type { MdxModule } from '@/lib/mdx';

const HOME_DOCUMENT = 'home';

const documentLoaders = import.meta.glob<MdxModule>('/contents/*.mdx');

interface PageProps {
  params: Promise<{ slug?: string[] }>;
}

export const dynamicParams = false;

export function generateStaticParams(): { slug: string[] }[] {
  return Object.keys(documentLoaders)
    .map((path) => path.slice('/contents/'.length, -'.mdx'.length))
    .map((name) => ({ slug: name === HOME_DOCUMENT ? [] : [name] }));
}

export const generateMetadata = async ({ params }: PageProps) => {
  const { slug } = await params;
  const { frontmatter } = await loadDocument(slug);
  return { title: frontmatter.title, description: frontmatter.summary };
};

export default async function Page({ params }: PageProps) {
  const { slug } = await params;
  const { default: Document } = await loadDocument(slug);

  return (
    <article className='max-w-162.5 mx-auto px-4 py-4 md:py-8'>
      <Document />
    </article>
  );
}

async function loadDocument(slug: string[] = []): Promise<MdxModule> {
  const loadDocumentModule = documentLoaders[`/contents/${slug[0] ?? HOME_DOCUMENT}.mdx`];

  if (slug.length > 1 || slug[0] === HOME_DOCUMENT || !loadDocumentModule) {
    notFound();
  }

  return loadDocumentModule();
}

On this page