Next.js
Use Fumadocs MDX with Next.js
Setup
Set up Fumadocs MDX for your Next.js application.
npm i fumadocs-mdx @types/mdx
Create 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';
const withMDX = createMDX({
// customise the config file path
// configPath: "source.config.ts"
});
/** @type {import('next').NextConfig} */
const config = {
reactStrictMode: true,
};
export default withMDX(config);
ESM Only
The Next.js config must be a .mjs
file since Fumadocs is ESM-only.
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(),
});
.source
will be generated when you run next dev
or next build
.
Make sure you are importing the .source
rather than source.config.ts
.
Integrate with CI (Optional)
You can run fumadocs-mdx
to generate the .source
folder.
Optionally, you can run it in post install to ensure types are generated when initializing the project.
{
"scripts": {
"postinstall": "fumadocs-mdx"
}
}
Done
Have fun!
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