perf(admin): 实现切页即时导航与列表 loading 壳
非阻塞 session 刷新、侧边栏 chunk 预取、KeepAlive 对齐,以及按 tab 延迟加载重型列表页。
This commit is contained in:
25
apps/admin/src/utils/route-prefetch.ts
Normal file
25
apps/admin/src/utils/route-prefetch.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import type { Router } from 'vue-router';
|
||||
|
||||
const prefetchedPaths = new Set<string>();
|
||||
|
||||
/** 预取目标路径上所有 lazy route 的 JS chunk(侧边栏 hover/focus 时调用)。 */
|
||||
export function prefetchRouteChunks(router: Router, path: string) {
|
||||
const key = path.split('?')[0] || '/';
|
||||
if (prefetchedPaths.has(key)) return;
|
||||
|
||||
let matched = false;
|
||||
try {
|
||||
const resolved = router.resolve(path);
|
||||
for (const record of resolved.matched) {
|
||||
const loader = record.components?.default;
|
||||
if (typeof loader === 'function') {
|
||||
matched = true;
|
||||
void (loader as () => Promise<unknown>)();
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
if (matched) prefetchedPaths.add(key);
|
||||
}
|
||||
Reference in New Issue
Block a user