Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: narrow Vite SPA watcher scope (#17693)

YuTengjingd81495521a0c
diff --git a/plugins/vite/envRestartKeys.ts b/plugins/vite/envRestartKeys.ts
index 43b40a28f5b..d287d452761 100644
--- a/plugins/vite/envRestartKeys.ts
+++ b/plugins/vite/envRestartKeys.ts
@@ -74,16 +74,6 @@ export function viteEnvRestartKeys(keys: string[]): Plugin {
name: 'vite-env-restart-keys',
apply: 'serve',
- config() {
- return {
- server: {
- watch: {
- ignored: ['**/.env', '**/.env.*', '**/*.test.ts', '**/*.test.tsx'],
- },
- },
- };
- },
-
configResolved(config) {
mode = config.mode;
envDir = config.envDir || config.root;
diff --git a/plugins/vite/watchOptions.test.ts b/plugins/vite/watchOptions.test.ts
new file mode 100644
index 00000000000..72161a4c73d
--- /dev/null
+++ b/plugins/vite/watchOptions.test.ts
@@ -0,0 +1,56 @@
+import { describe, expect, it } from 'vitest';
+
+import { createViteWatchOptions } from './watchOptions';
+
+describe('viteWatchOptions', () => {
+ const { ignored: isIgnored } = createViteWatchOptions([
+ '/repo',
+ '/repo/lobehub',
+ String.raw`C:\repo`,
+ String.raw`C:\repo\lobehub`,
+ ]);
+
+ it.each([
+ '/repo/.agents/skills/example/SKILL.md',
+ '/repo/lobehub/.cursor/rules/example.mdc',
+ '/repo/.next/server/app.js',
+ '/repo/lobehub/.turbo/cache/entry',
+ '/repo/doc/guide.md',
+ '/repo/docs/guide.md',
+ '/repo/lobehub/src/features/Chat/__tests__/index.test.ts',
+ '/repo/devtools/inspector.ts',
+ '/repo/lobehub/scripts/build.mts',
+ '/repo/lobehub/apps/cli/src/index.ts',
+ '/repo/lobehub/apps/desktop/src/main.ts',
+ '/repo/src/app/layout.tsx',
+ '/repo/lobehub/src/server/service.ts',
+ '/repo/packages/database/src/index.ts',
+ String.raw`C:\repo\lobehub\packages\database\src\index.ts`,
+ '/repo/.env.local',
+ '/repo/src/features/Chat/index.spec.tsx',
+ ])('ignores paths outside the SPA runtime: %s', (filePath) => {
+ expect(isIgnored(filePath)).toBe(true);
+ });
+
+ it.each([
+ '/repo/src/features/Chat/index.tsx',
+ '/repo/lobehub/src/routes/(main)/chat/index.tsx',
+ '/repo/packages/types/src/index.ts',
+ '/repo/lobehub/packages/locales/src/default/common.ts',
+ '/repo/locales/en-US/common.json',
+ '/repo/public/logo.svg',
+ '/repo/lobehub/public/logo.svg',
+ '/repo/apps',
+ '/repo/apps/server/src/utils/workspacePermissions.ts',
+ '/repo/apps/web/src/index.ts',
+ '/repo/lobehub/src/routes/(main)/agent/docs/index.tsx',
+ '/repo/lobehub/packages/model-runtime/docs/test-coverage.md',
+ '/repo/packages/database-client/src/index.ts',
+ '/repo/packages/example/src/app/index.ts',
+ '/repo/lobehub/packages/example/src/server/index.ts',
+ '/repo/src/application/index.ts',
+ '/other/repo/docs/guide.md',
+ ])('keeps SPA runtime paths watched: %s', (filePath) => {
+ expect(isIgnored(filePath)).toBe(false);
+ });
+});
diff --git a/plugins/vite/watchOptions.ts b/plugins/vite/watchOptions.ts
new file mode 100644
index 00000000000..15897ca3d34
--- /dev/null
+++ b/plugins/vite/watchOptions.ts
@@ -0,0 +1,88 @@
+import type { WatchOptions } from 'vite';
+
+const IGNORED_DIRECTORY_NAMES = new Set([
+ '.agent-tracing',
+ '.agents',
+ '.changeset',
+ '.claude',
+ '.codex',
+ '.cursor',
+ '.devcontainer',
+ '.github',
+ '.husky',
+ '.local-config',
+ '.next',
+ '.nyc_output',
+ '.records',
+ '.turbo',
+ '.vscode',
+ '__snapshots__',
+ '__tests__',
+ 'changelog',
+ 'coverage',
+ 'devtools',
+ 'e2e',
+ 'fixtures',
+ 'patches',
+ 'scripts',
+ 'test',
+ 'tests',
+]);
+
+/** Root-only repository areas; matching these names at arbitrary depth can hide browser routes. */
+const IGNORED_ROOT_DIRECTORY_PATHS = [
+ ['apps', 'cli'],
+ ['apps', 'desktop'],
+ ['doc'],
+ ['docs'],
+ ['packages', 'database'],
+ ['src', 'app'],
+ ['src', 'server'],
+];
+
+const TEST_FILE_PATTERN = /\.(?:spec|test)\.[cm]?[jt]sx?$/;
+
+const normalizeWatchedPath = (filePath: string) =>
+ filePath.replaceAll('\\', '/').replace(/\/+$/, '');
+
+const relativeToRoot = (filePath: string, root: string) => {
+ if (filePath === root) return '';
+
+ const rootPrefix = `${root}/`;
+ if (filePath.startsWith(rootPrefix)) return filePath.slice(rootPrefix.length);
+};
+
+const isViteWatchRelativePathIgnored = (relativePath: string) => {
+ const segments = relativePath.split('/').filter(Boolean);
+ const fileName = segments.at(-1) ?? '';
+
+ if (fileName === '.env' || fileName.startsWith('.env.') || TEST_FILE_PATTERN.test(fileName)) {
+ return true;
+ }
+
+ if (segments.some((segment) => IGNORED_DIRECTORY_NAMES.has(segment))) return true;
+
+ return IGNORED_ROOT_DIRECTORY_PATHS.some((directoryPath) =>
+ directoryPath.every((segment, index) => segments[index] === segment),
+ );
+};
+
+/**
+ * Keep the SPA watcher focused on browser runtime sources. Vite otherwise watches the entire
+ * monorepo recursively, including independent applications and repository-only tooling.
+ */
+export const createViteWatchOptions = (sourceRoots: string[]) => {
+ const normalizedRoots = sourceRoots.map(normalizeWatchedPath);
+
+ return {
+ ignored: (filePath: string) => {
+ const normalizedPath = normalizeWatchedPath(filePath);
+
+ return normalizedRoots.some((root) => {
+ const relativePath = relativeToRoot(normalizedPath, root);
+
+ return relativePath === undefined ? false : isViteWatchRelativePathIgnored(relativePath);
+ });
+ },
+ } satisfies WatchOptions;
+};
diff --git a/vite.config.ts b/vite.config.ts
index 8d4e67f4882..01a9fd92e15 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -18,6 +18,7 @@ import {
sharedRendererPlugins,
} from './plugins/vite/sharedRendererConfig';
import { vercelSkewProtection } from './plugins/vite/vercelSkewProtection';
+import { createViteWatchOptions } from './plugins/vite/watchOptions';
const isMobile = process.env.MOBILE === 'true';
const isAuth = process.env.AUTH === 'true';
@@ -368,8 +369,6 @@ export default defineConfig({
'./packages/agent-manager-runtime/src/**/*.ts',
],
},
- watch: {
- ignored: ['**/e2e/reports/**', '**/e2e/screenshots/**'],
- },
+ watch: createViteWatchOptions([__dirname]),
},
});