fix(plugin-search): fix search index race condition on pageUpdated, close #888 (#889)

This commit is contained in:
pengzhanbo 2026-04-14 15:29:58 +08:00 committed by GitHub
parent cfc89adab8
commit 28963eb419
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -165,6 +165,14 @@ export async function prepareSearchIndex({
} }
} }
let updateQueue = Promise.resolve()
async function queueUpdateIndexFile({ page, isSearchable, searchOptions }: UpdateSearchIndexOptions) {
updateQueue = updateQueue
.then(() => indexFile(page, searchOptions, isSearchable))
return updateQueue
}
/** /**
* Handle search index update when a page is modified. * Handle search index update when a page is modified.
* *
@ -187,7 +195,8 @@ export async function onSearchIndexUpdated(
if (isSearchable && !isSearchable(page)) if (isSearchable && !isSearchable(page))
return return
await indexFile(page, searchOptions, isSearchable) // FIXME: onPageUpdated 存在竞态问题,当前使用异步队列避免
await queueUpdateIndexFile({ page, isSearchable, searchOptions })
await writeTemp(app) await writeTemp(app)
} }
@ -297,8 +306,11 @@ async function indexFile(page: Page, options: SearchIndexOptions['searchOptions'
${page.contentRendered}` ${page.contentRendered}`
const sections = splitPageIntoSections(html) const sections = splitPageIntoSections(html)
if (cache && cache.length) try {
index.removeAll(cache) if (cache && cache.length)
index.removeAll(cache)
}
catch {}
// add sections to the locale index // add sections to the locale index
for await (const section of sections) { for await (const section of sections) {