perf: lighten routes and improve ai runtime events

This commit is contained in:
2026-06-21 21:01:38 +08:00
parent 0c52bf0641
commit 9647a227da
13 changed files with 130 additions and 74 deletions
+56 -11
View File
@@ -11,20 +11,32 @@ type WindowWithIdleCallback = Window & {
) => IdleCallbackHandle;
};
type NavigatorWithConnection = Navigator & {
connection?: {
effectiveType?: string;
saveData?: boolean;
};
};
let prefetched = false;
const criticalPrefetchTasks = [
() => import('#/views/_core/agent-chat/index.vue'),
() => import('#/views/ai-platform/workflow-runs/index.vue'),
() => import('#/components/ai-chat-panel/AiChatPanel.vue'),
() => import('#/views/ai-platform/workflow-runs/modules/detail-dialog.vue'),
const corePrefetchTasks = [
() => import('#/views/_core/user/index.vue'),
() => import('#/views/_core/menu/index.vue'),
() => import('#/views/_core/role/index.vue'),
];
const deferredPrefetchTasks = [
const aiInteractivePrefetchTasks = [
() => import('#/views/_core/agent-chat/index.vue'),
() => 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'),
];
function scheduleIdle(callback: () => void) {
@@ -38,6 +50,28 @@ function scheduleIdle(callback: () => void) {
window.setTimeout(callback, 1200);
}
function canPrefetch() {
if (typeof document !== 'undefined' && document.visibilityState !== 'visible') {
return false;
}
const connection = (navigator as NavigatorWithConnection).connection;
if (connection?.saveData) {
return false;
}
return !/(^|-)2g$/.test(connection?.effectiveType || '');
}
async function runTasksSequentially(tasks: Array<() => Promise<unknown>>) {
for (const task of tasks) {
if (!canPrefetch()) {
return;
}
await task().catch(() => undefined);
}
}
function prefetchAiPlatformPages() {
if (prefetched || typeof window === 'undefined') {
return;
@@ -45,11 +79,22 @@ function prefetchAiPlatformPages() {
prefetched = true;
window.setTimeout(() => {
void Promise.allSettled(criticalPrefetchTasks.map((task) => task()));
}, 300);
scheduleIdle(() => {
void Promise.allSettled(deferredPrefetchTasks.map((task) => task()));
});
scheduleIdle(() => {
void runTasksSequentially(corePrefetchTasks);
});
}, 800);
window.setTimeout(() => {
scheduleIdle(() => {
void runTasksSequentially(aiInteractivePrefetchTasks);
});
}, 4000);
window.setTimeout(() => {
scheduleIdle(() => {
void runTasksSequentially(aiManagementPrefetchTasks);
});
}, 9000);
}
export { prefetchAiPlatformPages };