mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-27 10:08:44 +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 { 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
|
||||
|
||||
@@ -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 (
|
||||
<Tour active={active} onClose={completeTour} zIndex={10002}>
|
||||
{ftusteps.map((step, index) => (
|
||||
<Tour.Step
|
||||
key={index}
|
||||
target={`#${step.target}`}
|
||||
title={step.title}
|
||||
onStepOpen={step.action_before}
|
||||
>
|
||||
{step.description}
|
||||
</Tour.Step>
|
||||
))}
|
||||
</Tour>
|
||||
<FirstUseTourView active={active} onClose={completeTour} steps={ftusteps} />
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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 { 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(
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<{
|
||||
|
||||
@@ -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';
|
||||
|
||||
export function PluginRoutes() {
|
||||
export default function PluginRoutes() {
|
||||
const routes = usePluginUIFeature<RouteUIFeature>({
|
||||
featureType: 'route',
|
||||
context: {}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
* 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 { 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) {
|
||||
|
||||
+22
-24
@@ -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(
|
||||
<React.StrictMode>
|
||||
<StrictMode>
|
||||
<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 { 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(
|
||||
|
||||
Reference in new issue
Block a user