WhoBuilt

MDX with Next.js App Router

Nov 30, 2024· 13 min· 35K views·Watch on YouTube

Speaker

Lee Robinson, whose role and company are not stated, explains how to use MDX with Next.js App Router, using his leerob.com portfolio as the example.

Configure Next.js for MDX

Robinson shows a TypeScript Next.js config that adds .mdx to the supported page extensions and uses the @next/mdx plugin. He also mentions an experimental Rust compiler that may speed up builds when statically prerendering many MDX pages. The example includes the MDX loader, React, and type dependencies.

Set shared components and styles

A root-level mdx-components.tsx file maps Markdown elements such as headings, links, code, tables, and quotes to React components. Robinson styles typography with Tailwind and routes internal links through Next.js Link for prefetching. Once defined, these components are available to MDX pages without importing them individually.

Build routes with Markdown and React

With .mdx enabled, app/page.mdx can serve as the application’s home route. Robinson mixes Markdown with a server component that uses CSS for a hover animation, and explains that App Router recognizes files such as Open Graph images and favicons for metadata. A root layout adds metadata and a view transitions provider.

Add post-specific components and metadata

File-system routes such as n/devrel/page.mdx use the shared typography components while supporting locally imported React components and page metadata, including a title and canonical link. A more complex post imports local components, a static image, and Next.js Image, and also uses HTML details elements and footnotes. Robinson notes that files can be colocated in the App Router without becoming routes.

Extend the setup as needed

Robinson recommends the Next.js Markdown and MDX documentation for Remark or Rehype plugins and guidance on remote MDX. The demonstrated setup focuses on local files and uses a small set of MDX and React dependencies. He also notes that the site can combine static and dynamic content and use Next.js image optimization.

Takeaways

  • Put shared MDX renderers in mdx-components.tsx to keep typography and behavior consistent across pages.
  • Use MDX page files as App Router routes when content lives in the project repository.
  • Import React components locally when a post needs custom interactive or animated content.
  • Use Next.js Link for internal links to enable page prefetching.
  • Consider the experimental Rust MDX compiler if static prerendering many pages dominates build time.

Mentioned in this talk

Search inside this talk

What was claimed 11

Show all 11 facts

Related talks

MDX with Next.js App Router, Lee Robinson · WhoBuilt