Fumadocs rebuilt on Hamur, the original is fumadocs.dev
Skip to Content
Fumadocs

Meilisearch

Using Meilisearch with Fumadocs UI.

Setup

Fumadocs CLI can set it up for you, it creates the search dialog, the static.json route and a script to sync the search index after each build:

npx @fumadocs/cli feature search --provider meilisearch

Or manually:

  1. Integrate Meilisearch.

  2. Create a search dialog, replace host and apiKey with your host URL and search API key.

    components/search.tsx
    'use client';
    import { Meilisearch } from 'meilisearch';
    import {
      SearchDialog,
      SearchDialogClose,
      SearchDialogContent,
      SearchDialogFooter,
      SearchDialogHeader,
      SearchDialogIcon,
      SearchDialogInput,
      SearchDialogList,
      SearchDialogOverlay,
      type SharedProps,
    } from 'fumadocs-ui/components/dialog/search';
    import { useMeilisearch } from 'fumadocs-core/search/client';
    import { useI18n } from 'fumadocs-ui/contexts/i18n';
    
    const client = new Meilisearch({
      host: 'replace me',
      apiKey: 'replace me',
    });
    
    export default function CustomSearchDialog(props: SharedProps) {
      const { locale } = useI18n(); // (optional) for i18n
      const search = useMeilisearch({
        client,
        indexName: 'docs',
        locale,
      });
    
      return (
        <SearchDialog {...search} {...props}>
          <SearchDialogOverlay />
          <SearchDialogContent>
            <SearchDialogHeader>
              <SearchDialogIcon />
              <SearchDialogInput />
              <SearchDialogClose />
            </SearchDialogHeader>
            <SearchDialogList />
            <SearchDialogFooter>
              <a
                href="https://www.meilisearch.com"
                rel="noreferrer noopener"
                className="ms-auto text-xs text-fd-muted-foreground"
              >
                Search powered by Meilisearch
              </a>
            </SearchDialogFooter>
          </SearchDialogContent>
        </SearchDialog>
      );
    }
    

Replace Search Dialog

Pass your search dialog to <RootProvider /> in the root layout:

app/root.tsx
import { Links, Meta, Scripts, ScrollRestoration } from 'react-router';
import { RootProvider } from 'fumadocs-ui/provider/react-router';
import SearchDialog from '@/components/search';

export function Layout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <Meta />
        <Links />
      </head>
      <body className="flex flex-col min-h-screen">
        <RootProvider search={{ SearchDialog }}>{children}</RootProvider>
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  );
}

Tag Filter

Optionally, you can add UI for filtering results by tags. Configure Tag Filter on documents and add the following:

'use client';

import {
  SearchDialog,
  SearchDialogContent,
  SearchDialogFooter,
  SearchDialogOverlay,
  type SharedProps,
  TagsList,
  TagsListItem,
} from 'fumadocs-ui/components/dialog/search';
import { useState } from 'react';
import { useMeilisearch } from 'fumadocs-core/search/client';

export default function CustomSearchDialog(props: SharedProps) {
  const [tag, setTag] = useState<string | undefined>();
  const search = useMeilisearch({
    tag, 
    // other options...
  });

  return (
    <SearchDialog {...search} {...props}>
      <SearchDialogOverlay />
      <SearchDialogContent>
        ...
        <SearchDialogFooter className="flex flex-row">
          <TagsList tag={tag} onTagChange={setTag}>
            <TagsListItem value="my-value">My Value</TagsListItem>
          </TagsList>
        </SearchDialogFooter>
      </SearchDialogContent>
    </SearchDialog>
  );
}

How is this guide?

Last updated on