From 55c39cbab8a45d90dc402b6b8163adb318ae6128 Mon Sep 17 00:00:00 2001 From: anime Date: Tue, 8 Jul 2025 01:41:50 +0800 Subject: [PATCH] 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 => {