From e37e5cb45a6c3507b906b9955897ce4e84adf500 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Mon, 17 Apr 2023 09:31:57 +0530 Subject: [PATCH] fix(search): ready event is not fired on mac --- docs/reference/default-theme-search.md | 4 ---- src/node/plugins/localSearchPlugin.ts | 22 +++++----------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/docs/reference/default-theme-search.md b/docs/reference/default-theme-search.md index d27e9159..6f5925ca 100644 --- a/docs/reference/default-theme-search.md +++ b/docs/reference/default-theme-search.md @@ -20,10 +20,6 @@ Example result: ![screenshot of the search modal](/search.png) -::: info -On dev server, the search index is generated on the fly and contains only the recently edited files. So don't worry if you don't see any results when you first open the search modal. It should work fine in production. -::: - Alternatively, you can use [Algolia DocSearch](#algolia-search) or some community plugins like or . ### i18n {#local-search-i18n} diff --git a/src/node/plugins/localSearchPlugin.ts b/src/node/plugins/localSearchPlugin.ts index c0a8b086..c12d03d3 100644 --- a/src/node/plugins/localSearchPlugin.ts +++ b/src/node/plugins/localSearchPlugin.ts @@ -72,7 +72,7 @@ export async function localSearchPlugin( let server: ViteDevServer | undefined - async function onIndexUpdated() { + function onIndexUpdated() { if (server) { server.moduleGraph.onFileChange(LOCAL_SEARCH_INDEX_REQUEST_PATH) // HMR @@ -146,22 +146,10 @@ export async function localSearchPlugin( return { name: 'vitepress:local-search', - configureServer(_server) { + async configureServer(_server) { server = _server - - server.watcher.on('ready', async () => { - const watched = server!.watcher.getWatched() - const files = Object.keys(watched).reduce((acc, dir) => { - acc.push( - ...watched[dir] - .map((file) => dir + '/' + file) - .filter((file) => file.endsWith('.md')) - ) - return acc - }, [] as string[]) - await indexAllFiles(files) - onIndexUpdated() - }) + await scanForBuild() + onIndexUpdated() }, resolveId(id) { @@ -203,7 +191,7 @@ export async function localSearchPlugin( } const index = getIndexForPath(ctx.file) const sections = splitPageIntoSections( - await md.render(await fs.readFile(ctx.file, 'utf-8')) + md.render(await fs.readFile(ctx.file, 'utf-8')) ) for (const section of sections) { const id = `${fileId}#${section.anchor}`