Fumadocs
Vite

React Router

Use Fumadocs MDX with React Router

Setup

Installation

npm i fumadocs-mdx fumadocs-core @types/mdx

Create the configuration file:

source.config.ts
import { defineConfig, defineDocs } from 'fumadocs-mdx/config';

export const docs = defineDocs({
  dir: 'content/docs',
});

export default defineConfig();

Add the Vite plugin:

vite.config.ts
import { defineConfig } from 'vite';
import mdx from 'fumadocs-mdx/vite';
import * as MdxConfig from './source.config';

export default defineConfig({
  plugins: [
    mdx(MdxConfig),
    // ...
  ],
});

Setup an import alias (optional):

tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "@/.source": [".source"]
    }
  }
}

Integrate with Fumadocs

To integrate with Fumadocs, make a lib/source.ts file:

app/lib/source.ts
import { loader } from 'fumadocs-core/source';
import { create, docs } from '@/.source';

export const source = loader({
  source: await create.sourceAsync(docs.doc, docs.meta),
  baseUrl: '/docs',
});

The .source folder will be generated when you run development server or production build.

Done

You can now write content in content/docs folder.

Examples

Rendering Content

As React Router doesn't support RSC at the moment, use toClientRenderer() to lazy load MDX content as a component on browser.

For example:

import type { Route } from './+types/page';
import { source } from '@/lib/source';
import { docs } from '@/.source';
import { toClientRenderer } from 'fumadocs-mdx/runtime/vite';

export async function loader({ params }: Route.LoaderArgs) {
  const slugs = params['*'].split('/').filter((v) => v.length > 0);
  const page = source.getPage(slugs);
  if (!page) throw new Response('Not found', { status: 404 });

  return {
    path: page.path,
  };
}

const renderer = toClientRenderer(docs.doc, ({ default: Mdx, frontmatter }) => {
  return (
    <div className="prose">
      <h1>{frontmatter.title}</h1>
      <Mdx />
    </div>
  );
});

export default function Page(props: Route.ComponentProps) {
  const { path } = props.loaderData;
  const Content = renderer[path];

  return <Content />;
}

How is this guide?

Last updated on