Add keyword length limit to search function to prevent search overload

This commit is contained in:
PrinOrange
2024-01-09 16:57:42 +08:00
parent bcde008408
commit 4196dfc641
2 changed files with 8 additions and 0 deletions

View File

@@ -11,6 +11,10 @@ export default function handler(req: NextApiRequest, res: NextApiResponse<Respon
res.status(200).json([]); res.status(200).json([]);
return; return;
} }
if (searchText.length < 10) {
res.status(200).json([]);
return;
}
const result: TSearchResultItem[] = SearchIndex.search(searchText).map((item) => ({ const result: TSearchResultItem[] = SearchIndex.search(searchText).map((item) => ({
id: item.id, id: item.id,
title: item.title, title: item.title,

View File

@@ -52,6 +52,10 @@ export default function SearchPage() {
toast({ title: "Enter a Keyword", description: "Please enter one keyword at least." }); toast({ title: "Enter a Keyword", description: "Please enter one keyword at least." });
return; return;
} }
if (searchText.length < 10) {
toast({ title: "Keywords too short", description: "Keyword length must be at least 10." });
return;
}
querySearch.refetch(); querySearch.refetch();
}; };