Mixedbread
Fumadocs UI で Mixedbread を使用する方法
セットアップ
Mixedbread 検索はリクエスト時にサーバー上で実行されるため、静的サイトでは利用できません。
Fumadocs CLI を使えば、セットアップを自動で行えます。検索ダイアログ、検索ルート、static.json ルートに加え、ビルドのたびにストアを同期するスクリプトが作成されます。
npx @fumadocs/cli feature search --provider mixedbreadまたは、手動で設定する場合:
-
Mixedbread Search を統合します。
-
検索ダイアログのコンポーネントを作成します。
components/search.tsx 'use client'; import { SearchDialog, SearchDialogClose, SearchDialogContent, SearchDialogFooter, SearchDialogHeader, SearchDialogIcon, SearchDialogInput, SearchDialogList, SearchDialogOverlay, type SharedProps, } from 'fumadocs-ui/components/dialog/search'; import { useFetchSearch } from 'fumadocs-core/search/client'; import { useI18n } from 'fumadocs-ui/contexts/i18n'; export default function CustomSearchDialog(props: SharedProps) { const { locale } = useI18n(); // (optional) for i18n const search = useFetchSearch({ api: '/api/search', locale, // every search is a request to Mixedbread delayMs: 300, }); return ( <SearchDialog {...search} {...props}> <SearchDialogOverlay /> <SearchDialogContent> <SearchDialogHeader> <SearchDialogIcon /> <SearchDialogInput /> <SearchDialogClose /> </SearchDialogHeader> <SearchDialogList /> <SearchDialogFooter> <a href="https://mixedbread.com" rel="noreferrer noopener" className="ms-auto text-xs text-fd-muted-foreground" > Search powered by Mixedbread </a> </SearchDialogFooter> </SearchDialogContent> </SearchDialog> ); }
検索ダイアログを置き換える
ルートレイアウトで、独自の検索ダイアログを <RootProvider /> に渡します。
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>
);
}タグフィルター
必要に応じて、検索結果をタグで絞り込むための UI を追加できます。検索サーバーでタグフィルターを設定したうえで、以下を追加してください。
'use client';
import {
SearchDialog,
SearchDialogContent,
SearchDialogFooter,
SearchDialogOverlay,
type SharedProps,
TagsList,
TagsListItem,
} from 'fumadocs-ui/components/dialog/search';
import { useState } from 'react';
import { useFetchSearch } from 'fumadocs-core/search/client';
export default function CustomSearchDialog(props: SharedProps) {
const [tag, setTag] = useState<string | undefined>();
const search = useFetchSearch({
tag,
});
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?
最終更新日
