fix(search): ready event is not fired on mac

pull/2248/head
Divyansh Singh 2 years ago
parent 67f1f912d9
commit e37e5cb45a

@ -20,10 +20,6 @@ Example result:
![screenshot of the search modal](/search.png) ![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 <https://www.npmjs.com/package/vitepress-plugin-search> or <https://www.npmjs.com/package/vitepress-plugin-pagefind>. Alternatively, you can use [Algolia DocSearch](#algolia-search) or some community plugins like <https://www.npmjs.com/package/vitepress-plugin-search> or <https://www.npmjs.com/package/vitepress-plugin-pagefind>.
### i18n {#local-search-i18n} ### i18n {#local-search-i18n}

@ -72,7 +72,7 @@ export async function localSearchPlugin(
let server: ViteDevServer | undefined let server: ViteDevServer | undefined
async function onIndexUpdated() { function onIndexUpdated() {
if (server) { if (server) {
server.moduleGraph.onFileChange(LOCAL_SEARCH_INDEX_REQUEST_PATH) server.moduleGraph.onFileChange(LOCAL_SEARCH_INDEX_REQUEST_PATH)
// HMR // HMR
@ -146,22 +146,10 @@ export async function localSearchPlugin(
return { return {
name: 'vitepress:local-search', name: 'vitepress:local-search',
configureServer(_server) { async configureServer(_server) {
server = _server server = _server
await scanForBuild()
server.watcher.on('ready', async () => { onIndexUpdated()
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()
})
}, },
resolveId(id) { resolveId(id) {
@ -203,7 +191,7 @@ export async function localSearchPlugin(
} }
const index = getIndexForPath(ctx.file) const index = getIndexForPath(ctx.file)
const sections = splitPageIntoSections( 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) { for (const section of sections) {
const id = `${fileId}#${section.anchor}` const id = `${fileId}#${section.anchor}`

Loading…
Cancel
Save