Vite
Tanstack Start
Use Fumadocs MDX with Tanstack Start & Router
Setup
Installation
npm i fumadocs-mdx fumadocs-core @types/mdxCreate the configuration file:
import { defineConfig, defineDocs } from 'fumadocs-mdx/config';
export const docs = defineDocs({
dir: 'content/docs',
});
export default defineConfig();Add the Vite plugin:
import react from '@vitejs/plugin-react';
import { tanstackStart } from '@tanstack/react-start/plugin/vite';
import { defineConfig } from 'vite';
import tsConfigPaths from 'vite-tsconfig-paths';
import tailwindcss from '@tailwindcss/vite';
import mdx from 'fumadocs-mdx/vite';
export default defineConfig({
server: {
port: 3000,
},
plugins: [
mdx(await import('./source.config')),
tailwindcss(),
tsConfigPaths({
projects: ['./tsconfig.json'],
}),
tanstackStart({
prerender: {
enabled: true,
},
}),
react(),
],
});Setup an import alias (optional):
{
"compilerOptions": {
"paths": {
"@/.source": [".source"]
}
}
}Integrate with Fumadocs
To integrate with Fumadocs, create a lib/source.ts file:
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 Tanstack Start doesn't support RSC at the moment, use createClientLoader() to lazy load MDX content as a component on browser.
For example:
import { createFileRoute, notFound } from '@tanstack/react-router';
import { createServerFn } from '@tanstack/react-start';
import { source } from '@/lib/source';
import { docs } from '@/.source';
import { createClientLoader } from 'fumadocs-mdx/runtime/vite';
export const Route = createFileRoute('/docs/$')({
component: Page,
loader: async ({ params }) => {
const data = await loader({ data: params._splat?.split('/') ?? [] });
await clientLoader.preload(data.path);
return data;
},
});
const loader = createServerFn({
method: 'GET',
})
.inputValidator((slugs: string[]) => slugs)
.handler(async ({ data: slugs }) => {
const page = source.getPage(slugs);
if (!page) throw notFound();
return {
path: page.path,
};
});
const clientLoader = createClientLoader(docs.doc, {
id: 'docs',
component({ frontmatter, default: MDX }) {
return (
<div className="prose">
<h1>{frontmatter.title}</h1>
<MDX />
</div>
);
},
});
function Page() {
const data = Route.useLoaderData();
const Content = clientLoader.getComponent(data.path);
return <Content />;
}How is this guide?
Last updated on
