perf: defer route prefetch and enrich workbench

This commit is contained in:
2026-06-21 22:03:33 +08:00
parent 46401e1a4e
commit 81ce2c4f0a
3 changed files with 515 additions and 189 deletions
+84 -31
View File
@@ -18,25 +18,51 @@ type NavigatorWithConnection = Navigator & {
};
};
let prefetched = false;
const prefetchedGroups = new Set<string>();
const corePrefetchTasks = [
() => import('#/views/_core/user/index.vue'),
() => import('#/views/_core/menu/index.vue'),
() => import('#/views/_core/role/index.vue'),
type PrefetchTask = {
key: string;
load: () => Promise<unknown>;
};
const corePrefetchTasks: PrefetchTask[] = [
{ key: 'system-user', load: () => import('#/views/_core/user/index.vue') },
{ key: 'system-menu', load: () => import('#/views/_core/menu/index.vue') },
{ key: 'system-role', load: () => import('#/views/_core/role/index.vue') },
];
const aiInteractivePrefetchTasks = [
() => import('#/views/_core/agent-chat/index.vue'),
() => import('#/components/ai-chat-panel/AiChatPanel.vue'),
const aiInteractivePrefetchTasks: PrefetchTask[] = [
{
key: 'agent-chat',
load: () => import('#/views/_core/agent-chat/index.vue'),
},
{
key: 'ai-chat-panel',
load: () => import('#/components/ai-chat-panel/AiChatPanel.vue'),
},
];
const aiManagementPrefetchTasks = [
() => import('#/views/ai-platform/agent/index.vue'),
() => import('#/views/ai-platform/workflow/index.vue'),
() => import('#/views/ai-platform/model/index.vue'),
() => import('#/views/ai-platform/knowledge/index.vue'),
() => import('#/views/ai-platform/workflow-runs/index.vue'),
const aiManagementPrefetchTasks: PrefetchTask[] = [
{
key: 'ai-agent',
load: () => import('#/views/ai-platform/agent/index.vue'),
},
{
key: 'ai-workflow',
load: () => import('#/views/ai-platform/workflow/index.vue'),
},
{
key: 'ai-model',
load: () => import('#/views/ai-platform/model/index.vue'),
},
{
key: 'ai-knowledge',
load: () => import('#/views/ai-platform/knowledge/index.vue'),
},
{
key: 'ai-workflow-runs',
load: () => import('#/views/ai-platform/workflow-runs/index.vue'),
},
];
function scheduleIdle(callback: () => void) {
@@ -63,38 +89,65 @@ function canPrefetch() {
return !/(^|-)2g$/.test(connection?.effectiveType || '');
}
async function runTasksSequentially(tasks: Array<() => Promise<unknown>>) {
async function runTasksSequentially(tasks: PrefetchTask[]) {
for (const task of tasks) {
if (!canPrefetch()) {
return;
}
await task().catch(() => undefined);
if (prefetchedGroups.has(task.key)) {
continue;
}
prefetchedGroups.add(task.key);
await task.load().catch(() => undefined);
}
}
function prefetchAiPlatformPages() {
if (prefetched || typeof window === 'undefined') {
function delayedPrefetch(tasks: PrefetchTask[], delay: number) {
if (!tasks.length || typeof window === 'undefined') {
return;
}
prefetched = true;
window.setTimeout(() => {
scheduleIdle(() => {
void runTasksSequentially(corePrefetchTasks);
void runTasksSequentially(tasks);
});
}, 800);
}, delay);
}
window.setTimeout(() => {
scheduleIdle(() => {
void runTasksSequentially(aiInteractivePrefetchTasks);
});
}, 4000);
function isSystemRoute(path: string) {
return path.startsWith('/system/') || path.startsWith('/message/');
}
window.setTimeout(() => {
scheduleIdle(() => {
void runTasksSequentially(aiManagementPrefetchTasks);
});
}, 9000);
function isAiPlatformRoute(path: string) {
return path.startsWith('/ai-platform/');
}
function isAgentChatRoute(path: string) {
return path.startsWith('/agent-chat/');
}
function prefetchAiPlatformPages(currentPath = '') {
if (typeof window === 'undefined') {
return;
}
if (isSystemRoute(currentPath)) {
delayedPrefetch(corePrefetchTasks, 6000);
return;
}
if (isAgentChatRoute(currentPath)) {
delayedPrefetch(aiInteractivePrefetchTasks, 1500);
return;
}
if (isAiPlatformRoute(currentPath)) {
delayedPrefetch(aiManagementPrefetchTasks, 2000);
return;
}
delayedPrefetch(corePrefetchTasks, 7000);
delayedPrefetch(aiInteractivePrefetchTasks, 12_000);
}
export { prefetchAiPlatformPages };