From 1e70ea0bab2d6c74536b4c04545181a523e6678d Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Thu, 3 Mar 2022 09:19:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85=E6=9F=A5=E8=AF=A2=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/search/[keyword].js | 76 ++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index 1ab5584e..90a2bd62 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -4,6 +4,41 @@ import BLOG from '@/blog.config' import { useGlobal } from '@/lib/global' import { getDataFromCache } from '@/lib/cache/cache_manager' +export async function getServerSideProps ({ params: { keyword } }) { + const { + allPosts, + categories, + tags, + postCount, + latestPosts, + customNav + } = await getGlobalNotionData({ from: 'search-props', pageType: ['Post'] }) + + const filterPosts = await filterByMemCache(allPosts, keyword) + return { + props: { + posts: filterPosts, + tags, + categories, + postCount, + latestPosts, + customNav, + keyword + } + } +} + +const Index = (props) => { + const { keyword } = props + const { locale } = useGlobal() + const meta = { + title: `${keyword || ''} | ${locale.NAV.SEARCH} | ${BLOG.TITLE} `, + description: BLOG.DESCRIPTION, + type: 'website' + } + return +} + /** * 将对象的指定字段拼接到字符串 * @param sourceTextArray @@ -47,16 +82,13 @@ function getTextContent (textArray) { */ const isIterable = obj => obj != null && typeof obj[Symbol.iterator] === 'function' -export async function getServerSideProps ({ params: { keyword } }) { - const { - allPosts, - categories, - tags, - postCount, - latestPosts, - customNav - } = await getGlobalNotionData({ from: 'search-props', pageType: ['Post'] }) - +/** + * 在内存缓存中进行全文索引 + * @param {*} allPosts + * @param keyword 关键词 + * @returns + */ +async function filterByMemCache (allPosts, keyword) { const filterPosts = [] for (const post of allPosts) { const cacheKey = 'page_block_' + post.id @@ -97,29 +129,7 @@ export async function getServerSideProps ({ params: { keyword } }) { filterPosts.push(post) } } - - return { - props: { - posts: filterPosts, - tags, - categories, - postCount, - latestPosts, - customNav, - keyword - } - } -} - -const Index = (props) => { - const { keyword } = props - const { locale } = useGlobal() - const meta = { - title: `${keyword || ''} | ${locale.NAV.SEARCH} | ${BLOG.TITLE} `, - description: BLOG.DESCRIPTION, - type: 'website' - } - return + return filterPosts } export default Index