Next.js
Use Fumadocs MDX with Next.js
Setup
Set up Fumadocs MDX for your Next.js application.
Installation
npm i fumadocs-mdx fumadocs-core @types/mdxCreate the configuration file:
import { defineDocs, defineConfig } from 'fumadocs-mdx/config';
export const docs = defineDocs({
  dir: 'content/docs',
});
export default defineConfig();Add the plugin to Next.js config:
import { createMDX } from 'fumadocs-mdx/next';
/** @type {import('next').NextConfig} */
const config = {
  reactStrictMode: true,
};
const withMDX = createMDX({
  // customise the config file path
  // configPath: "source.config.ts"
});
export default withMDX(config);ESM Only
The Next.js config must be a .mjs file since Fumadocs is ESM-only.
Setup an import alias (optional):
{
  "compilerOptions": {
    "paths": {
      "@/.source": [".source"]
    }
  }
}Integrate with Fumadocs
You can create a lib/source.ts file and obtain Fumadocs source from the docs collection output.
import { docs } from '@/.source';
import { loader } from 'fumadocs-core/source';
export const source = loader({
  baseUrl: '/docs',
  source: docs.toFumadocsSource(),
});The .source folder will be generated when you run next dev or next build.
Done
You can now write content in content/docs folder.
Examples
Accessing Content
Generally, you'll interact with the collections through loader(), same for multiple docs collections.
import { source } from '@/lib/source';
const page = source.getPage(['slugs']);
if (page) {
  // access page data
  console.log(page.data);
  // frontmatter properties are also inside
  console.log(page.data.title);
}To render the page, use page.data.body as a component.
import { getMDXComponents } from '@/mdx-components';
const MDX = page.data.body;
// set your MDX components with `components` prop
return <MDX components={getMDXComponents()} />;Without using loader(), you can also import the .source folder using its collection name:
import { defineDocs } from 'fumadocs-mdx/config';
export const docs = defineDocs({
  dir: 'content/docs',
  docs: {
    // options for `doc` collection
  },
  meta: {
    // options for `meta` collection
  },
});How is this guide?
Last updated on
