perf: prefetch ai pages and soften route loading

This commit is contained in:
2026-06-14 08:27:51 +08:00
parent 829d85625f
commit 58ce877e64
3 changed files with 93 additions and 12 deletions
+2
View File
@@ -13,6 +13,7 @@ import { normalizeHomePath } from '#/utils/legacy-home';
import { generateAccess } from './access';
import { isLightTabRoute } from './light-menu';
import { prefetchAiPlatformPages } from './prefetch';
function filterLightTabHistory<T extends { tabs?: unknown[] }>(history: T) {
const tabs = Array.isArray(history.tabs) ? history.tabs : [];
@@ -197,6 +198,7 @@ function setupAccessGuard(router: Router) {
accessStore.setAccessMenus(accessibleMenus);
accessStore.setAccessRoutes(accessibleRoutes);
accessStore.setIsAccessChecked(true);
prefetchAiPlatformPages();
// 子应用根路径重定向(/app/hr -> /app/hr/xxx
if (appContextStore.isSubApp) {
+47
View File
@@ -0,0 +1,47 @@
type IdleCallbackHandle = number;
type IdleCallbackOptions = {
timeout?: number;
};
type WindowWithIdleCallback = Window & {
requestIdleCallback?: (
callback: () => void,
options?: IdleCallbackOptions,
) => IdleCallbackHandle;
};
let prefetched = false;
const prefetchTasks = [
() => import('#/views/ai-platform/agent/index.vue'),
() => import('#/views/_core/agent-chat/index.vue'),
() => import('#/views/ai-platform/workflow/index.vue'),
() => import('#/views/ai-platform/workflow-runs/index.vue'),
() => import('#/views/ai-platform/model/index.vue'),
() => import('#/views/ai-platform/knowledge/index.vue'),
];
function scheduleIdle(callback: () => void) {
const browserWindow = window as WindowWithIdleCallback;
if (browserWindow.requestIdleCallback) {
browserWindow.requestIdleCallback(callback, { timeout: 3000 });
return;
}
window.setTimeout(callback, 1200);
}
function prefetchAiPlatformPages() {
if (prefetched || typeof window === 'undefined') {
return;
}
prefetched = true;
scheduleIdle(() => {
void Promise.allSettled(prefetchTasks.map((task) => task()));
});
}
export { prefetchAiPlatformPages };