Custom Search
For other search solutions.
Overview
Fumadocs is very flexible, you can integrate custom search with Fumadocs easily.
Search Indexes
With Fumadocs MDX, the generated search indexes are exposed during runtime.
To expose search indexes statically, export the records with a pre-rendered static.json route, and process them with a script after each build:
import { source } from '@/lib/source';
async function getDocuments() {
const documents = [];
for (const page of source.getPages()) {
documents.push({
structured: page.data.structuredData,
url: page.url,
title: page.data.title,
description: page.data.description,
});
}
return documents;
}
export const revalidate = false;
export async function GET() {
return Response.json(await getDocuments());
}
import * as fs from 'node:fs/promises';
import type { StructuredData } from 'fumadocs-core/mdx-plugins';
interface DocumentRecord {
title: string;
description?: string;
url: string;
structured: StructuredData;
}
const records = JSON.parse(await fs.readFile('.next/server/app/static.json.body', 'utf-8')) as DocumentRecord[];
// sync the records to your search engine
console.log(`search updated: ${records.length} records`);
{
"scripts": {
"build": "next build && node --env-file-if-exists=.env.local scripts/sync-content.ts"
}
}The structured field contains the headings and paragraphs of each page, use it to generate accurate search indexes.
How is this guide?
Last updated on
