From 608ab06c40d55dc8fc80b3865464c81cc5bc17a3 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 2 Mar 2022 14:43:30 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BD=BF=E7=94=A8getServerSideProps?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/search/[keyword].js | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index 3e74b996..f3b7ed45 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -4,13 +4,6 @@ import BLOG from '@/blog.config' import { useGlobal } from '@/lib/global' import { getDataFromCache } from '@/lib/cache/cache_manager' -export async function getStaticPaths () { - return { - paths: [], - fallback: true - } -} - /** * 将对象的指定字段拼接到字符串 * @param sourceTextArray @@ -38,16 +31,16 @@ function appendText (sourceTextArray, targetObj, key) { function getTextContent (textArray) { if (typeof textArray === 'object') { let result = '' - textArray.forEach(textObj => { + for (const textObj of textArray) { result = result + getTextContent(textObj) - }) + } return result } else if (typeof textArray === 'string') { return textArray } } -export async function getStaticProps ({ params: { keyword } }) { +export async function getServerSideProps ({ params: { keyword } }) { const { allPosts, categories, @@ -66,7 +59,7 @@ export async function getStaticProps ({ params: { keyword } }) { const articleInfo = post.title + post.summary + tagContent + categoryContent let hit = articleInfo.indexOf(keyword) > -1 let indexContent = [post.summary] - console.log('搜索是否命中缓存', page !== null) + // console.log('搜索是否命中缓存', page !== null) if (page !== null) { const contentIds = Object.keys(page.block) contentIds.forEach(id => { @@ -77,21 +70,21 @@ export async function getStaticProps ({ params: { keyword } }) { } post.results = [] let hitCount = 0 - const re = new RegExp(`${keyword}`, 'g') - indexContent.forEach((c, i) => { + const re = new RegExp(`${keyword}`, 'gim') + for (const i in indexContent) { + const c = indexContent[i] const index = c.toLowerCase().indexOf(keyword.toLowerCase()) + const referText = c?.replace(re, `${keyword}`) if (index > -1) { hit = true - const referText = c?.replace(re, `${keyword}`) - post.results.push(`${referText}`) hitCount += 1 + post.results.push(`${referText}`) } else { if ((post.results.length - 1) / hitCount < 3 || i === 0) { - post.results.push(`${c}`) + post.results.push(`${referText}`) } } - }) - + } if (hit) { filterPosts.push(post) } @@ -106,8 +99,7 @@ export async function getStaticProps ({ params: { keyword } }) { latestPosts, customNav, keyword - }, - revalidate: 1 + } } } From 7c91dc3fb3edc2355403ff101d9606cb288a8f82 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 2 Mar 2022 15:31:13 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E8=B0=83=E6=95=B4?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E6=8E=A5=E5=8F=A3=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/search/[keyword].js | 8 +++++--- themes/NEXT/components/SearchInput.js | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index f3b7ed45..41b74654 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -3,6 +3,7 @@ import { LayoutSearch } from '@/themes' import BLOG from '@/blog.config' import { useGlobal } from '@/lib/global' import { getDataFromCache } from '@/lib/cache/cache_manager' +import { getPostBlocks } from '@/lib/notion/getPostBlocks' /** * 将对象的指定字段拼接到字符串 @@ -52,14 +53,13 @@ export async function getServerSideProps ({ params: { keyword } }) { const filterPosts = [] for (const post of allPosts) { - const cacheKey = 'page_block_' + post.id - const page = await getDataFromCache(cacheKey) + // const cacheKey = 'page_block_' + post.id + const page = await getPostBlocks(post.id, 'search') const tagContent = post.tags ? post.tags.join(' ') : '' const categoryContent = post.category ? post.category.join(' ') : '' const articleInfo = post.title + post.summary + tagContent + categoryContent let hit = articleInfo.indexOf(keyword) > -1 let indexContent = [post.summary] - // console.log('搜索是否命中缓存', page !== null) if (page !== null) { const contentIds = Object.keys(page.block) contentIds.forEach(id => { @@ -68,6 +68,8 @@ export async function getServerSideProps ({ params: { keyword } }) { indexContent = appendText(indexContent, properties, 'caption') }) } + console.log('搜索是否命中缓存', page !== null, indexContent) + post.results = [] let hitCount = 0 const re = new RegExp(`${keyword}`, 'gim') diff --git a/themes/NEXT/components/SearchInput.js b/themes/NEXT/components/SearchInput.js index 4ee8165a..c3565c43 100644 --- a/themes/NEXT/components/SearchInput.js +++ b/themes/NEXT/components/SearchInput.js @@ -2,6 +2,8 @@ import { useRouter } from 'next/router' import { useGlobal } from '@/lib/global' import { useImperativeHandle, useRef, useState } from 'react' +let lock = false + const SearchInput = ({ currentTag, currentSearch, cRef }) => { const { locale } = useGlobal() // const [searchKey, setSearchKey] = useState(currentSearch || '') @@ -39,7 +41,6 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => { searchInputRef.current.value = '' setShowClean(false) } - let lock = false function lockSearchInput () { lock = true } From a6ce65066f79b0f148a361cfe47ac681c19cf33e Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 2 Mar 2022 16:03:11 +0800 Subject: [PATCH 3/3] build --- pages/search/[keyword].js | 1 - 1 file changed, 1 deletion(-) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index 41b74654..c3bfaeff 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -2,7 +2,6 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData' import { LayoutSearch } from '@/themes' import BLOG from '@/blog.config' import { useGlobal } from '@/lib/global' -import { getDataFromCache } from '@/lib/cache/cache_manager' import { getPostBlocks } from '@/lib/notion/getPostBlocks' /**