Skip to content

fix(search): drop pages removed from disk in dev - #5483

Open
rubenmarcus wants to merge 2 commits into
vuejs:mainfrom
rubenmarcus:fix/local-search-unlink-stale
Open

rubenmarcus wants to merge 2 commits into
vuejs:mainfrom
rubenmarcus:fix/local-search-unlink-stale

Conversation

@rubenmarcus

@rubenmarcus rubenmarcus commented Oct 5, 2026 •

Copy link
Copy Markdown

Fixes #5481.

In vitepress dev, localSearchPlugin only ever writes into MiniSearch. hotUpdate guards on siteConfig.pages.includes(page), so a .md file that has just been deleted never reaches indexFile, and the plugin registers no watcher unlink handler at all. MiniSearch holds a document until discard is called, so every section of a removed page stays searchable and keeps linking to a route that now 404s. After git mv both the old and the new path show up for the same content. A dev server restart is the only thing that clears it.

The plugin now records the ids each page puts into its locale index and discards them again on unlink:

const indexedIdsByPage = new Map<string, Set<string>>()

indexFile adds every id it stores, and configureServer handles the watcher event:

server.watcher.on('unlink', (file) => {
  if (!file.endsWith('.md')) return
  const page = slash(path.relative(siteConfig.srcDir, file))
  const ids = indexedIdsByPage.get(page)
  // not a page we indexed: an include, a route template, a rewrite source
  if (!ids?.size) return
  const index = getIndexByLocale(
    getLocaleForPath(siteConfig.site, siteConfig.rewrites.map[page] || page)
  )
  for (const id of ids) index.has(id) && index.discard(id)
  indexedIdsByPage.delete(page)
  debug('🔍️ Removed', file)
  onIndexUpdated()
})

The locale is resolved through rewrites.map exactly like indexFile does, so a page indexed under a rewritten path lands in the right index, and onIndexUpdated() invalidates the virtual @localSearchIndex module and sends the HMR update the same way the edit path does.

Verification, three new tests in __tests__/unit/node/plugins/localSearchPlugin.test.ts:

$ npx vitest run -r __tests__/unit node/plugins/localSearchPlugin.test.ts
# pristine upstream/main @ 0ad6d72d (includes #5509) with these tests transplanted: 2 failed, 6 passed (8)
#   x drops a deleted page from the index when its file is unlinked
#   x keeps only the new path after a page is renamed
# with this change: 8 passed (8)

$ npx vitest run -r __tests__/unit
# 32 passed (32) / 518 passed, 2 skipped (520), 0 failed

$ npx tsc -p tsconfig.node.json --noEmit && npx tsc -p __tests__/unit --noEmit
# clean

The three tests drive the plugin the way vite does: a stub dev server (watcher emitter plus a moduleGraph mock), configResolved, configureServer, then load to read the serialized per-locale index back through loadLocalSearchIndex (after merging #5509, which now ships that helper with the same options the plugin used, including its tokenizer). Deletion asserts the token stops matching while a sibling page survives; the rename case asserts the keyword resolves to /guide/new.html#renamed-guide only and that index.has('/guide/old.html#renamed-guide') is false; a third test asserts an unlink for a file that was never indexed (a partial, a non-markdown file) does not touch the index or bump the invalidation counter.

Tradeoffs: this only covers pages the dev server already knows about. A brand new file created after startup is still not indexed until the next restart, since siteConfig.pages is resolved once and hotUpdate gates on it; that is unchanged here and out of scope for this issue. Discarding is keyed by ids the page actually stored, so a section dropped by a later edit of the same page keeps its current behavior and is left to the stale-section work in #5447, which touches the same file but a different case. index.has(id) && index.discard(id) guards the case where the index was already rebuilt without the entry.

Prepared with AI assistance (GLM 5.3 via Oh My Pi) and reviewed before submission.

@rubenmarcus

Copy link
Copy Markdown
Author

GitHub holds both workflows on this PR (Test, CR) until a maintainer approves runs from this fork, so the branch shows no checks. The local runs are in the description: npx vitest run -r __tests__/unit node/plugins/localSearchPlugin.test.ts goes from 2 failed on pristine upstream/main to 6 passed here, and the full unit slice is 505 passed / 0 failed. I'll re-run whatever is needed once the workflows are approved.

…link-stale

# Conflicts:
#	__tests__/unit/node/plugins/localSearchPlugin.test.ts
@rubenmarcus

Copy link
Copy Markdown
Author

Merged main: #5509 changed the same test file, and the conflict came down to its index-loading helper. I dropped my local MiniSearch.loadJSON copy and read the index through loadLocalSearchIndex like the new tests do, so the options (tokenizer included) come from one place.

Re-ran on the merged head a510ceab:

  • node/plugins/localSearchPlugin.test.ts: 8 passed (8), transplanting the three tests onto pristine main @ 0ad6d72d still fails the unlink and rename cases
  • __tests__/unit: 32 files, 518 passed, 2 skipped, 0 failed
  • tsc -p tsconfig.node.json --noEmit and tsc -p __tests__/unit --noEmit: clean

The Test and CR workflows are back to needing the fork-run approval after the push, so CI has nothing on this head yet.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Local search keeps results for pages deleted or renamed during vitepress dev

1 participant