fix(build): deterministic local search indexing and non-blocking initial scan (closes #4081)

pull/4522/merge
Divyansh Singh 6 months ago
parent cf9994d3c2
commit fc0f203f0a

@ -2,7 +2,6 @@ import { createDebug } from 'obug'
import fs from 'fs-extra'
import MiniSearch from 'minisearch'
import path from 'node:path'
import pMap from 'p-map'
import type { Plugin, ViteDevServer } from 'vite'
import type { SiteConfig } from '../config'
import type { DefaultTheme } from '../defaultTheme'
@ -81,9 +80,10 @@ export async function localSearchPlugin(
}
let server: ViteDevServer | undefined
let pending: Promise<void>
function onIndexUpdated() {
if (server) {
if (!server) return
server.moduleGraph.onFileChange(LOCAL_SEARCH_INDEX_REQUEST_PATH)
// HMR
const mod = server.moduleGraph.getModuleById(
@ -102,7 +102,6 @@ export async function localSearchPlugin(
]
})
}
}
function getDocId(file: string) {
let relFile = slash(path.relative(siteConfig.srcDir, file))
@ -142,9 +141,9 @@ export async function localSearchPlugin(
async function scanForBuild() {
debug('🔍️ Indexing files for search...')
await pMap(siteConfig.pages, indexFile, {
concurrency: siteConfig.buildConcurrency
})
for (const page of siteConfig.pages) {
await indexFile(page)
}
debug('✅ Indexing finished...')
}
@ -161,10 +160,9 @@ export async function localSearchPlugin(
}
}),
async configureServer(_server) {
configureServer(_server) {
server = _server
await scanForBuild()
onIndexUpdated()
pending = scanForBuild().then(onIndexUpdated)
},
resolveId(id) {
@ -175,6 +173,7 @@ export async function localSearchPlugin(
async load(id) {
if (id === LOCAL_SEARCH_INDEX_REQUEST_PATH) {
await pending
if (process.env.NODE_ENV === 'production') {
await scanForBuild()
}
@ -188,6 +187,7 @@ export async function localSearchPlugin(
}
return `export default {${records.join(',')}}`
} else if (id.startsWith(LOCAL_SEARCH_INDEX_REQUEST_PATH)) {
await pending
return `export default ${JSON.stringify(
JSON.stringify(
indexByLocales.get(

Loading…
Cancel
Save