vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
13.22k stars 2.13k forks source link

Failed to restart using Vite plugin after setting up local search #4251

Closed ghost closed 1 month ago

ghost commented 1 month ago

Describe the bug

I used a Vite plugin (which automatically restarts the server after modifying the MD file), but when the search was set to local search, the plugin failed to restart, perhaps because the search cache was not cleared

Reproduction

import { defineConfig } from "vitepress";
import fs from "fs";

function autoRestart() {
    let restartPromise;
    return {
        name: "auto-restart",
        configureServer(server) {
            server.watcher.on("all", (event, path) => {
                if (path.endsWith(".md")) {
                    if (!restartPromise) {
                        restartPromise = (async () => {
                            console.log("Restarting server...");
                            // TODO: clearCache()?
                            // [vitepress] MiniSearch: duplicate ID
                            // [vitepress] server restart failed
                            await server.restart();
                            // await fs.promises.utimes(server.config.vitepress.configPath, new Date(), new Date());
                        })().finally(() => {
                            restartPromise = undefined;
                        });
                    }
                }
            });
        },
    }
}
export default defineConfig({
    vite: {
        plugins: [
            autoRestart(),
        ],
    },
    themeConfig: {
        search: {
            provider: "local",
        }
    },
});

When modifying the MD file did not restart successfully, but after turning off local search, the code can restart successfully

Restarting server...
[vitepress] MiniSearch: duplicate ID
[vitepress] server restart failed

Expected behavior

Perhaps a method is needed to clearCache() to restart, or the contents of the sidebar can be modified at any time during runtime to take effect in real-time

like this: https://github.com/vuejs/vitepress/blob/2ac64b8d4180f2a7c54fda57df7f3a0a52488d62/src/node/shortcuts.ts#L76-L88 https://github.com/vuejs/vitepress/blob/2ac64b8d4180f2a7c54fda57df7f3a0a52488d62/src/node/plugin.ts#L371-L394

System Info

System:
    OS: macOS 15.0
    CPU: (8) arm64 Apple M1
    Memory: 130.73 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.9.0 - /opt/homebrew/bin/node
    npm: 10.8.3 - /opt/homebrew/bin/npm
    pnpm: 9.12.0 - /opt/homebrew/bin/pnpm
  Browsers:
    Edge: 129.0.2792.79
    Safari: 18.0

Additional context

No response

Validations