Skip to content

Commit e26e17c

Browse files
committed
fix: Fix docs search.
1 parent 4a04843 commit e26e17c

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

apps/docs/app/api/search/route.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
1-
import { createFromSource } from "fumadocs-core/search/server";
1+
import { structure } from "fumadocs-core/mdx-plugins";
2+
import {
3+
type AdvancedIndex,
4+
createSearchAPI,
5+
} from "fumadocs-core/search/server";
6+
import remarkMdx from "remark-mdx";
7+
import type { AsyncPageData } from "@/app/docs/[[...slug]]/page";
28
import { source } from "@/lib/source";
39

410
export const dynamic = "force-dynamic";
511

612
// Lazy initialization to avoid build-time errors with async source
7-
let searchHandler: ReturnType<typeof createFromSource> | null = null;
13+
let searchApi: ReturnType<typeof createSearchAPI<"advanced">> | null = null;
814

9-
function getSearchHandler() {
10-
if (!searchHandler) {
11-
searchHandler = createFromSource(source);
15+
async function buildIndexes(): Promise<AdvancedIndex[]> {
16+
const pages = source.getPages();
17+
const indexes: AdvancedIndex[] = [];
18+
19+
for (const page of pages) {
20+
const pageData = page.data as AsyncPageData;
21+
const processed = await pageData.getText("processed");
22+
23+
indexes.push({
24+
id: page.url,
25+
title: pageData.title ?? "",
26+
description: pageData.description ?? "",
27+
url: page.url,
28+
structuredData: structure(processed, [remarkMdx]),
29+
});
30+
}
31+
return indexes;
32+
}
33+
34+
async function getSearchAPI() {
35+
if (!searchApi) {
36+
const indexes = await buildIndexes();
37+
searchApi = createSearchAPI("advanced", { indexes });
1238
}
13-
return searchHandler;
39+
return searchApi;
1440
}
1541

16-
export function GET(request: Request) {
17-
const handler = getSearchHandler();
18-
return handler.GET(request);
42+
export async function GET(request: Request) {
43+
const api = await getSearchAPI();
44+
return api.GET(request);
1945
}

apps/docs/app/docs/[[...slug]]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { onRateDocs } from "@/lib/feedback-action";
88
import { getPageImage, source } from "@/lib/source";
99
import { getMDXComponents } from "@/mdx-components";
1010

11-
type AsyncPageData = DocMethods & {
11+
export type AsyncPageData = DocMethods & {
1212
title?: string;
1313
description?: string;
1414
load: () => Promise<DocData>;

apps/docs/source.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export const docs = defineDocs({
55
docs: {
66
schema: frontmatterSchema,
77
async: true,
8+
postprocess: {
9+
includeProcessedMarkdown: true,
10+
},
811
},
912
meta: {
1013
schema: metaSchema,

0 commit comments

Comments
 (0)