From b686d7cb64e0b09644d9e9afb0ce4997dfde0c1d Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Fri, 27 Nov 2026 08:36:23 +0100 Subject: [PATCH] make login page load faster (#13073) * make login page load faster * ann error handler * simplify lazy import * more lazy loading * remove dead function * nove spotlight out to get smaller chunk * load drawer --- .../components/buttons/SpotlightButton.tsx | 2 +- .../src/components/nav/FirstUseTour.tsx | 27 +++-------- .../src/components/nav/FirstUseTourView.tsx | 33 +++++++++++++ src/frontend/src/components/nav/Header.tsx | 5 +- src/frontend/src/components/nav/Layout.tsx | 19 ++++---- .../src/components/nav/SearchDrawer.tsx | 2 +- .../src/components/nav/spotlightStore.ts | 5 ++ .../src/components/plugins/PluginRoutes.tsx | 2 +- .../src/components/plugins/PluginSource.tsx | 3 ++ src/frontend/src/functions/loading.tsx | 5 -- src/frontend/src/functions/pluginGlobals.tsx | 10 ++++ .../src/functions/pluginGlobalsSetup.tsx | 22 +++++++++ src/frontend/src/hooks/UseRemotePlugin.tsx | 2 + src/frontend/src/main.tsx | 46 +++++++++---------- src/frontend/src/router.tsx | 6 ++- 15 files changed, 123 insertions(+), 66 deletions(-) create mode 100644 src/frontend/src/components/nav/FirstUseTourView.tsx create mode 100644 src/frontend/src/components/nav/spotlightStore.ts create mode 100644 src/frontend/src/functions/pluginGlobals.tsx create mode 100644 src/frontend/src/functions/pluginGlobalsSetup.tsx diff --git a/src/frontend/src/components/buttons/SpotlightButton.tsx b/src/frontend/src/components/buttons/SpotlightButton.tsx index 473f1af188..b133491f67 100644 --- a/src/frontend/src/components/buttons/SpotlightButton.tsx +++ b/src/frontend/src/components/buttons/SpotlightButton.tsx @@ -3,7 +3,7 @@ import { ActionIcon, Tooltip } from '@mantine/core'; import { IconCommand } from '@tabler/icons-react'; import { useLocalLibState } from '@lib/states/LocalLibState'; -import { firstSpotlight, searchShortcutKey } from '../nav/Layout'; +import { firstSpotlight, searchShortcutKey } from '../nav/spotlightStore'; /** * A button which opens the quick command modal diff --git a/src/frontend/src/components/nav/FirstUseTour.tsx b/src/frontend/src/components/nav/FirstUseTour.tsx index 3b1a6b97ee..3b8328e329 100644 --- a/src/frontend/src/components/nav/FirstUseTour.tsx +++ b/src/frontend/src/components/nav/FirstUseTour.tsx @@ -1,32 +1,30 @@ import { ApiEndpoints, apiUrl } from '@lib/index'; import { t } from '@lingui/core/macro'; import { Trans } from '@lingui/react/macro'; -import { Button, Group, Text, Tour, rem } from '@mantine/core'; +import { Button, Group, Text, rem } from '@mantine/core'; import { useWindowEvent } from '@mantine/hooks'; import { type NotificationData, notifications } from '@mantine/notifications'; import { type Dispatch, type SetStateAction, + lazy, useEffect, useMemo, useRef, useState } from 'react'; import { api } from '../../App'; +import { Loadable } from '../../functions/loading'; import { useServerApiState } from '../../states/ServerApiState'; import { useGlobalSettingsState, useUserSettingsState } from '../../states/SettingsStates'; import { useUserState } from '../../states/UserState'; +import type { FtuStep } from './FirstUseTourView'; -interface FtuStep { - target: string; - title: string; - description: string; - action_before?: () => void; -} +const FirstUseTourView = Loadable(lazy(() => import('./FirstUseTourView'))); const ftuKex = 'org.inventree.i.tip.ftug'; @@ -153,22 +151,11 @@ export function FirstUseTour() { }, [showNudge]); // rendering - if (!user) { + if (!user || !active) { return null; } return ( - - {ftusteps.map((step, index) => ( - - {step.description} - - ))} - + ); } diff --git a/src/frontend/src/components/nav/FirstUseTourView.tsx b/src/frontend/src/components/nav/FirstUseTourView.tsx new file mode 100644 index 0000000000..3c01fe0fb8 --- /dev/null +++ b/src/frontend/src/components/nav/FirstUseTourView.tsx @@ -0,0 +1,33 @@ +import { Tour } from '@mantine/core'; + +export interface FtuStep { + target: string; + title: string; + description: string; + action_before?: () => void; +} + +export default function FirstUseTourView({ + active, + onClose, + steps +}: Readonly<{ + active: boolean; + onClose: () => void; + steps: FtuStep[]; +}>) { + return ( + + {steps.map((step, index) => ( + + {step.description} + + ))} + + ); +} diff --git a/src/frontend/src/components/nav/Header.tsx b/src/frontend/src/components/nav/Header.tsx index 81e6adb671..e890819467 100644 --- a/src/frontend/src/components/nav/Header.tsx +++ b/src/frontend/src/components/nav/Header.tsx @@ -12,8 +12,10 @@ import { import { useDisclosure, useDocumentVisibility } from '@mantine/hooks'; import { IconBell, IconSearch, IconUserBolt } from '@tabler/icons-react'; import { useQuery } from '@tanstack/react-query'; +import { lazy } from 'react'; import { type ReactNode, useEffect, useMemo, useState } from 'react'; import { Link, matchPath, useLocation, useMatch } from 'react-router-dom'; +import { Loadable } from '../../functions/loading'; import { ApiEndpoints } from '@lib/enums/ApiEndpoints'; import { apiUrl } from '@lib/functions/Api'; @@ -39,7 +41,8 @@ import { MainMenu } from './MainMenu'; import { NavHoverMenu } from './NavHoverMenu'; import { NavigationDrawer } from './NavigationDrawer'; import { NotificationDrawer } from './NotificationDrawer'; -import { SearchDrawer } from './SearchDrawer'; + +const SearchDrawer = Loadable(lazy(() => import('./SearchDrawer'))); export function Header() { const [setNavigationOpen, navigationOpen] = useLocalState( diff --git a/src/frontend/src/components/nav/Layout.tsx b/src/frontend/src/components/nav/Layout.tsx index c24d93dd88..1bc1e64e23 100644 --- a/src/frontend/src/components/nav/Layout.tsx +++ b/src/frontend/src/components/nav/Layout.tsx @@ -1,12 +1,8 @@ import { t } from '@lingui/core/macro'; import { Container, Flex, Space } from '@mantine/core'; -import { - Spotlight, - type SpotlightActionData, - createSpotlight -} from '@mantine/spotlight'; +import { Spotlight, type SpotlightActionData } from '@mantine/spotlight'; import { IconSearch } from '@tabler/icons-react'; -import { type JSX, useEffect, useMemo, useState } from 'react'; +import { type JSX, lazy, useEffect, useMemo, useState } from 'react'; import { Navigate, Outlet, useLocation, useNavigate } from 'react-router-dom'; import { Boundary } from '@lib/components/Boundary'; @@ -15,6 +11,7 @@ import { ApiEndpoints, apiUrl } from '@lib/index'; import { useQuery } from '@tanstack/react-query'; import { api } from '../../App'; import { getActions } from '../../defaults/actions'; +import { Loadable } from '../../functions/loading'; import * as classes from '../../main.css'; import { useGlobalSettingsState, @@ -30,10 +27,14 @@ import { type PluginUIFeature, PluginUIFeatureType } from '../plugins/PluginUIFeature'; -import GlobalPreviewDrawer from '../previews/GlobalPreviewDrawer'; import { FirstUseTour } from './FirstUseTour'; import { Footer } from './Footer'; import { Header } from './Header'; +import { firstStore, searchShortcutKey } from './spotlightStore'; + +const GlobalPreviewDrawer = Loadable( + lazy(() => import('../previews/GlobalPreviewDrawer')) +); export const ProtectedRoute = ({ children }: { children: JSX.Element }) => { const location = useLocation(); @@ -55,10 +56,6 @@ export const ProtectedRoute = ({ children }: { children: JSX.Element }) => { return children; }; -export const [firstStore, firstSpotlight] = createSpotlight(); - -export const searchShortcutKey = 'mod+K'; - export default function LayoutComponent() { const navigate = useNavigate(); const location = useLocation(); diff --git a/src/frontend/src/components/nav/SearchDrawer.tsx b/src/frontend/src/components/nav/SearchDrawer.tsx index 11d9663a0f..55d7c8130d 100644 --- a/src/frontend/src/components/nav/SearchDrawer.tsx +++ b/src/frontend/src/components/nav/SearchDrawer.tsx @@ -212,7 +212,7 @@ function QueryResultGroup({ * Construct a drawer which provides quick-search functionality * @param */ -export function SearchDrawer({ +export default function SearchDrawer({ opened, onClose }: Readonly<{ diff --git a/src/frontend/src/components/nav/spotlightStore.ts b/src/frontend/src/components/nav/spotlightStore.ts new file mode 100644 index 0000000000..de5be82872 --- /dev/null +++ b/src/frontend/src/components/nav/spotlightStore.ts @@ -0,0 +1,5 @@ +import { createSpotlight } from '@mantine/spotlight'; + +export const [firstStore, firstSpotlight] = createSpotlight(); + +export const searchShortcutKey = 'mod+K'; diff --git a/src/frontend/src/components/plugins/PluginRoutes.tsx b/src/frontend/src/components/plugins/PluginRoutes.tsx index 49878735f6..9f7cfa2a84 100644 --- a/src/frontend/src/components/plugins/PluginRoutes.tsx +++ b/src/frontend/src/components/plugins/PluginRoutes.tsx @@ -8,7 +8,7 @@ import { useInvenTreeContext } from './PluginContext'; import NotFound from '../errors/NotFound'; -export function PluginRoutes() { +export default function PluginRoutes() { const routes = usePluginUIFeature({ featureType: 'route', context: {} diff --git a/src/frontend/src/components/plugins/PluginSource.tsx b/src/frontend/src/components/plugins/PluginSource.tsx index 57a5eb1832..f7f58970bb 100644 --- a/src/frontend/src/components/plugins/PluginSource.tsx +++ b/src/frontend/src/components/plugins/PluginSource.tsx @@ -1,4 +1,5 @@ import type { InvenTreePluginContext } from '@lib/types/Plugins'; +import { ensurePluginGlobals } from '../../functions/pluginGlobals'; import { generateUrl } from '../../functions/urls'; import { useLocalState } from '../../states/LocalState'; @@ -15,6 +16,8 @@ export async function loadExternalPluginSource(source: string) { const url = generateUrl(source); + await ensurePluginGlobals(); + const module = await import(/* @vite-ignore */ url) .catch((error) => { console.error(`ERR: Failed to load plugin from ${url}:`, error); diff --git a/src/frontend/src/functions/loading.tsx b/src/frontend/src/functions/loading.tsx index 2264871376..5de3093c09 100644 --- a/src/frontend/src/functions/loading.tsx +++ b/src/frontend/src/functions/loading.tsx @@ -40,11 +40,6 @@ export function Loadable( ); } -export function LoadingItem({ item }: Readonly<{ item: any }>): JSX.Element { - const Itm = Loadable(item); - return ; -} - /** * Like Loadable, but never suspends: the import is resolved into plain * state instead of thrown as a Suspense promise, so React.lazy's commit- diff --git a/src/frontend/src/functions/pluginGlobals.tsx b/src/frontend/src/functions/pluginGlobals.tsx new file mode 100644 index 0000000000..ff56acedb1 --- /dev/null +++ b/src/frontend/src/functions/pluginGlobals.tsx @@ -0,0 +1,10 @@ +let pluginGlobals: Promise | null = null; + +/* + * Ensure the window globals required by external plugins are set, + * loading them on first use. + */ +export function ensurePluginGlobals(): Promise { + pluginGlobals ??= import('./pluginGlobalsSetup'); + return pluginGlobals; +} diff --git a/src/frontend/src/functions/pluginGlobalsSetup.tsx b/src/frontend/src/functions/pluginGlobalsSetup.tsx new file mode 100644 index 0000000000..11f55ee33e --- /dev/null +++ b/src/frontend/src/functions/pluginGlobalsSetup.tsx @@ -0,0 +1,22 @@ +/* + * Expose shared modules as window globals for use in external plugins. + * + * This is loaded on demand (see ensurePluginGlobals in ./pluginGlobals) rather than at startup: + * the namespace imports below defeat tree-shaking, so keeping them out of the + * initial bundle avoids shipping all of @mantine/core to the login page. + */ +import * as LinguiCore from '@lingui/core'; +import * as LinguiReact from '@lingui/react'; +import * as MantineCore from '@mantine/core'; +import * as MantineNotifications from '@mantine/notifications'; +import * as React from 'react'; +import * as ReactDOM from 'react-dom'; +import * as ReactDOMClient from 'react-dom/client'; + +window.React = React; +window.ReactDOM = ReactDOM; +window.ReactDOMClient = ReactDOMClient; +window.MantineCore = MantineCore; +window.MantineNotifications = MantineNotifications; +window.LinguiCore = LinguiCore; +window.LinguiReact = LinguiReact; diff --git a/src/frontend/src/hooks/UseRemotePlugin.tsx b/src/frontend/src/hooks/UseRemotePlugin.tsx index 8d0460cfe4..da4972d1b9 100644 --- a/src/frontend/src/hooks/UseRemotePlugin.tsx +++ b/src/frontend/src/hooks/UseRemotePlugin.tsx @@ -2,6 +2,7 @@ import type { InvenTreePluginContext } from '@lib/types/Plugins'; import { t } from '@lingui/core/macro'; import { useCallback, useEffect, useMemo, useState } from 'react'; import type { ReactElement } from 'react'; +import { ensurePluginGlobals } from '../functions/pluginGlobals'; import { useLocalState } from '../states/LocalState'; type LegacyPluginEntryFn = ( @@ -94,6 +95,7 @@ export function useRemotePlugin({ const loadModule = async () => { try { + await ensurePluginGlobals(); const mod = await import(/* @vite-ignore */ moduleUrl); if (!cancelled) setRemoteModule(mod); } catch (err) { diff --git a/src/frontend/src/main.tsx b/src/frontend/src/main.tsx index 1bbb000b3f..de1083d7ab 100644 --- a/src/frontend/src/main.tsx +++ b/src/frontend/src/main.tsx @@ -4,19 +4,19 @@ import '@mantine/core/styles.css'; import '@mantine/dates/styles.css'; import '@mantine/notifications/styles.css'; import '@mantine/spotlight/styles.css'; -import * as Sentry from '@sentry/react'; import 'mantine-contextmenu/styles.css'; import 'mantine-datatable/styles.css'; import 'react-grid-layout/css/styles.css'; import 'react-resizable/css/styles.css'; -import * as LinguiCore from '@lingui/core'; -import * as LinguiReact from '@lingui/react'; -// Global types to be exported for use in plugins -import * as MantineCore from '@mantine/core'; -import * as MantineNotifications from '@mantine/notifications'; -import * as React from 'react'; -import * as ReactDOM from 'react-dom'; +import type * as LinguiCore from '@lingui/core'; +import type * as LinguiReact from '@lingui/react'; +// Global types to be exported for use in plugins (see functions/pluginGlobalsSetup) +import type * as MantineCore from '@mantine/core'; +import type * as MantineNotifications from '@mantine/notifications'; +import { StrictMode } from 'react'; +import type * as React from 'react'; +import type * as ReactDOM from 'react-dom'; import * as ReactDOMClient from 'react-dom/client'; import './styles/overrides.css'; @@ -103,11 +103,18 @@ window.INVENTREE_SETTINGS = { if (window.INVENTREE_SETTINGS.sentry_dsn) { console.log('Sentry enabled'); - Sentry.init({ - dsn: window.INVENTREE_SETTINGS.sentry_dsn, - tracesSampleRate: 1.0, - environment: window.INVENTREE_SETTINGS.environment || 'default' - }); + // Loaded on demand to keep Sentry out of the initial bundle + import('@sentry/react') + .then((Sentry) => + Sentry.init({ + dsn: window.INVENTREE_SETTINGS.sentry_dsn, + tracesSampleRate: 1.0, + environment: window.INVENTREE_SETTINGS.environment || 'default' + }) + ) + .catch((err) => { + console.error('Failed to load Sentry:', err); + }); } // Redirect to base url if on / if (window.location.pathname === '/') { @@ -117,16 +124,7 @@ if (window.location.pathname === '/') { ReactDOMClient.createRoot( document.getElementById('root') as HTMLElement ).render( - + - + ); - -// All window globals assigned here since these modules are already statically bundled -window.React = React; -window.ReactDOM = ReactDOM; -window.ReactDOMClient = ReactDOMClient; -window.MantineCore = MantineCore; -window.MantineNotifications = MantineNotifications; -window.LinguiCore = LinguiCore; -window.LinguiReact = LinguiReact; diff --git a/src/frontend/src/router.tsx b/src/frontend/src/router.tsx index 3c7248d0f0..fcc3017ae8 100644 --- a/src/frontend/src/router.tsx +++ b/src/frontend/src/router.tsx @@ -4,8 +4,6 @@ import { Navigate, Route, Routes } from 'react-router-dom'; import { EagerLoadable, Loadable } from './functions/loading'; import { onLocaleReady } from './functions/localeReady'; -import { PluginRoutes } from './components/plugins/PluginRoutes'; - // Lazy loaded pages // These two are mutually exclusive and one of them is always needed // immediately on initial load, so they're loaded eagerly rather than via @@ -17,6 +15,10 @@ export const LoginLayoutComponent = EagerLoadable( () => import('./pages/Auth/Layout') ); +export const PluginRoutes = Loadable( + lazy(() => import('./components/plugins/PluginRoutes')) +); + export const Home = Loadable(lazy(() => import('./pages/Index/Home'))); export const CompanyDetail = Loadable(