From 03db4bfb26d35ee654c4eca759464116abaf6203 Mon Sep 17 00:00:00 2001 From: chengpeiquan Date: Wed, 30 Oct 2024 00:28:50 +0800 Subject: [PATCH] chore: debugging deployment issues --- src/app/api/search/route.ts | 28 ++++++++++++++++++++++ src/cache/content-cache/index.ts | 41 ++++++++++++++++---------------- src/core/search.ts | 12 ++++++++-- 3 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 src/app/api/search/route.ts diff --git a/src/app/api/search/route.ts b/src/app/api/search/route.ts new file mode 100644 index 000000000..370a90052 --- /dev/null +++ b/src/app/api/search/route.ts @@ -0,0 +1,28 @@ +import { getContentCache } from '@/cache/content-cache' + +export async function POST(req: Request) { + try { + const body = await req.json() + console.log('-----') + console.log(body) + console.log('-----') + + const { folder, locale } = body + + const data = await getContentCache(folder, locale) + console.log('-----') + console.log(data) + console.log('-----') + + return Response.json({ + code: 0, + data, + }) + } catch (e) { + console.error(e) + + return Response.json({ + code: 1, + }) + } +} diff --git a/src/cache/content-cache/index.ts b/src/cache/content-cache/index.ts index 950ab4641..12e96cc17 100644 --- a/src/cache/content-cache/index.ts +++ b/src/cache/content-cache/index.ts @@ -1,36 +1,35 @@ -import { - // ContentFolder, - type ListFolder, -} from '@/config/content-config' +'use server' + +import { ContentFolder, type ListFolder } from '@/config/content-config' import { type Locale } from '@/config/locale-config' import { type CacheMapKey, type ContentCacheItem, getCacheMapKey, } from '@/config/cache-config' -// import article_en from './article-en.json' -// import article_zh from './article-zh.json' -// import cookbook_zh from './cookbook-zh.json' +import article_en from './article-en.json' +import article_zh from './article-zh.json' +import cookbook_zh from './cookbook-zh.json' const contentCacheMap = new Map([ - // [ - // getCacheMapKey(ContentFolder.Article, 'zh'), - // article_zh as ContentCacheItem[], - // ], - // [ - // getCacheMapKey(ContentFolder.Article, 'en'), - // article_en as ContentCacheItem[], - // ], - // [ - // getCacheMapKey(ContentFolder.Cookbook, 'zh'), - // cookbook_zh as ContentCacheItem[], - // ], + [ + getCacheMapKey(ContentFolder.Article, 'zh'), + article_zh as ContentCacheItem[], + ], + [ + getCacheMapKey(ContentFolder.Article, 'en'), + article_en as ContentCacheItem[], + ], + [ + getCacheMapKey(ContentFolder.Cookbook, 'zh'), + cookbook_zh as ContentCacheItem[], + ], ]) -export const getContentCache = ( +export const getContentCache = async ( folder: ListFolder, locale: Locale, -): ContentCacheItem[] => { +): Promise => { const key = getCacheMapKey(folder, locale) const cache = contentCacheMap.get(key) return cache || [] diff --git a/src/core/search.ts b/src/core/search.ts index 9ac2b349c..ee90747af 100644 --- a/src/core/search.ts +++ b/src/core/search.ts @@ -6,7 +6,6 @@ import { } from '@/config/cache-config' import { type Locale } from '@/config/locale-config' import { type ListFolder } from '@/config/content-config' -import { getContentCache } from '@/cache/content-cache' interface SearchDocumentItem extends Omit { id: FlexSearch.Id @@ -49,7 +48,16 @@ export const getSearchEngine = async (folder: ListFolder, locale: Locale) => { const engine = searchEngineMap.get(key) if (engine) return engine - const data = getContentCache(...args) + // const data = await getContentCache(...args) + const res = await fetch('https://chengpeiquan.com/api/search', { + method: 'POST', + body: JSON.stringify({ + folder: 'article', + locale: 'zh', + }), + }) + // @ts-ignore + const data = res.data const instance = await initializeEngine(data) searchEngineMap.set(key, instance)