Skip to content

Commit 286ef0e

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

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

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

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,56 @@
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+
18+
const results = await Promise.all(
19+
pages.map(async (page): Promise<AdvancedIndex | null> => {
20+
try {
21+
const pageData = page.data as AsyncPageData;
22+
if (!pageData.getText) {
23+
return null;
24+
}
25+
26+
const processed = await pageData.getText("processed");
27+
28+
return {
29+
id: page.url,
30+
title: pageData.title ?? "",
31+
description: pageData.description ?? "",
32+
url: page.url,
33+
structuredData: structure(processed, [remarkMdx]),
34+
};
35+
} catch {
36+
console.error(`Failed to index page: ${page.url}`);
37+
return null;
38+
}
39+
}),
40+
);
41+
42+
return results.filter((index): index is AdvancedIndex => index !== null);
43+
}
44+
45+
async function getSearchAPI() {
46+
if (!searchApi) {
47+
const indexes = await buildIndexes();
48+
searchApi = createSearchAPI("advanced", { indexes });
1249
}
13-
return searchHandler;
50+
return searchApi;
1451
}
1552

16-
export function GET(request: Request) {
17-
const handler = getSearchHandler();
18-
return handler.GET(request);
53+
export async function GET(request: Request) {
54+
const api = await getSearchAPI();
55+
return api.GET(request);
1956
}

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)