mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-29 19:14:35 +00:00
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
This commit is contained in:
1 parent
fd3dfc8517
commit
b686d7cb64
15 files changed
+123
-66
No files matched your search
@@ -3,7 +3,7 @@ import { ActionIcon, Tooltip } from '@mantine/core';
|
|||||||
import { IconCommand } from '@tabler/icons-react';
|
import { IconCommand } from '@tabler/icons-react';
|
||||||
|
|
||||||
import { useLocalLibState } from '@lib/states/LocalLibState';
|
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
|
* A button which opens the quick command modal
|
||||||
|
|||||||
@@ -1,32 +1,30 @@
|
|||||||
import { ApiEndpoints, apiUrl } from '@lib/index';
|
import { ApiEndpoints, apiUrl } from '@lib/index';
|
||||||
import { t } from '@lingui/core/macro';
|
import { t } from '@lingui/core/macro';
|
||||||
import { Trans } from '@lingui/react/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 { useWindowEvent } from '@mantine/hooks';
|
||||||
import { type NotificationData, notifications } from '@mantine/notifications';
|
import { type NotificationData, notifications } from '@mantine/notifications';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
type Dispatch,
|
type Dispatch,
|
||||||
type SetStateAction,
|
type SetStateAction,
|
||||||
|
lazy,
|
||||||
useEffect,
|
useEffect,
|
||||||
useMemo,
|
useMemo,
|
||||||
useRef,
|
useRef,
|
||||||
useState
|
useState
|
||||||
} from 'react';
|
} from 'react';
|
||||||
import { api } from '../../App';
|
import { api } from '../../App';
|
||||||
|
import { Loadable } from '../../functions/loading';
|
||||||
import { useServerApiState } from '../../states/ServerApiState';
|
import { useServerApiState } from '../../states/ServerApiState';
|
||||||
import {
|
import {
|
||||||
useGlobalSettingsState,
|
useGlobalSettingsState,
|
||||||
useUserSettingsState
|
useUserSettingsState
|
||||||
} from '../../states/SettingsStates';
|
} from '../../states/SettingsStates';
|
||||||
import { useUserState } from '../../states/UserState';
|
import { useUserState } from '../../states/UserState';
|
||||||
|
import type { FtuStep } from './FirstUseTourView';
|
||||||
|
|
||||||
interface FtuStep {
|
const FirstUseTourView = Loadable(lazy(() => import('./FirstUseTourView')));
|
||||||
target: string;
|
|
||||||
title: string;
|
|
||||||
description: string;
|
|
||||||
action_before?: () => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
const ftuKex = 'org.inventree.i.tip.ftug';
|
const ftuKex = 'org.inventree.i.tip.ftug';
|
||||||
|
|
||||||
@@ -153,22 +151,11 @@ export function FirstUseTour() {
|
|||||||
}, [showNudge]);
|
}, [showNudge]);
|
||||||
|
|
||||||
// rendering
|
// rendering
|
||||||
if (!user) {
|
if (!user || !active) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<Tour active={active} onClose={completeTour} zIndex={10002}>
|
<FirstUseTourView active={active} onClose={completeTour} steps={ftusteps} />
|
||||||
{ftusteps.map((step, index) => (
|
|
||||||
<Tour.Step
|
|
||||||
key={index}
|
|
||||||
target={`#${step.target}`}
|
|
||||||
title={step.title}
|
|
||||||
onStepOpen={step.action_before}
|
|
||||||
>
|
|
||||||
{step.description}
|
|
||||||
</Tour.Step>
|
|
||||||
))}
|
|
||||||
</Tour>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 (
|
||||||
|
<Tour active={active} onClose={onClose} zIndex={10002}>
|
||||||
|
{steps.map((step, index) => (
|
||||||
|
<Tour.Step
|
||||||
|
key={index}
|
||||||
|
target={`#${step.target}`}
|
||||||
|
title={step.title}
|
||||||
|
onStepOpen={step.action_before}
|
||||||
|
>
|
||||||
|
{step.description}
|
||||||
|
</Tour.Step>
|
||||||
|
))}
|
||||||
|
</Tour>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -12,8 +12,10 @@ import {
|
|||||||
import { useDisclosure, useDocumentVisibility } from '@mantine/hooks';
|
import { useDisclosure, useDocumentVisibility } from '@mantine/hooks';
|
||||||
import { IconBell, IconSearch, IconUserBolt } from '@tabler/icons-react';
|
import { IconBell, IconSearch, IconUserBolt } from '@tabler/icons-react';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
import { lazy } from 'react';
|
||||||
import { type ReactNode, useEffect, useMemo, useState } from 'react';
|
import { type ReactNode, useEffect, useMemo, useState } from 'react';
|
||||||
import { Link, matchPath, useLocation, useMatch } from 'react-router-dom';
|
import { Link, matchPath, useLocation, useMatch } from 'react-router-dom';
|
||||||
|
import { Loadable } from '../../functions/loading';
|
||||||
|
|
||||||
import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
|
import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
|
||||||
import { apiUrl } from '@lib/functions/Api';
|
import { apiUrl } from '@lib/functions/Api';
|
||||||
@@ -39,7 +41,8 @@ import { MainMenu } from './MainMenu';
|
|||||||
import { NavHoverMenu } from './NavHoverMenu';
|
import { NavHoverMenu } from './NavHoverMenu';
|
||||||
import { NavigationDrawer } from './NavigationDrawer';
|
import { NavigationDrawer } from './NavigationDrawer';
|
||||||
import { NotificationDrawer } from './NotificationDrawer';
|
import { NotificationDrawer } from './NotificationDrawer';
|
||||||
import { SearchDrawer } from './SearchDrawer';
|
|
||||||
|
const SearchDrawer = Loadable(lazy(() => import('./SearchDrawer')));
|
||||||
|
|
||||||
export function Header() {
|
export function Header() {
|
||||||
const [setNavigationOpen, navigationOpen] = useLocalState(
|
const [setNavigationOpen, navigationOpen] = useLocalState(
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
import { t } from '@lingui/core/macro';
|
import { t } from '@lingui/core/macro';
|
||||||
import { Container, Flex, Space } from '@mantine/core';
|
import { Container, Flex, Space } from '@mantine/core';
|
||||||
import {
|
import { Spotlight, type SpotlightActionData } from '@mantine/spotlight';
|
||||||
Spotlight,
|
|
||||||
type SpotlightActionData,
|
|
||||||
createSpotlight
|
|
||||||
} from '@mantine/spotlight';
|
|
||||||
import { IconSearch } from '@tabler/icons-react';
|
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 { Navigate, Outlet, useLocation, useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
import { Boundary } from '@lib/components/Boundary';
|
import { Boundary } from '@lib/components/Boundary';
|
||||||
@@ -15,6 +11,7 @@ import { ApiEndpoints, apiUrl } from '@lib/index';
|
|||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { api } from '../../App';
|
import { api } from '../../App';
|
||||||
import { getActions } from '../../defaults/actions';
|
import { getActions } from '../../defaults/actions';
|
||||||
|
import { Loadable } from '../../functions/loading';
|
||||||
import * as classes from '../../main.css';
|
import * as classes from '../../main.css';
|
||||||
import {
|
import {
|
||||||
useGlobalSettingsState,
|
useGlobalSettingsState,
|
||||||
@@ -30,10 +27,14 @@ import {
|
|||||||
type PluginUIFeature,
|
type PluginUIFeature,
|
||||||
PluginUIFeatureType
|
PluginUIFeatureType
|
||||||
} from '../plugins/PluginUIFeature';
|
} from '../plugins/PluginUIFeature';
|
||||||
import GlobalPreviewDrawer from '../previews/GlobalPreviewDrawer';
|
|
||||||
import { FirstUseTour } from './FirstUseTour';
|
import { FirstUseTour } from './FirstUseTour';
|
||||||
import { Footer } from './Footer';
|
import { Footer } from './Footer';
|
||||||
import { Header } from './Header';
|
import { Header } from './Header';
|
||||||
|
import { firstStore, searchShortcutKey } from './spotlightStore';
|
||||||
|
|
||||||
|
const GlobalPreviewDrawer = Loadable(
|
||||||
|
lazy(() => import('../previews/GlobalPreviewDrawer'))
|
||||||
|
);
|
||||||
|
|
||||||
export const ProtectedRoute = ({ children }: { children: JSX.Element }) => {
|
export const ProtectedRoute = ({ children }: { children: JSX.Element }) => {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
@@ -55,10 +56,6 @@ export const ProtectedRoute = ({ children }: { children: JSX.Element }) => {
|
|||||||
return children;
|
return children;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const [firstStore, firstSpotlight] = createSpotlight();
|
|
||||||
|
|
||||||
export const searchShortcutKey = 'mod+K';
|
|
||||||
|
|
||||||
export default function LayoutComponent() {
|
export default function LayoutComponent() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|||||||
@@ -212,7 +212,7 @@ function QueryResultGroup({
|
|||||||
* Construct a drawer which provides quick-search functionality
|
* Construct a drawer which provides quick-search functionality
|
||||||
* @param
|
* @param
|
||||||
*/
|
*/
|
||||||
export function SearchDrawer({
|
export default function SearchDrawer({
|
||||||
opened,
|
opened,
|
||||||
onClose
|
onClose
|
||||||
}: Readonly<{
|
}: Readonly<{
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import { createSpotlight } from '@mantine/spotlight';
|
||||||
|
|
||||||
|
export const [firstStore, firstSpotlight] = createSpotlight();
|
||||||
|
|
||||||
|
export const searchShortcutKey = 'mod+K';
|
||||||
@@ -8,7 +8,7 @@ import { useInvenTreeContext } from './PluginContext';
|
|||||||
|
|
||||||
import NotFound from '../errors/NotFound';
|
import NotFound from '../errors/NotFound';
|
||||||
|
|
||||||
export function PluginRoutes() {
|
export default function PluginRoutes() {
|
||||||
const routes = usePluginUIFeature<RouteUIFeature>({
|
const routes = usePluginUIFeature<RouteUIFeature>({
|
||||||
featureType: 'route',
|
featureType: 'route',
|
||||||
context: {}
|
context: {}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { InvenTreePluginContext } from '@lib/types/Plugins';
|
import type { InvenTreePluginContext } from '@lib/types/Plugins';
|
||||||
|
import { ensurePluginGlobals } from '../../functions/pluginGlobals';
|
||||||
import { generateUrl } from '../../functions/urls';
|
import { generateUrl } from '../../functions/urls';
|
||||||
import { useLocalState } from '../../states/LocalState';
|
import { useLocalState } from '../../states/LocalState';
|
||||||
|
|
||||||
@@ -15,6 +16,8 @@ export async function loadExternalPluginSource(source: string) {
|
|||||||
|
|
||||||
const url = generateUrl(source);
|
const url = generateUrl(source);
|
||||||
|
|
||||||
|
await ensurePluginGlobals();
|
||||||
|
|
||||||
const module = await import(/* @vite-ignore */ url)
|
const module = await import(/* @vite-ignore */ url)
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error(`ERR: Failed to load plugin from ${url}:`, error);
|
console.error(`ERR: Failed to load plugin from ${url}:`, error);
|
||||||
|
|||||||
@@ -40,11 +40,6 @@ export function Loadable(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function LoadingItem({ item }: Readonly<{ item: any }>): JSX.Element {
|
|
||||||
const Itm = Loadable(item);
|
|
||||||
return <Itm />;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Like Loadable, but never suspends: the import is resolved into plain
|
* Like Loadable, but never suspends: the import is resolved into plain
|
||||||
* state instead of thrown as a Suspense promise, so React.lazy's commit-
|
* state instead of thrown as a Suspense promise, so React.lazy's commit-
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
let pluginGlobals: Promise<unknown> | null = null;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Ensure the window globals required by external plugins are set,
|
||||||
|
* loading them on first use.
|
||||||
|
*/
|
||||||
|
export function ensurePluginGlobals(): Promise<unknown> {
|
||||||
|
pluginGlobals ??= import('./pluginGlobalsSetup');
|
||||||
|
return pluginGlobals;
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
@@ -2,6 +2,7 @@ import type { InvenTreePluginContext } from '@lib/types/Plugins';
|
|||||||
import { t } from '@lingui/core/macro';
|
import { t } from '@lingui/core/macro';
|
||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import type { ReactElement } from 'react';
|
import type { ReactElement } from 'react';
|
||||||
|
import { ensurePluginGlobals } from '../functions/pluginGlobals';
|
||||||
import { useLocalState } from '../states/LocalState';
|
import { useLocalState } from '../states/LocalState';
|
||||||
|
|
||||||
type LegacyPluginEntryFn = (
|
type LegacyPluginEntryFn = (
|
||||||
@@ -94,6 +95,7 @@ export function useRemotePlugin({
|
|||||||
|
|
||||||
const loadModule = async () => {
|
const loadModule = async () => {
|
||||||
try {
|
try {
|
||||||
|
await ensurePluginGlobals();
|
||||||
const mod = await import(/* @vite-ignore */ moduleUrl);
|
const mod = await import(/* @vite-ignore */ moduleUrl);
|
||||||
if (!cancelled) setRemoteModule(mod);
|
if (!cancelled) setRemoteModule(mod);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
+22
-24
@@ -4,19 +4,19 @@ import '@mantine/core/styles.css';
|
|||||||
import '@mantine/dates/styles.css';
|
import '@mantine/dates/styles.css';
|
||||||
import '@mantine/notifications/styles.css';
|
import '@mantine/notifications/styles.css';
|
||||||
import '@mantine/spotlight/styles.css';
|
import '@mantine/spotlight/styles.css';
|
||||||
import * as Sentry from '@sentry/react';
|
|
||||||
import 'mantine-contextmenu/styles.css';
|
import 'mantine-contextmenu/styles.css';
|
||||||
import 'mantine-datatable/styles.css';
|
import 'mantine-datatable/styles.css';
|
||||||
import 'react-grid-layout/css/styles.css';
|
import 'react-grid-layout/css/styles.css';
|
||||||
import 'react-resizable/css/styles.css';
|
import 'react-resizable/css/styles.css';
|
||||||
|
|
||||||
import * as LinguiCore from '@lingui/core';
|
import type * as LinguiCore from '@lingui/core';
|
||||||
import * as LinguiReact from '@lingui/react';
|
import type * as LinguiReact from '@lingui/react';
|
||||||
// Global types to be exported for use in plugins
|
// Global types to be exported for use in plugins (see functions/pluginGlobalsSetup)
|
||||||
import * as MantineCore from '@mantine/core';
|
import type * as MantineCore from '@mantine/core';
|
||||||
import * as MantineNotifications from '@mantine/notifications';
|
import type * as MantineNotifications from '@mantine/notifications';
|
||||||
import * as React from 'react';
|
import { StrictMode } from 'react';
|
||||||
import * as ReactDOM from 'react-dom';
|
import type * as React from 'react';
|
||||||
|
import type * as ReactDOM from 'react-dom';
|
||||||
import * as ReactDOMClient from 'react-dom/client';
|
import * as ReactDOMClient from 'react-dom/client';
|
||||||
|
|
||||||
import './styles/overrides.css';
|
import './styles/overrides.css';
|
||||||
@@ -103,11 +103,18 @@ window.INVENTREE_SETTINGS = {
|
|||||||
|
|
||||||
if (window.INVENTREE_SETTINGS.sentry_dsn) {
|
if (window.INVENTREE_SETTINGS.sentry_dsn) {
|
||||||
console.log('Sentry enabled');
|
console.log('Sentry enabled');
|
||||||
Sentry.init({
|
// Loaded on demand to keep Sentry out of the initial bundle
|
||||||
dsn: window.INVENTREE_SETTINGS.sentry_dsn,
|
import('@sentry/react')
|
||||||
tracesSampleRate: 1.0,
|
.then((Sentry) =>
|
||||||
environment: window.INVENTREE_SETTINGS.environment || 'default'
|
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 /
|
// Redirect to base url if on /
|
||||||
if (window.location.pathname === '/') {
|
if (window.location.pathname === '/') {
|
||||||
@@ -117,16 +124,7 @@ if (window.location.pathname === '/') {
|
|||||||
ReactDOMClient.createRoot(
|
ReactDOMClient.createRoot(
|
||||||
document.getElementById('root') as HTMLElement
|
document.getElementById('root') as HTMLElement
|
||||||
).render(
|
).render(
|
||||||
<React.StrictMode>
|
<StrictMode>
|
||||||
<MainView />
|
<MainView />
|
||||||
</React.StrictMode>
|
</StrictMode>
|
||||||
);
|
);
|
||||||
|
|
||||||
// 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;
|
|
||||||
@@ -4,8 +4,6 @@ import { Navigate, Route, Routes } from 'react-router-dom';
|
|||||||
import { EagerLoadable, Loadable } from './functions/loading';
|
import { EagerLoadable, Loadable } from './functions/loading';
|
||||||
import { onLocaleReady } from './functions/localeReady';
|
import { onLocaleReady } from './functions/localeReady';
|
||||||
|
|
||||||
import { PluginRoutes } from './components/plugins/PluginRoutes';
|
|
||||||
|
|
||||||
// Lazy loaded pages
|
// Lazy loaded pages
|
||||||
// These two are mutually exclusive and one of them is always needed
|
// These two are mutually exclusive and one of them is always needed
|
||||||
// immediately on initial load, so they're loaded eagerly rather than via
|
// immediately on initial load, so they're loaded eagerly rather than via
|
||||||
@@ -17,6 +15,10 @@ export const LoginLayoutComponent = EagerLoadable(
|
|||||||
() => import('./pages/Auth/Layout')
|
() => import('./pages/Auth/Layout')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const PluginRoutes = Loadable(
|
||||||
|
lazy(() => import('./components/plugins/PluginRoutes'))
|
||||||
|
);
|
||||||
|
|
||||||
export const Home = Loadable(lazy(() => import('./pages/Index/Home')));
|
export const Home = Loadable(lazy(() => import('./pages/Index/Home')));
|
||||||
|
|
||||||
export const CompanyDetail = Loadable(
|
export const CompanyDetail = Loadable(
|
||||||
|
|||||||
Reference in new issue
Block a user