diff --git a/src/app/search/page.tsx b/src/app/search/page.tsx index 221d304..a10fed1 100644 --- a/src/app/search/page.tsx +++ b/src/app/search/page.tsx @@ -372,11 +372,11 @@ function SearchPageClient() { const yearsSet = new Set(); searchResults.forEach((item) => { - if (item.source && item.source_name) { + if (item.source && item.source_name && item.source.trim() !== '' && item.source_name.trim() !== '') { sourcesSet.set(item.source, item.source_name); } - if (item.title) titlesSet.add(item.title); - if (item.year) yearsSet.add(item.year); + if (item.title && item.title.trim() !== '') titlesSet.add(item.title); + if (item.year && item.year.trim() !== '') yearsSet.add(item.year); }); const sourceOptions: { label: string; value: string }[] = [ diff --git a/src/lib/db.client.ts b/src/lib/db.client.ts index 9adf9c4..5fcfcde 100644 --- a/src/lib/db.client.ts +++ b/src/lib/db.client.ts @@ -860,13 +860,15 @@ export async function getSearchHistory(): Promise { // 返回缓存数据,同时后台异步更新 fetchFromApi(`/api/searchhistory`) .then((freshData) => { + // 去重处理 + const uniqueData = Array.from(new Set(freshData)); // 只有数据真正不同时才更新缓存 - if (JSON.stringify(cachedData) !== JSON.stringify(freshData)) { - cacheManager.cacheSearchHistory(freshData); + if (JSON.stringify(cachedData) !== JSON.stringify(uniqueData)) { + cacheManager.cacheSearchHistory(uniqueData); // 触发数据更新事件 window.dispatchEvent( new CustomEvent('searchHistoryUpdated', { - detail: freshData, + detail: uniqueData, }) ); } @@ -881,8 +883,10 @@ export async function getSearchHistory(): Promise { // 缓存为空,直接从 API 获取并缓存 try { const freshData = await fetchFromApi(`/api/searchhistory`); - cacheManager.cacheSearchHistory(freshData); - return freshData; + // 去重处理 + const uniqueData = Array.from(new Set(freshData)); + cacheManager.cacheSearchHistory(uniqueData); + return uniqueData; } catch (err) { console.error('获取搜索历史失败:', err); triggerGlobalError('获取搜索历史失败'); @@ -896,8 +900,9 @@ export async function getSearchHistory(): Promise { const raw = localStorage.getItem(SEARCH_HISTORY_KEY); if (!raw) return []; const arr = JSON.parse(raw) as string[]; - // 仅返回字符串数组 - return Array.isArray(arr) ? arr : []; + // 仅返回字符串数组,并去重 + const validArray = Array.isArray(arr) ? arr : []; + return Array.from(new Set(validArray)); } catch (err) { console.error('读取搜索历史失败:', err); triggerGlobalError('读取搜索历史失败');