Repository navigation
fix(search): drop pages removed from disk in dev - #5483
rubenmarcus wants to merge 2 commits into
Conversation
|
GitHub holds both workflows on this PR ( |
…link-stale # Conflicts: # __tests__/unit/node/plugins/localSearchPlugin.test.ts
|
Merged main: #5509 changed the same test file, and the conflict came down to its index-loading helper. I dropped my local Re-ran on the merged head
The Test and CR workflows are back to needing the fork-run approval after the push, so CI has nothing on this head yet. |
Fixes #5481.
In
vitepress dev,localSearchPluginonly ever writes into MiniSearch.hotUpdateguards onsiteConfig.pages.includes(page), so a.mdfile that has just been deleted never reachesindexFile, and the plugin registers no watcherunlinkhandler at all. MiniSearch holds a document untildiscardis called, so every section of a removed page stays searchable and keeps linking to a route that now 404s. Aftergit mvboth 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:indexFileadds every id it stores, andconfigureServerhandles the watcher event:The locale is resolved through
rewrites.mapexactly likeindexFiledoes, so a page indexed under a rewritten path lands in the right index, andonIndexUpdated()invalidates the virtual@localSearchIndexmodule and sends the HMR update the same way the edit path does.Verification, three new tests in
__tests__/unit/node/plugins/localSearchPlugin.test.ts:The three tests drive the plugin the way vite does: a stub dev server (
watcheremitter plus amoduleGraphmock),configResolved,configureServer, thenloadto read the serialized per-locale index back throughloadLocalSearchIndex(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-guideonly and thatindex.has('/guide/old.html#renamed-guide')is false; a third test asserts anunlinkfor 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.pagesis resolved once andhotUpdategates 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.