Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

perf(webv2): split widget hosts into own chunks

joshistoastd29aa913d945
diff --git a/invokeai/frontend/webv2/src/features/queue/widget.ts b/invokeai/frontend/webv2/src/features/queue/widget.ts
index 470cb6a3ae..61dcbec706 100644
--- a/invokeai/frontend/webv2/src/features/queue/widget.ts
+++ b/invokeai/frontend/webv2/src/features/queue/widget.ts
@@ -1,9 +1,11 @@
/** Queue-owned deferred widget slots; Workbench supplies the manifest adapter. */
-export {
- ModelCacheFooter,
- QueueDataRuntime,
- QueueHeaderActions,
- QueueHeaderLabel,
- QueueHeaderMenu,
- QueueWidgetView,
-} from './ui';
+export const loadQueueWidgetImplementation = () =>
+ import('./ui').then((module) => ({
+ footer: module.ModelCacheFooter,
+ headerActions: module.QueueHeaderActions,
+ headerLabel: module.QueueHeaderLabel,
+ headerMenu: module.QueueHeaderMenu,
+ view: module.QueueWidgetView,
+ }));
+
+export const loadQueueWidgetHost = () => import('./ui/QueueDataRuntime').then((module) => module.QueueDataRuntime);
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/implementation.ts b/invokeai/frontend/webv2/src/features/workflow/ui/implementation.ts
index b354d851f9..dfe9701174 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/implementation.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/implementation.ts
@@ -1,15 +1,9 @@
-import {
- WorkflowDialogHost,
- WorkflowHeaderActions,
- WorkflowMenuItems,
- WorkflowWidgetLabel,
-} from './WorkflowWidgetChrome';
+import { WorkflowHeaderActions, WorkflowMenuItems, WorkflowWidgetLabel } from './WorkflowWidgetChrome';
import { WorkflowWidgetView } from './WorkflowWidgetView';
export const widgetImplementation = {
headerActions: WorkflowHeaderActions,
headerLabel: WorkflowWidgetLabel,
headerMenu: WorkflowMenuItems,
- host: WorkflowDialogHost,
view: WorkflowWidgetView,
};
diff --git a/invokeai/frontend/webv2/src/features/workflow/widget.ts b/invokeai/frontend/webv2/src/features/workflow/widget.ts
index 1557198793..623e1208d3 100644
--- a/invokeai/frontend/webv2/src/features/workflow/widget.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/widget.ts
@@ -1,2 +1,5 @@
export const loadWorkflowWidgetImplementation = () =>
import('./ui/implementation').then((module) => module.widgetImplementation);
+
+export const loadWorkflowWidgetHost = () =>
+ import('./ui/WorkflowWidgetChrome').then((module) => module.WorkflowDialogHost);
diff --git a/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.ts b/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.ts
index f5a963d34c..16167141b1 100644
--- a/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.ts
+++ b/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.ts
@@ -93,7 +93,7 @@ export const writeBootWidgetHint = (typeIds: readonly WidgetTypeId[]): void => {
*/
export const preloadBootWidgets = (): void => {
for (const widget of getWidgetHosts()) {
- widget.implementation.preload();
+ widget.host?.preload();
}
for (const typeId of readBootWidgetHint() ?? getBootWidgetTypeIds(defaultLayoutPreset.snapshot)) {
diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetHosts.tsx b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetHosts.tsx
index 37a37f45d9..f02805264c 100644
--- a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetHosts.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetHosts.tsx
@@ -4,9 +4,16 @@ import { Suspense, use } from 'react';
import { WidgetFailureBoundary } from './WidgetFailureBoundary';
const WidgetHost = ({ widget }: { widget: ReturnType<typeof getWidgetHosts>[number] }) => {
- const Host = use(widget.implementation.load()).host;
+ const Host = use(widget.host!.load());
- return Host ? <Host /> : null;
+ // react-compiler flags any JSX tag that is directly the value returned by
+ // `use()` as though it were freshly created each render. `Host` is the
+ // module's cached export, resolved once by the deferred resource and
+ // stable across renders; the false positive disappears the moment the
+ // value is read through a property access instead of being the call's
+ // direct result, which is what the sibling `WidgetRenderer` slots do.
+ // eslint-disable-next-line react/react-compiler
+ return <Host />;
};
const WidgetHostBoundary = ({ widget }: { widget: ReturnType<typeof getWidgetHosts>[number] }) => {
@@ -21,7 +28,7 @@ const WidgetHostBoundary = ({ widget }: { widget: ReturnType<typeof getWidgetHos
resetKey={widget.manifest.id}
widget={widget}
widgetId={widget.manifest.id}
- onRetry={widget.implementation.retry}
+ onRetry={widget.host!.retry}
>
{content}
</WidgetFailureBoundary>
diff --git a/invokeai/frontend/webv2/src/workbench/widgetContracts.ts b/invokeai/frontend/webv2/src/workbench/widgetContracts.ts
index 898d8e6ab9..7053ff69d0 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetContracts.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetContracts.ts
@@ -93,7 +93,6 @@ export type WidgetHost = ComponentType;
/** Deferred render implementation shared by every slot for one widget type. */
export interface WidgetImplementation {
view: WidgetView;
- host?: WidgetHost;
headerActions?: WidgetHeaderActions;
headerLabel?: WidgetHeaderLabel;
headerMenu?: WidgetHeaderMenu;
@@ -309,8 +308,16 @@ export interface WidgetManifest {
* failure state, and retry; callers never invoke this directly.
*/
load: () => Promise<WidgetImplementation>;
- /** The deferred implementation contains a singleton host mounted at editor boot. */
- hasHost?: boolean;
+ /**
+ * A singleton runtime the editor mounts once at boot, in its own chunk.
+ *
+ * Deliberately separate from `load`: hosts are data runtimes and dialog
+ * shells that must run whether or not the widget is on screen, so sharing a
+ * module with the view meant every boot paid for the view. Splitting the
+ * loader makes it impossible to declare an always-on part without giving it
+ * its own chunk.
+ */
+ loadHost?: () => Promise<WidgetHost>;
/** When set, the frame header shows a gear that opens this settings dialog section. */
settingsSection?: SettingsSectionId;
state?: WidgetStateRegistration;
@@ -335,6 +342,7 @@ export interface RegisteredWidget {
manifest: NormalizedWidgetManifest;
status: 'enabled' | 'disabled' | 'hidden';
failure?: WidgetFailure;
+ host?: DeferredResource<WidgetHost>;
}
export interface WidgetFailure {
diff --git a/invokeai/frontend/webv2/src/workbench/widgetRegistry.test.ts b/invokeai/frontend/webv2/src/workbench/widgetRegistry.test.ts
index d0d8f9ff16..d9c7d380af 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetRegistry.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetRegistry.test.ts
@@ -3,7 +3,13 @@ import type { WidgetManifest } from '@workbench/widgetContracts';
import { ImageUpscaleIcon } from 'lucide-react';
import { describe, expect, it, vi } from 'vitest';
-import { getWidgetHosts, registerFirstPartyWidgets, registerWidgets } from './widgetRegistry';
+import {
+ getWidgetById,
+ getWidgetHosts,
+ registeredWidgets,
+ registerFirstPartyWidgets,
+ registerWidgets,
+} from './widgetRegistry';
import { upscaleWidgetManifest } from './widgets/upscale/manifest';
const TestIcon = () => null;
@@ -103,3 +109,34 @@ describe('widget registry', () => {
expect(badApiVersion.failure?.message).toContain('unsupported apiVersion');
});
});
+
+describe('widget hosts', () => {
+ it('offers a host resource only for manifests that declare loadHost', () => {
+ const hosts = getWidgetHosts();
+
+ expect(hosts.length).toBeGreaterThan(0);
+
+ for (const widget of hosts) {
+ expect(widget.manifest.loadHost).toBeTypeOf('function');
+ expect(widget.host).toBeDefined();
+ }
+
+ for (const widget of registeredWidgets) {
+ if (!widget.manifest.loadHost) {
+ expect(widget.host).toBeUndefined();
+ }
+ }
+ });
+
+ it('loads a host without touching the widget implementation resource', async () => {
+ const workflow = getWidgetById('workflow');
+
+ expect(workflow?.host).toBeDefined();
+
+ const statusBeforeHostLoad = workflow?.implementation.getStatus();
+ await workflow?.host?.load();
+
+ // The whole point: mounting the singleton host must not drag the view chunk in.
+ expect(workflow?.implementation.getStatus()).toBe(statusBeforeHostLoad);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts b/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
index a2bd03d051..f7f27796cd 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
@@ -9,6 +9,7 @@ import type {
import { getAuthSession } from '@features/identity';
+import { createDeferredResource } from './deferredResource';
import { createWidgetImplementationResource } from './widgetImplementationResource';
import { autosaveStatusWidgetManifest } from './widgets/autosave-status/manifest';
import { canvasWidgetManifest } from './widgets/canvas/manifest';
@@ -96,10 +97,13 @@ export const registerWidgets = (manifests: WidgetManifest[]): RegisteredWidget[]
manifests.map((rawManifest) => {
const manifest = normalizeWidgetManifest(rawManifest);
+ const host = manifest.loadHost ? createDeferredResource(manifest.loadHost) : undefined;
+
try {
validateManifest(manifest);
return {
+ host,
implementation: createWidgetImplementationResource(manifest.id, manifest.load),
manifest,
status: 'enabled' as const,
@@ -110,6 +114,7 @@ export const registerWidgets = (manifests: WidgetManifest[]): RegisteredWidget[]
return {
failure,
+ host,
implementation: createWidgetImplementationResource(manifest.id, manifest.load),
manifest,
status,
@@ -145,7 +150,7 @@ export const getWidgetsForRegion = (region: WidgetRegion): RegisteredWidget[] =>
export const getWidgetHosts = (): RegisteredWidget[] =>
registeredWidgets.filter(
- (widget) => widget.status === 'enabled' && widget.manifest.hasHost && isWidgetAvailable(widget)
+ (widget) => widget.status === 'enabled' && widget.host !== undefined && isWidgetAvailable(widget)
);
export const getWidgetById = (widgetId: WidgetTypeId): RegisteredWidget | undefined =>
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/image-map/implementation.ts b/invokeai/frontend/webv2/src/workbench/widgets/image-map/implementation.ts
index 629bfded5f..ecd81f4a52 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/image-map/implementation.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/image-map/implementation.ts
@@ -1,6 +1,5 @@
import type { WidgetImplementation } from '@workbench/widgetContracts';
-import { ImageMapDataRuntime } from './ImageMapDataRuntime';
import { ImageMapHeaderActions } from './ImageMapHeaderActions';
import { ImageMapWidgetFooter } from './ImageMapWidgetFooter';
import { ImageMapWidgetView } from './ImageMapWidgetView';
@@ -8,6 +7,5 @@ import { ImageMapWidgetView } from './ImageMapWidgetView';
export const widgetImplementation = {
footer: ImageMapWidgetFooter,
headerActions: ImageMapHeaderActions,
- host: ImageMapDataRuntime,
view: ImageMapWidgetView,
} satisfies WidgetImplementation;
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/image-map/manifest.ts b/invokeai/frontend/webv2/src/workbench/widgets/image-map/manifest.ts
index 495393b29d..e1399e2184 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/image-map/manifest.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/image-map/manifest.ts
@@ -7,10 +7,10 @@ export const imageMapWidgetManifest: WidgetManifest = {
allowMultiple: false,
allowedRegions: ['left', 'right', 'center'],
failurePolicy: { isolateRenderFailure: true, onRegistrationFailure: 'disable' },
- hasHost: true,
icon: MapIcon,
id: 'image-map',
label: (t) => t('widgets.labels.imageMap'),
load: () => import('./implementation').then((module) => module.widgetImplementation),
+ loadHost: () => import('./ImageMapDataRuntime').then((module) => module.ImageMapDataRuntime),
version: 1,
};
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/queue/manifest.ts b/invokeai/frontend/webv2/src/workbench/widgets/queue/manifest.ts
index 0f0a005546..a16abaffe7 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/queue/manifest.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/queue/manifest.ts
@@ -1,5 +1,6 @@
import type { WidgetManifest } from '@workbench/widgetContracts';
+import { loadQueueWidgetHost, loadQueueWidgetImplementation } from '@features/queue/widget';
import { ListOrderedIcon } from 'lucide-react';
export const queueWidgetManifest: WidgetManifest = {
@@ -9,19 +10,11 @@ export const queueWidgetManifest: WidgetManifest = {
isolateRenderFailure: true,
onRegistrationFailure: 'disable',
},
- hasHost: true,
icon: ListOrderedIcon,
id: 'queue',
label: (t) => t('widgets.labels.queue'),
- load: () =>
- import('@features/queue/widget').then((module) => ({
- footer: module.ModelCacheFooter,
- headerActions: module.QueueHeaderActions,
- headerLabel: module.QueueHeaderLabel,
- headerMenu: module.QueueHeaderMenu,
- host: module.QueueDataRuntime,
- view: module.QueueWidgetView,
- })),
+ load: loadQueueWidgetImplementation,
+ loadHost: loadQueueWidgetHost,
settingsSection: 'queue',
version: 1,
};
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/workflow/manifest.ts b/invokeai/frontend/webv2/src/workbench/widgets/workflow/manifest.ts
index 3a3442b31d..0576ac1039 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/workflow/manifest.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/workflow/manifest.ts
@@ -1,6 +1,6 @@
import type { WidgetManifest } from '@workbench/widgetContracts';
-import { loadWorkflowWidgetImplementation } from '@features/workflow/widget';
+import { loadWorkflowWidgetHost, loadWorkflowWidgetImplementation } from '@features/workflow/widget';
import { WorkflowIcon } from 'lucide-react';
export const workflowWidgetManifest: WidgetManifest = {
@@ -13,11 +13,11 @@ export const workflowWidgetManifest: WidgetManifest = {
sourceId: 'workflow',
surfaces: ['center', 'left', 'bottom'],
},
- hasHost: true,
icon: WorkflowIcon,
id: 'workflow',
label: (t) => t('widgets.labels.workflow'),
load: loadWorkflowWidgetImplementation,
+ loadHost: loadWorkflowWidgetHost,
settingsSection: 'workflow',
version: 1,
};