Skip to content

Commit 8faaea3

Browse files
committed
fix: orama
1 parent 21bd5a2 commit 8faaea3

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

routes/api/search.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,18 @@ async function buildIndex(): Promise<SearchResult[]> {
207207
const db = ensureOrama();
208208
if (db) {
209209
// Prepare docs for Orama inserting. Keep id as url to make it stable.
210-
const docs = items.map((it) => ({
210+
// Deduplicate by URL to avoid inserting the same id multiple times which
211+
// causes Orama DOCUMENT_ALREADY_EXISTS errors (seen e.g. for "/docs/index").
212+
const seen = new Set<string>();
213+
const uniqueItems = items.filter((it) => {
214+
const id = String(it.url || "");
215+
if (!id) return true;
216+
if (seen.has(id)) return false;
217+
seen.add(id);
218+
return true;
219+
});
220+
221+
const docs = uniqueItems.map((it) => ({
211222
id: it.url,
212223
title: it.title,
213224
excerpt: it.excerpt || "",
@@ -229,16 +240,32 @@ async function buildIndex(): Promise<SearchResult[]> {
229240
}
230241
})(),
231242
}));
243+
232244
if (typeof insertMultiple === "function") {
233-
Promise.resolve(insertMultiple(db, docs))
234-
.then(() => {})
235-
.catch((e: Error) => {
236-
console.warn("Orama insertMultiple failed:", e);
237-
});
245+
// insertMultiple may throw synchronously or return a Promise.
246+
// Catch both to prevent the outer try/catch from receiving the error
247+
// and to log the failure without breaking the search API.
248+
try {
249+
const res = insertMultiple(db, docs);
250+
if (res && typeof (res as Promise<unknown>).then === "function") {
251+
(res as Promise<unknown>).catch((e: Error) => {
252+
console.warn("Orama insertMultiple failed:", e);
253+
});
254+
}
255+
} catch (e) {
256+
console.warn("Orama insertMultiple failed:", e);
257+
}
238258
} else {
239259
for (const d of docs) {
240260
// @ts-ignore: best-effort insert, allow unknown signature
241-
insert(db, d).catch(() => {});
261+
try {
262+
const r = insert(db, d);
263+
if (r && typeof (r as Promise<unknown>).then === "function") {
264+
(r as Promise<unknown>).catch(() => {});
265+
}
266+
} catch {
267+
// ignore individual insert errors
268+
}
242269
}
243270
}
244271
}

0 commit comments

Comments
 (0)