Fumadocs

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, create a function to generate records.

lib/export-search-indexes.ts
import { source } from '@/lib/source';
import type { StructuredData } from 'fumadocs-core/mdx-plugins';

export interface DocumentRecord {
  title: string;
  description?: string;
  url: string;
  structured: StructuredData;
}

export async function exportSearchIndexes() {
  const results: DocumentRecord[] = [];

  for (const page of source.getPages()) {
    results.push({
      structured: page.data.structuredData,
      url: page.url,
      title: page.data.title,
      description: page.data.description,
    });
  }

  return results;
}

Export it via static route handler.

app/static.json/route.ts
import { exportSearchIndexes } from '@/lib/export-search-indexes';

export const revalidate = false;

export async function GET() {
  return Response.json(await exportSearchIndexes());
}

Finally, the exported records can be accessed at:

const filePath = '.next/server/app/static.json.body';

You can read it for further processing:

import * as fs from 'node:fs';
import type { DocumentRecord } from '@/lib/export-search-indexes';

const content = fs.readFileSync(filePath);
const records = JSON.parse(content.toString()) as DocumentRecord[];

Use structuredData to generate accurate search indexes.

How is this guide?

Last updated on

On this page