我叫洪圣文
This commit is contained in:
@@ -10,6 +10,15 @@ import { createHomeMenuMap, getHomeValidPaths, HOME_EXCLUDED_PATHS } from '@/uti
|
||||
|
||||
const WHITE_LIST_SET = new Set(ROUTER_WHITE_LIST)
|
||||
const mode = import.meta.env.VITE_ROUTER_MODE
|
||||
const ROUTER_PERF_DEBUG = import.meta.env.DEV
|
||||
|
||||
const perfNow = () => (typeof performance !== 'undefined' ? performance.now() : Date.now())
|
||||
const logRouterPerf = (label: string, start: number, extra?: Record<string, unknown>) => {
|
||||
if (!ROUTER_PERF_DEBUG) return
|
||||
const duration = Math.round((perfNow() - start) * 100) / 100
|
||||
console.info(`[router-perf] ${label}`, { duration, ...extra })
|
||||
}
|
||||
let routePerfStart = 0
|
||||
|
||||
const routerMode = {
|
||||
hash: () => createWebHashHistory(),
|
||||
@@ -26,10 +35,17 @@ const router = createRouter({
|
||||
const syncHomeStateWithMenus = () => {
|
||||
const authStore = useAuthStore()
|
||||
const homeStore = useHomeStore()
|
||||
homeStore.syncWithMenus(getHomeValidPaths(authStore.flatMenuListGet))
|
||||
const start = perfNow()
|
||||
const flatMenuList = authStore.flatMenuListGet
|
||||
homeStore.syncWithMenus(getHomeValidPaths(flatMenuList))
|
||||
logRouterPerf('sync-home-state', start, {
|
||||
menuCount: flatMenuList.length
|
||||
})
|
||||
}
|
||||
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const guardStart = perfNow()
|
||||
routePerfStart = guardStart
|
||||
const userStore = useUserStore()
|
||||
const authStore = useAuthStore()
|
||||
|
||||
@@ -53,8 +69,18 @@ router.beforeEach(async (to, from, next) => {
|
||||
|
||||
if (!authStore.authMenuListGet.length) {
|
||||
try {
|
||||
const initStart = perfNow()
|
||||
await initDynamicRouter()
|
||||
logRouterPerf('init-dynamic-router', initStart)
|
||||
|
||||
const syncStart = perfNow()
|
||||
syncHomeStateWithMenus()
|
||||
logRouterPerf('first-sync-home-state', syncStart)
|
||||
|
||||
logRouterPerf('before-each-first-init', guardStart, {
|
||||
path: to.path,
|
||||
from: from.path
|
||||
})
|
||||
return next({ ...to, replace: true })
|
||||
} catch (error) {
|
||||
console.error('Dynamic router initialization failed', error)
|
||||
@@ -64,12 +90,23 @@ router.beforeEach(async (to, from, next) => {
|
||||
}
|
||||
|
||||
syncHomeStateWithMenus()
|
||||
logRouterPerf('before-each-menu-sync', guardStart, {
|
||||
path: to.path,
|
||||
from: from.path,
|
||||
hasActivateInfo: authStore.activateInfoLoadedGet
|
||||
})
|
||||
await authStore.setRouteName(to.name as string)
|
||||
|
||||
if (!authStore.activateInfoLoadedGet) {
|
||||
const activateStart = perfNow()
|
||||
await authStore.setActivateInfo()
|
||||
logRouterPerf('load-activate-info', activateStart)
|
||||
}
|
||||
|
||||
logRouterPerf('before-each-total', guardStart, {
|
||||
path: to.path,
|
||||
from: from.path
|
||||
})
|
||||
next()
|
||||
})
|
||||
|
||||
@@ -89,8 +126,15 @@ router.onError(error => {
|
||||
})
|
||||
})
|
||||
|
||||
router.afterEach(to => {
|
||||
router.afterEach((to, from) => {
|
||||
NProgress.done()
|
||||
const afterEachStart = perfNow()
|
||||
if (routePerfStart) {
|
||||
logRouterPerf('navigation-total', routePerfStart, {
|
||||
path: to.path,
|
||||
from: from.path
|
||||
})
|
||||
}
|
||||
|
||||
if (HOME_EXCLUDED_PATHS.has(to.path)) return
|
||||
|
||||
@@ -101,6 +145,10 @@ router.afterEach(to => {
|
||||
if (!menuMap[to.path]) return
|
||||
|
||||
homeStore.addRecentVisited(to.path)
|
||||
logRouterPerf('after-each-home-recent', afterEachStart, {
|
||||
path: to.path,
|
||||
menuCount: Object.keys(menuMap).length
|
||||
})
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
Reference in New Issue
Block a user