From 7ce6ad0c2f77a91c42ffacb6d43619f17f0139fb Mon Sep 17 00:00:00 2001 From: anime Date: Tue, 8 Jul 2025 01:13:09 +0800 Subject: [PATCH 1/4] feat(algolia): add data deletion functionality and execute in index.js - Add `checkDataFromAlgolia` function to identify and delete unused data - Implement `deletePostDataFromAlgolia` helper function - Execute Algolia data check during homepage generation - Handle password-protected and draft pages deletion Fixes 1526 --- lib/plugins/algolia.js | 53 ++++++++++++++++++++++++++++++++++++++++++ pages/index.js | 3 +++ 2 files changed, 56 insertions(+) diff --git a/lib/plugins/algolia.js b/lib/plugins/algolia.js index e6c76422..fecec3a1 100644 --- a/lib/plugins/algolia.js +++ b/lib/plugins/algolia.js @@ -15,6 +15,59 @@ const generateAlgoliaSearch = ({ allPages, force = false }) => { }) } + +/** + * 检查数据是否需要从algolia删除 + * @param {*} props + */ +export const checkDataFromAlgolia = async props => { + const { allPages } = props + const deletions = (allPages || []) + .map(p => { + if (p && (p.password || p.status === 'Draft')) { + return deletePostDataFromAlgolia(p) + } + }) + .filter(Boolean) // 去除 undefined + await Promise.all(deletions) +} + +/** + * 删除数据 + * @param post + */ +const deletePostDataFromAlgolia = async post => { + // Connect and authenticate with your Algolia app + const client = algoliasearch(BLOG.ALGOLIA_APP_ID, BLOG.ALGOLIA_ADMIN_APP_KEY) + + // Create a new index and add a record + const index = client.initIndex(BLOG.ALGOLIA_INDEX) + + if (!post) { + return + } + + // 检查是否有索引 + let existed + try { + existed = await index.getObject(post.id) + } catch (error) { + // 通常是不存在索引 + } + + if (existed) { + await index + .deleteObject(post.id) + .wait() + .then(r => { + console.log('Algolia索引更新', r) + }) + .catch(err => { + console.log('Algolia异常', err) + }) + } +} + /** * 上传数据 * 根据上次修改文章日期和上次更新索引数据判断是否需要更新algolia索引 diff --git a/pages/index.js b/pages/index.js index b66b5610..b4b9937a 100644 --- a/pages/index.js +++ b/pages/index.js @@ -6,6 +6,7 @@ import { generateRss } from '@/lib/rss' import { generateSitemapXml } from '@/lib/sitemap.xml' import { DynamicLayout } from '@/themes/theme' import { generateRedirectJson } from '@/lib/redirect' +import { checkDataFromAlgolia } from '@/lib/plugins/algolia' /** * 首页布局 @@ -61,6 +62,8 @@ export async function getStaticProps(req) { generateRss(props) // 生成 generateSitemapXml(props) + // 检查数据是否需要从algolia删除 + checkDataFromAlgolia(props) if (siteConfig('UUID_REDIRECT', false, props?.NOTION_CONFIG)) { // 生成重定向 JSON generateRedirectJson(props) From 8d200120fc21c23605f6c00f1aaa77c2c0247720 Mon Sep 17 00:00:00 2001 From: anime Date: Tue, 8 Jul 2025 01:14:20 +0800 Subject: [PATCH 2/4] feat(algolia): add data deletion functionality and execute in index.js - Add `checkDataFromAlgolia` function to identify and delete unused data - Implement `deletePostDataFromAlgolia` helper function - Execute Algolia data check during homepage generation - Handle password-protected and draft pages deletion --- lib/plugins/algolia.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/algolia.js b/lib/plugins/algolia.js index fecec3a1..f3485ab7 100644 --- a/lib/plugins/algolia.js +++ b/lib/plugins/algolia.js @@ -60,7 +60,7 @@ const deletePostDataFromAlgolia = async post => { .deleteObject(post.id) .wait() .then(r => { - console.log('Algolia索引更新', r) + console.log('Algolia索引删除成功', r) }) .catch(err => { console.log('Algolia异常', err) From 55c39cbab8a45d90dc402b6b8163adb318ae6128 Mon Sep 17 00:00:00 2001 From: anime Date: Tue, 8 Jul 2025 01:41:50 +0800 Subject: [PATCH 3/4] feat(algolia): implement global client initialization and reuse - Add global Algolia client and index initialization - Initialize client once and reuse across functions - Remove redundant client initialization in individual functions - Add configuration validation during initialization --- lib/plugins/algolia.js | 46 ++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/lib/plugins/algolia.js b/lib/plugins/algolia.js index f3485ab7..439451f1 100644 --- a/lib/plugins/algolia.js +++ b/lib/plugins/algolia.js @@ -2,6 +2,31 @@ import BLOG from '@/blog.config' import { getPageContentText } from '@/pages/search/[keyword]' import algoliasearch from 'algoliasearch' +// 全局初始化 Algolia 客户端和索引 +let algoliaClient +let algoliaIndex + +const initAlgolia = () => { + if (!algoliaClient) { + if ( + !BLOG.ALGOLIA_APP_ID || + !BLOG.ALGOLIA_ADMIN_APP_KEY || + !BLOG.ALGOLIA_INDEX + ) { + throw new Error('Algolia configuration is missing') + } + algoliaClient = algoliasearch( + BLOG.ALGOLIA_APP_ID, + BLOG.ALGOLIA_ADMIN_APP_KEY + ) + algoliaIndex = algoliaClient.initIndex(BLOG.ALGOLIA_INDEX) + } + return { client: algoliaClient, index: algoliaIndex } +} + +// 初始化全局实例 +initAlgolia() + /** * 生成全文索引 * @param {*} allPages @@ -15,7 +40,6 @@ const generateAlgoliaSearch = ({ allPages, force = false }) => { }) } - /** * 检查数据是否需要从algolia删除 * @param {*} props @@ -37,12 +61,6 @@ export const checkDataFromAlgolia = async props => { * @param post */ const deletePostDataFromAlgolia = async post => { - // Connect and authenticate with your Algolia app - const client = algoliasearch(BLOG.ALGOLIA_APP_ID, BLOG.ALGOLIA_ADMIN_APP_KEY) - - // Create a new index and add a record - const index = client.initIndex(BLOG.ALGOLIA_INDEX) - if (!post) { return } @@ -50,13 +68,13 @@ const deletePostDataFromAlgolia = async post => { // 检查是否有索引 let existed try { - existed = await index.getObject(post.id) + existed = await algoliaIndex.getObject(post.id) } catch (error) { // 通常是不存在索引 } if (existed) { - await index + await algoliaIndex .deleteObject(post.id) .wait() .then(r => { @@ -73,12 +91,6 @@ const deletePostDataFromAlgolia = async post => { * 根据上次修改文章日期和上次更新索引数据判断是否需要更新algolia索引 */ const uploadDataToAlgolia = async post => { - // Connect and authenticate with your Algolia app - const client = algoliasearch(BLOG.ALGOLIA_APP_ID, BLOG.ALGOLIA_ADMIN_APP_KEY) - - // Create a new index and add a record - const index = client.initIndex(BLOG.ALGOLIA_INDEX) - if (!post) { return } @@ -87,7 +99,7 @@ const uploadDataToAlgolia = async post => { let existed let needUpdateIndex = false try { - existed = await index.getObject(post.id) + existed = await algoliaIndex.getObject(post.id) } catch (error) { // 通常是不存在索引 } @@ -117,7 +129,7 @@ const uploadDataToAlgolia = async post => { content: truncate(getPageContentText(post, post.blockMap), 8192) // 索引8192个字符,API限制总请求内容上限1万个字节 } // console.log('更新Algolia索引', record) - index + algoliaIndex .saveObject(record) .wait() .then(r => { From 93df743559a95f0bf9599fb2d94f8c4b5d9bd248 Mon Sep 17 00:00:00 2001 From: anime Date: Tue, 8 Jul 2025 15:37:59 +0800 Subject: [PATCH 4/4] feat(algolia): replace error throw with console.error for missing config Change error handling from throwing an exception to logging an error when Algolia configuration is missing. This provides better error reporting without breaking the application flow. --- lib/plugins/algolia.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/algolia.js b/lib/plugins/algolia.js index 439451f1..2421d142 100644 --- a/lib/plugins/algolia.js +++ b/lib/plugins/algolia.js @@ -13,7 +13,7 @@ const initAlgolia = () => { !BLOG.ALGOLIA_ADMIN_APP_KEY || !BLOG.ALGOLIA_INDEX ) { - throw new Error('Algolia configuration is missing') + console.error('Algolia configuration is missing') } algoliaClient = algoliasearch( BLOG.ALGOLIA_APP_ID,