perf(frontend): lazy-load modals + split heavy vendor chunks (#4501)
* perf(frontend): lazy-load modals on inbounds / clients / index pages Modals on the three list pages were imported statically, so the JS + CSS for every form, info, qr, log, backup, metrics, system-history, version, and config-text modal sat in the initial bundle even though they're only needed after a click. Converted those imports to React.lazy() and gated each modal with a new LazyMount helper that mounts on first open and keeps the component mounted thereafter so AntD close animations still play. Build now emits a dedicated chunk per modal — InboundFormModal at 66 kB (13 kB gzipped) and InboundInfoModal at 23 kB (4 kB gzipped) are the largest, totalling roughly 150 kB of code that no longer parses on first paint. Profiler measured the inbounds-page React render tree drop from ~444 ms to ~254 ms on a prod build. * perf(frontend): split codemirror / jalali / otpauth into lazy vendor chunks Heavy libs (codemirror, persian-calendar-suite, otpauth) and antd's rc-/cssinjs transitive deps used to fall into the catch-all `vendor` chunk and load with every entry point. Give them their own manualChunks groups so they only load with the lazy modal/page that needs them. Initial vendor (catch-all) drops from 1293 kB / 408 kB gzip to 76 kB / 27 kB gzip; codemirror (408 kB / 131 kB gzip) is now on the JsonEditor lazy path instead of the inbounds/clients/index initial load.
This commit is contained in:
1 parent
c5b71041d3
commit
09df07ddf5
5 files changed
+251
-176
No files matched your search
@@ -0,0 +1,20 @@
|
||||
import { Suspense, useEffect, useState, type ReactNode } from 'react';
|
||||
|
||||
interface LazyMountProps {
|
||||
when: boolean;
|
||||
fallback?: ReactNode;
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
// Mounts children only after `when` first becomes true and keeps them mounted
|
||||
// thereafter, so React.lazy modals get loaded on demand but their close
|
||||
// animations still play out. Pair with `lazy(() => import(...))` modal imports
|
||||
// on heavy list pages to keep the initial bundle small.
|
||||
export default function LazyMount({ when, fallback = null, children }: LazyMountProps) {
|
||||
const [mounted, setMounted] = useState(when);
|
||||
useEffect(() => {
|
||||
if (when && !mounted) setMounted(true);
|
||||
}, [when, mounted]);
|
||||
if (!mounted) return null;
|
||||
return <Suspense fallback={fallback}>{children}</Suspense>;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { lazy, useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import {
|
||||
Badge,
|
||||
@@ -51,11 +51,12 @@ import AppSidebar from '@/components/AppSidebar';
|
||||
import CustomStatistic from '@/components/CustomStatistic';
|
||||
import { IntlUtil, SizeFormatter } from '@/utils';
|
||||
import { setMessageInstance } from '@/utils/messageBus';
|
||||
import ClientFormModal from './ClientFormModal';
|
||||
import ClientInfoModal from './ClientInfoModal';
|
||||
import ClientQrModal from './ClientQrModal';
|
||||
import ClientBulkAddModal from './ClientBulkAddModal';
|
||||
import ClientBulkAdjustModal from './ClientBulkAdjustModal';
|
||||
import LazyMount from '@/components/LazyMount';
|
||||
const ClientFormModal = lazy(() => import('./ClientFormModal'));
|
||||
const ClientInfoModal = lazy(() => import('./ClientInfoModal'));
|
||||
const ClientQrModal = lazy(() => import('./ClientQrModal'));
|
||||
const ClientBulkAddModal = lazy(() => import('./ClientBulkAddModal'));
|
||||
const ClientBulkAdjustModal = lazy(() => import('./ClientBulkAdjustModal'));
|
||||
import '@/styles/page-cards.css';
|
||||
import './ClientsPage.css';
|
||||
|
||||
@@ -853,51 +854,61 @@ export default function ClientsPage() {
|
||||
</Layout.Content>
|
||||
</Layout>
|
||||
|
||||
<ClientFormModal
|
||||
open={formOpen}
|
||||
mode={formMode}
|
||||
client={editingClient}
|
||||
attachedIds={editingAttachedIds}
|
||||
inbounds={inbounds}
|
||||
ipLimitEnable={ipLimitEnable}
|
||||
tgBotEnable={tgBotEnable}
|
||||
save={onSave}
|
||||
onOpenChange={setFormOpen}
|
||||
/>
|
||||
<ClientInfoModal
|
||||
open={infoOpen}
|
||||
client={infoClient}
|
||||
inboundsById={inboundsById}
|
||||
isOnline={infoClient ? isOnline(infoClient.email) : false}
|
||||
subSettings={subSettings}
|
||||
onOpenChange={setInfoOpen}
|
||||
/>
|
||||
<ClientQrModal
|
||||
open={qrOpen}
|
||||
client={qrClient}
|
||||
subSettings={subSettings}
|
||||
onOpenChange={setQrOpen}
|
||||
/>
|
||||
<ClientBulkAddModal
|
||||
open={bulkAddOpen}
|
||||
inbounds={inbounds}
|
||||
ipLimitEnable={ipLimitEnable}
|
||||
onOpenChange={setBulkAddOpen}
|
||||
onSaved={() => setBulkAddOpen(false)}
|
||||
/>
|
||||
<ClientBulkAdjustModal
|
||||
open={bulkAdjustOpen}
|
||||
count={selectedRowKeys.length}
|
||||
onOpenChange={setBulkAdjustOpen}
|
||||
onSubmit={async (addDays, addBytes) => {
|
||||
const msg = await bulkAdjust([...selectedRowKeys], addDays, addBytes);
|
||||
if (msg?.success) {
|
||||
setSelectedRowKeys([]);
|
||||
return msg.obj ?? { adjusted: 0 };
|
||||
}
|
||||
return null;
|
||||
}}
|
||||
/>
|
||||
<LazyMount when={formOpen}>
|
||||
<ClientFormModal
|
||||
open={formOpen}
|
||||
mode={formMode}
|
||||
client={editingClient}
|
||||
attachedIds={editingAttachedIds}
|
||||
inbounds={inbounds}
|
||||
ipLimitEnable={ipLimitEnable}
|
||||
tgBotEnable={tgBotEnable}
|
||||
save={onSave}
|
||||
onOpenChange={setFormOpen}
|
||||
/>
|
||||
</LazyMount>
|
||||
<LazyMount when={infoOpen}>
|
||||
<ClientInfoModal
|
||||
open={infoOpen}
|
||||
client={infoClient}
|
||||
inboundsById={inboundsById}
|
||||
isOnline={infoClient ? isOnline(infoClient.email) : false}
|
||||
subSettings={subSettings}
|
||||
onOpenChange={setInfoOpen}
|
||||
/>
|
||||
</LazyMount>
|
||||
<LazyMount when={qrOpen}>
|
||||
<ClientQrModal
|
||||
open={qrOpen}
|
||||
client={qrClient}
|
||||
subSettings={subSettings}
|
||||
onOpenChange={setQrOpen}
|
||||
/>
|
||||
</LazyMount>
|
||||
<LazyMount when={bulkAddOpen}>
|
||||
<ClientBulkAddModal
|
||||
open={bulkAddOpen}
|
||||
inbounds={inbounds}
|
||||
ipLimitEnable={ipLimitEnable}
|
||||
onOpenChange={setBulkAddOpen}
|
||||
onSaved={() => setBulkAddOpen(false)}
|
||||
/>
|
||||
</LazyMount>
|
||||
<LazyMount when={bulkAdjustOpen}>
|
||||
<ClientBulkAdjustModal
|
||||
open={bulkAdjustOpen}
|
||||
count={selectedRowKeys.length}
|
||||
onOpenChange={setBulkAdjustOpen}
|
||||
onSubmit={async (addDays, addBytes) => {
|
||||
const msg = await bulkAdjust([...selectedRowKeys], addDays, addBytes);
|
||||
if (msg?.success) {
|
||||
setSelectedRowKeys([]);
|
||||
return msg.obj ?? { adjusted: 0 };
|
||||
}
|
||||
return null;
|
||||
}}
|
||||
/>
|
||||
</LazyMount>
|
||||
</Layout>
|
||||
</ConfigProvider>
|
||||
);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { lazy, useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import {
|
||||
Card,
|
||||
@@ -28,14 +28,15 @@ import { useWebSocket } from '@/hooks/useWebSocket';
|
||||
import { useNodes } from '@/hooks/useNodes';
|
||||
import AppSidebar from '@/components/AppSidebar';
|
||||
import CustomStatistic from '@/components/CustomStatistic';
|
||||
import TextModal from '@/components/TextModal';
|
||||
import PromptModal from '@/components/PromptModal';
|
||||
const TextModal = lazy(() => import('@/components/TextModal'));
|
||||
const PromptModal = lazy(() => import('@/components/PromptModal'));
|
||||
|
||||
import { useInbounds } from './useInbounds';
|
||||
import InboundList from './InboundList';
|
||||
import InboundFormModal from './InboundFormModal';
|
||||
import InboundInfoModal from './InboundInfoModal';
|
||||
import QrCodeModal from './QrCodeModal';
|
||||
import LazyMount from '@/components/LazyMount';
|
||||
const InboundFormModal = lazy(() => import('./InboundFormModal'));
|
||||
const InboundInfoModal = lazy(() => import('./InboundInfoModal'));
|
||||
const QrCodeModal = lazy(() => import('./QrCodeModal'));
|
||||
import '@/styles/page-cards.css';
|
||||
import './InboundsPage.css';
|
||||
|
||||
@@ -517,56 +518,66 @@ export default function InboundsPage() {
|
||||
</Layout.Content>
|
||||
</Layout>
|
||||
|
||||
<InboundFormModal
|
||||
open={formOpen}
|
||||
onClose={() => setFormOpen(false)}
|
||||
onSaved={refresh}
|
||||
mode={formMode}
|
||||
dbInbound={formDbInbound}
|
||||
dbInbounds={dbInbounds as any[]}
|
||||
availableNodes={nodesList}
|
||||
/>
|
||||
<InboundInfoModal
|
||||
open={infoOpen}
|
||||
onClose={() => setInfoOpen(false)}
|
||||
dbInbound={infoDbInbound}
|
||||
clientIndex={infoClientIndex}
|
||||
remarkModel={remarkModel}
|
||||
expireDiff={expireDiff}
|
||||
trafficDiff={trafficDiff}
|
||||
ipLimitEnable={ipLimitEnable}
|
||||
tgBotEnable={tgBotEnable}
|
||||
subSettings={subSettings}
|
||||
lastOnlineMap={lastOnlineMap}
|
||||
nodeAddress={infoNodeAddress}
|
||||
/>
|
||||
<QrCodeModal
|
||||
open={qrOpen}
|
||||
onClose={() => setQrOpen(false)}
|
||||
dbInbound={qrDbInbound}
|
||||
client={null}
|
||||
remarkModel={remarkModel}
|
||||
nodeAddress={qrNodeAddress}
|
||||
subSettings={subSettings}
|
||||
/>
|
||||
<LazyMount when={formOpen}>
|
||||
<InboundFormModal
|
||||
open={formOpen}
|
||||
onClose={() => setFormOpen(false)}
|
||||
onSaved={refresh}
|
||||
mode={formMode}
|
||||
dbInbound={formDbInbound}
|
||||
dbInbounds={dbInbounds as any[]}
|
||||
availableNodes={nodesList}
|
||||
/>
|
||||
</LazyMount>
|
||||
<LazyMount when={infoOpen}>
|
||||
<InboundInfoModal
|
||||
open={infoOpen}
|
||||
onClose={() => setInfoOpen(false)}
|
||||
dbInbound={infoDbInbound}
|
||||
clientIndex={infoClientIndex}
|
||||
remarkModel={remarkModel}
|
||||
expireDiff={expireDiff}
|
||||
trafficDiff={trafficDiff}
|
||||
ipLimitEnable={ipLimitEnable}
|
||||
tgBotEnable={tgBotEnable}
|
||||
subSettings={subSettings}
|
||||
lastOnlineMap={lastOnlineMap}
|
||||
nodeAddress={infoNodeAddress}
|
||||
/>
|
||||
</LazyMount>
|
||||
<LazyMount when={qrOpen}>
|
||||
<QrCodeModal
|
||||
open={qrOpen}
|
||||
onClose={() => setQrOpen(false)}
|
||||
dbInbound={qrDbInbound}
|
||||
client={null}
|
||||
remarkModel={remarkModel}
|
||||
nodeAddress={qrNodeAddress}
|
||||
subSettings={subSettings}
|
||||
/>
|
||||
</LazyMount>
|
||||
|
||||
<TextModal
|
||||
open={textOpen}
|
||||
onClose={() => setTextOpen(false)}
|
||||
title={textTitle}
|
||||
content={textContent}
|
||||
fileName={textFileName}
|
||||
/>
|
||||
<PromptModal
|
||||
open={promptOpen}
|
||||
onClose={() => setPromptOpen(false)}
|
||||
title={promptTitle}
|
||||
okText={promptOkText}
|
||||
type={promptType}
|
||||
initialValue={promptInitial}
|
||||
loading={promptLoading}
|
||||
onConfirm={onPromptConfirm}
|
||||
/>
|
||||
<LazyMount when={textOpen}>
|
||||
<TextModal
|
||||
open={textOpen}
|
||||
onClose={() => setTextOpen(false)}
|
||||
title={textTitle}
|
||||
content={textContent}
|
||||
fileName={textFileName}
|
||||
/>
|
||||
</LazyMount>
|
||||
<LazyMount when={promptOpen}>
|
||||
<PromptModal
|
||||
open={promptOpen}
|
||||
onClose={() => setPromptOpen(false)}
|
||||
title={promptTitle}
|
||||
okText={promptOkText}
|
||||
type={promptType}
|
||||
initialValue={promptInitial}
|
||||
loading={promptLoading}
|
||||
onConfirm={onPromptConfirm}
|
||||
/>
|
||||
</LazyMount>
|
||||
</Layout>
|
||||
</ConfigProvider>
|
||||
);
|
||||
|
||||
Loaded 3 of 5 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user