perf: defer ai page prefetch

This commit is contained in:
2026-06-22 08:26:18 +08:00
parent e6683fcb35
commit 3e9ed9e132
2 changed files with 70 additions and 29 deletions
+63 -29
View File
@@ -16,9 +16,11 @@ type NavigatorWithConnection = Navigator & {
effectiveType?: string;
saveData?: boolean;
};
deviceMemory?: number;
};
const prefetchedGroups = new Set<string>();
const scheduledGroups = new Set<string>();
type PrefetchTask = {
key: string;
@@ -31,39 +33,36 @@ const corePrefetchTasks: PrefetchTask[] = [
{ key: 'system-role', load: () => import('#/views/_core/role/index.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: PrefetchTask[] = [
{
const aiPrefetchTasks = {
agent: {
key: 'ai-agent',
load: () => import('#/views/ai-platform/agent/index.vue'),
},
{
key: 'ai-workflow',
load: () => import('#/views/ai-platform/workflow/index.vue'),
agentChat: {
key: 'agent-chat',
load: () => import('#/views/_core/agent-chat/index.vue'),
},
{
key: 'ai-model',
load: () => import('#/views/ai-platform/model/index.vue'),
aiChatPanel: {
key: 'ai-chat-panel',
load: () => import('#/components/ai-chat-panel/AiChatPanel.vue'),
},
{
knowledge: {
key: 'ai-knowledge',
load: () => import('#/views/ai-platform/knowledge/index.vue'),
},
{
model: {
key: 'ai-model',
load: () => import('#/views/ai-platform/model/index.vue'),
},
workflow: {
key: 'ai-workflow',
load: () => import('#/views/ai-platform/workflow/index.vue'),
},
workflowRuns: {
key: 'ai-workflow-runs',
load: () => import('#/views/ai-platform/workflow-runs/index.vue'),
},
];
} as const;
function scheduleIdle(callback: () => void) {
const browserWindow = window as WindowWithIdleCallback;
@@ -86,6 +85,11 @@ function canPrefetch() {
return false;
}
const deviceMemory = (navigator as NavigatorWithConnection).deviceMemory;
if (deviceMemory && deviceMemory < 4) {
return false;
}
return !/(^|-)2g$/.test(connection?.effectiveType || '');
}
@@ -99,13 +103,18 @@ async function runTasksSequentially(tasks: PrefetchTask[]) {
}
prefetchedGroups.add(task.key);
await task.load().catch(() => undefined);
await new Promise((resolve) => window.setTimeout(resolve, 300));
}
}
function delayedPrefetch(tasks: PrefetchTask[], delay: number) {
function delayedPrefetch(groupKey: string, tasks: PrefetchTask[], delay: number) {
if (!tasks.length || typeof window === 'undefined') {
return;
}
if (scheduledGroups.has(groupKey)) {
return;
}
scheduledGroups.add(groupKey);
window.setTimeout(() => {
scheduleIdle(() => {
@@ -126,29 +135,54 @@ function isAgentChatRoute(path: string) {
return path.startsWith('/agent-chat/');
}
function getAiRoutePrefetchTasks(path: string): PrefetchTask[] {
if (path.startsWith('/ai-platform/agent')) {
return [aiPrefetchTasks.workflow, aiPrefetchTasks.model];
}
if (path.startsWith('/ai-platform/workflow-runs')) {
return [aiPrefetchTasks.workflow];
}
if (path.startsWith('/ai-platform/workflow')) {
return [aiPrefetchTasks.workflowRuns, aiPrefetchTasks.agent];
}
if (path.startsWith('/ai-platform/model')) {
return [aiPrefetchTasks.agent, aiPrefetchTasks.workflow];
}
if (path.startsWith('/ai-platform/knowledge')) {
return [aiPrefetchTasks.agent];
}
return [aiPrefetchTasks.agent, aiPrefetchTasks.workflow];
}
function prefetchAiPlatformPages(currentPath = '') {
if (typeof window === 'undefined') {
return;
}
if (isSystemRoute(currentPath)) {
delayedPrefetch(corePrefetchTasks, 6000);
delayedPrefetch(`core:${currentPath}`, corePrefetchTasks, 8000);
return;
}
if (isAgentChatRoute(currentPath)) {
delayedPrefetch(aiInteractivePrefetchTasks, 1500);
delayedPrefetch(
`agent-chat:${currentPath}`,
[aiPrefetchTasks.agentChat, aiPrefetchTasks.aiChatPanel],
6000,
);
return;
}
if (isAiPlatformRoute(currentPath)) {
delayedPrefetch(aiManagementPrefetchTasks, 2000);
delayedPrefetch(aiInteractivePrefetchTasks, 4500);
delayedPrefetch(
`ai:${currentPath}`,
getAiRoutePrefetchTasks(currentPath),
6000,
);
return;
}
delayedPrefetch(corePrefetchTasks, 7000);
delayedPrefetch(aiInteractivePrefetchTasks, 12_000);
delayedPrefetch('core:home', corePrefetchTasks, 10_000);
}
export { prefetchAiPlatformPages };