perf(frontend): eliminate navigation loading states

This commit is contained in:
Tunglies
2026-07-20 02:43:38 +08:00
parent 971dd37965
commit e63b8e6eb0
6 changed files with 54 additions and 216 deletions
+5 -70
View File
@@ -24,22 +24,14 @@ import {
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import type { CSSProperties } from 'react'
import {
lazy,
Suspense,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Outlet, useLocation, useNavigate } from 'react-router'
import { Outlet, useNavigate } from 'react-router'
import iconDark from '@/assets/image/icon_dark.svg?react'
import iconLight from '@/assets/image/icon_light.svg?react'
import LogoSvg from '@/assets/image/logo.svg?react'
import { BaseErrorBoundary, BaseLoading } from '@/components/base'
import { BaseErrorBoundary } from '@/components/base'
import { LayoutItem } from '@/components/layout/layout-item'
import { LayoutTraffic } from '@/components/layout/layout-traffic'
import { NoticeManager } from '@/components/layout/notice-manager'
@@ -50,7 +42,6 @@ import {
} from '@/components/layout/window-controller'
import { useI18n } from '@/hooks/use-i18n'
import { useVerge } from '@/hooks/use-verge'
import { useVisibility } from '@/hooks/use-visibility'
import { useWindowDecorations } from '@/hooks/use-window'
import { useThemeMode } from '@/services/states'
import getSystem from '@/utils/get-system'
@@ -62,17 +53,11 @@ import {
useNavMenuOrder,
} from './_layout/hooks'
import { handleNoticeMessage } from './_layout/utils'
import {
navItems,
preloadLogsPage,
preloadNavigationRoutes,
} from './_navigation'
import { navItems } from './_navigation'
import 'dayjs/locale/ru'
import 'dayjs/locale/zh-cn'
const LogsPage = lazy(() => preloadLogsPage())
type NavItem = (typeof navItems)[number]
type MenuContextPosition = { top: number; left: number }
@@ -107,7 +92,6 @@ const SortableNavMenuItem = ({ item, label }: SortableNavMenuItemProps) => {
<LayoutItem
to={item.path}
icon={item.icon}
onPreload={item.preload}
sortable={{
setNodeRef,
attributes,
@@ -135,9 +119,6 @@ const Layout = () => {
const navCollapsed = verge?.collapse_navbar ?? false
const { switchLanguage } = useI18n()
const navigate = useNavigate()
const { pathname } = useLocation()
const isLogsPage = pathname === '/logs'
const pageVisible = useVisibility()
const themeReady = useMemo(() => Boolean(theme), [theme])
const [menuUnlocked, setMenuUnlocked] = useState(false)
@@ -236,19 +217,6 @@ const Layout = () => {
useLoadingOverlay(themeReady)
useEffect(() => {
if (!themeReady || !pageVisible) {
return
}
const controller = new AbortController()
void preloadNavigationRoutes(controller.signal)
return () => {
controller.abort()
}
}, [themeReady, pageVisible])
const handleNotice = useCallback(
(payload: [string, string]) => {
const [status, msg] = payload
@@ -425,12 +393,7 @@ const Layout = () => {
return null
}
return (
<LayoutItem
key={item.path}
to={item.path}
icon={item.icon}
onPreload={item.preload}
>
<LayoutItem key={item.path} to={item.path} icon={item.icon}>
{t(item.label)}
</LayoutItem>
)
@@ -490,34 +453,6 @@ const Layout = () => {
<BaseErrorBoundary>
<Outlet />
</BaseErrorBoundary>
{isLogsPage && (
<div
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
>
<Suspense
fallback={
<Box
sx={{
display: 'flex',
height: '100%',
alignItems: 'center',
justifyContent: 'center',
}}
>
<BaseLoading />
</Box>
}
>
<LogsPage />
</Suspense>
</div>
)}
</div>
</div>
</div>
+15 -122
View File
@@ -6,8 +6,7 @@ import LockOpenOutlinedIcon from '@mui/icons-material/LockOpenOutlined'
import SettingsOutlinedIcon from '@mui/icons-material/SettingsOutlined'
import SubjectOutlinedIcon from '@mui/icons-material/SubjectOutlined'
import WifiOutlinedIcon from '@mui/icons-material/WifiOutlined'
import { Box } from '@mui/material'
import { lazy, Suspense, type ComponentType, type ReactNode } from 'react'
import { type ComponentType, type ReactNode } from 'react'
import ConnectionsSvg from '@/assets/image/itemicon/connections.svg?react'
import HomeSvg from '@/assets/image/itemicon/home.svg?react'
@@ -17,115 +16,24 @@ import ProxiesSvg from '@/assets/image/itemicon/proxies.svg?react'
import RulesSvg from '@/assets/image/itemicon/rules.svg?react'
import SettingsSvg from '@/assets/image/itemicon/settings.svg?react'
import UnlockSvg from '@/assets/image/itemicon/unlock.svg?react'
import { BaseLoading } from '@/components/base'
import { ensureLanguageSections } from '@/services/i18n'
import { navigationItems } from './_navigation-meta'
import ConnectionsPage from './connections'
import HomePage from './home'
import LogsPage from './logs'
import ProfilePage from './profiles'
import ProxyPage from './proxies'
import RulesPage from './rules'
import SettingPage from './settings'
import UnlockPage from './unlock'
type NavigationItem = {
label: (typeof navigationItems)[keyof typeof navigationItems]['label']
path: string
icon: ReactNode[]
Component: ComponentType
preload?: () => Promise<{ default: ComponentType }>
}
const waitForWarmupIdle = (signal: AbortSignal) =>
new Promise<void>((resolve) => {
let idleId: number | undefined
let timeoutId: number | undefined
const cleanup = () => {
signal.removeEventListener('abort', finish)
if (idleId !== undefined) {
window.cancelIdleCallback(idleId)
}
if (timeoutId !== undefined) {
window.clearTimeout(timeoutId)
}
}
const finish = () => {
cleanup()
resolve()
}
if (signal.aborted) {
resolve()
return
}
signal.addEventListener('abort', finish, { once: true })
if (window.requestIdleCallback) {
idleId = window.requestIdleCallback(finish, { timeout: 500 })
} else {
timeoutId = window.setTimeout(finish, 120)
}
})
const createRoutePreload = (
load: () => Promise<{ default: ComponentType }>,
sections?: string | readonly string[],
) => {
let componentPromise: Promise<{ default: ComponentType }> | undefined
const loadComponent = () => {
componentPromise ??= load().catch((error) => {
componentPromise = undefined
throw error
})
return componentPromise
}
if (!sections) {
return loadComponent
}
return async () => {
const [component] = await Promise.all([
loadComponent(),
ensureLanguageSections(sections),
])
return component
}
}
const createLazyRoute = (
load: () => Promise<{ default: ComponentType }>,
sections?: string | readonly string[],
) => {
const preload = createRoutePreload(load, sections)
const Component = lazy(preload)
const LazyRoute = () => (
<Suspense
fallback={
<Box
sx={{
display: 'flex',
height: '100%',
alignItems: 'center',
justifyContent: 'center',
}}
>
<BaseLoading />
</Box>
}
>
<Component />
</Suspense>
)
return { Component: LazyRoute, preload }
}
export const preloadLogsPage = createRoutePreload(
() => import('./logs'),
'logs',
)
export const navItems: NavigationItem[] = [
{
...navigationItems.home,
@@ -135,51 +43,36 @@ export const navItems: NavigationItem[] = [
{
...navigationItems.proxies,
icon: [<WifiOutlinedIcon key="mui" />, <ProxiesSvg key="svg" />],
...createLazyRoute(() => import('./proxies')),
Component: ProxyPage,
},
{
...navigationItems.profiles,
icon: [<DnsOutlinedIcon key="mui" />, <ProfilesSvg key="svg" />],
...createLazyRoute(() => import('./profiles'), 'rules'),
Component: ProfilePage,
},
{
...navigationItems.connections,
icon: [<LanguageOutlinedIcon key="mui" />, <ConnectionsSvg key="svg" />],
...createLazyRoute(() => import('./connections'), 'connections'),
Component: ConnectionsPage,
},
{
...navigationItems.rules,
icon: [<ForkRightOutlinedIcon key="mui" />, <RulesSvg key="svg" />],
...createLazyRoute(() => import('./rules'), 'rules'),
Component: RulesPage,
},
{
...navigationItems.logs,
icon: [<SubjectOutlinedIcon key="mui" />, <LogsSvg key="svg" />],
Component: () => null /* LogsPage rendered in Layout only on /logs route */,
preload: preloadLogsPage,
Component: LogsPage,
},
{
...navigationItems.unlock,
icon: [<LockOpenOutlinedIcon key="mui" />, <UnlockSvg key="svg" />],
...createLazyRoute(() => import('./unlock')),
Component: UnlockPage,
},
{
...navigationItems.settings,
icon: [<SettingsOutlinedIcon key="mui" />, <SettingsSvg key="svg" />],
...createLazyRoute(() => import('./settings')),
Component: SettingPage,
},
]
export const preloadNavigationRoutes = async (signal: AbortSignal) => {
await waitForWarmupIdle(signal)
if (signal.aborted) {
return
}
await Promise.all(
navItems.map((item) => {
const preload = 'preload' in item ? item.preload : undefined
return preload?.().catch(() => {})
}),
)
}
+23 -12
View File
@@ -36,26 +36,37 @@ import { useProfiles } from '@/hooks/use-profiles'
import { useVerge } from '@/hooks/use-verge'
import { entry_lightweight_mode, openWebUrl } from '@/services/cmds'
const LazyTestCard = lazy(() =>
const preloadTestCard = () =>
import('@/components/home/test-card').then((module) => ({
default: module.TestCard,
})),
)
const LazyIpInfoCard = lazy(() =>
}))
const preloadIpInfoCard = () =>
import('@/components/home/ip-info-card').then((module) => ({
default: module.IpInfoCard,
})),
)
const LazyClashInfoCard = lazy(() =>
}))
const preloadClashInfoCard = () =>
import('@/components/home/clash-info-card').then((module) => ({
default: module.ClashInfoCard,
})),
)
const LazySystemInfoCard = lazy(() =>
}))
const preloadSystemInfoCard = () =>
import('@/components/home/system-info-card').then((module) => ({
default: module.SystemInfoCard,
})),
)
}))
const LazyTestCard = lazy(preloadTestCard)
const LazyIpInfoCard = lazy(preloadIpInfoCard)
const LazyClashInfoCard = lazy(preloadClashInfoCard)
const LazySystemInfoCard = lazy(preloadSystemInfoCard)
// Used by bootstrap to initiate optional card imports without blocking render.
// eslint-disable-next-line react-refresh/only-export-components
export const preloadHomePageCards = () =>
Promise.all([
preloadTestCard().catch(() => {}),
preloadIpInfoCard().catch(() => {}),
preloadClashInfoCard().catch(() => {}),
preloadSystemInfoCard().catch(() => {}),
])
// 定义首页卡片设置接口
interface HomeCardsSettings {